| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* A sensor driver for the magnetometer AK8975. |
| 4 |
* |
| 5 |
* Magnetic compass sensor driver for monitoring magnetic flux information. |
| 6 |
* |
| 7 |
* Copyright (c) 2010, NVIDIA Corporation. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/module.h> |
| 11 |
#include <linux/mod_devicetable.h> |
| 12 |
#include <linux/kernel.h> |
| 13 |
#include <linux/slab.h> |
| 14 |
#include <linux/i2c.h> |
| 15 |
#include <linux/interrupt.h> |
| 16 |
#include <linux/err.h> |
| 17 |
#include <linux/mutex.h> |
| 18 |
#include <linux/delay.h> |
| 19 |
#include <linux/bitops.h> |
| 20 |
#include <linux/gpio/consumer.h> |
| 21 |
#include <linux/regulator/consumer.h> |
| 22 |
#include <linux/pm_runtime.h> |
| 23 |
|
| 24 |
#include <linux/iio/iio.h> |
| 25 |
#include <linux/iio/sysfs.h> |
| 26 |
#include <linux/iio/buffer.h> |
| 27 |
#include <linux/iio/trigger.h> |
| 28 |
#include <linux/iio/trigger_consumer.h> |
| 29 |
#include <linux/iio/triggered_buffer.h> |
| 30 |
|
| 31 |
/* |
| 32 |
* Register definitions, as well as various shifts and masks to get at the |
| 33 |
* individual fields of the registers. |
| 34 |
*/ |
| 35 |
#define AK8975_REG_WIA 0x00 |
| 36 |
#define AK8975_DEVICE_ID 0x48 |
| 37 |
|
| 38 |
#define AK8975_REG_INFO 0x01 |
| 39 |
|
| 40 |
#define AK8975_REG_ST1 0x02 |
| 41 |
#define AK8975_REG_ST1_DRDY_SHIFT 0 |
| 42 |
#define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT) |
| 43 |
|
| 44 |
#define AK8975_REG_HXL 0x03 |
| 45 |
#define AK8975_REG_HXH 0x04 |
| 46 |
#define AK8975_REG_HYL 0x05 |
| 47 |
#define AK8975_REG_HYH 0x06 |
| 48 |
#define AK8975_REG_HZL 0x07 |
| 49 |
#define AK8975_REG_HZH 0x08 |
| 50 |
#define AK8975_REG_ST2 0x09 |
| 51 |
#define AK8975_REG_ST2_DERR_SHIFT 2 |
| 52 |
#define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT) |
| 53 |
|
| 54 |
#define AK8975_REG_ST2_HOFL_SHIFT 3 |
| 55 |
#define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT) |
| 56 |
|
| 57 |
#define AK8975_REG_CNTL 0x0A |
| 58 |
#define AK8975_REG_CNTL_MODE_SHIFT 0 |
| 59 |
#define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT) |
| 60 |
#define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00 |
| 61 |
#define AK8975_REG_CNTL_MODE_ONCE 0x01 |
| 62 |
#define AK8975_REG_CNTL_MODE_SELF_TEST 0x08 |
| 63 |
#define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F |
| 64 |
|
| 65 |
#define AK8975_REG_RSVC 0x0B |
| 66 |
#define AK8975_REG_ASTC 0x0C |
| 67 |
#define AK8975_REG_TS1 0x0D |
| 68 |
#define AK8975_REG_TS2 0x0E |
| 69 |
#define AK8975_REG_I2CDIS 0x0F |
| 70 |
#define AK8975_REG_ASAX 0x10 |
| 71 |
#define AK8975_REG_ASAY 0x11 |
| 72 |
#define AK8975_REG_ASAZ 0x12 |
| 73 |
|
| 74 |
#define AK8975_MAX_REGS AK8975_REG_ASAZ |
| 75 |
|
| 76 |
/* |
| 77 |
* AK09912 Register definitions |
| 78 |
*/ |
| 79 |
#define AK09912_REG_WIA1 0x00 |
| 80 |
#define AK09912_REG_WIA2 0x01 |
| 81 |
#define AK09918_DEVICE_ID 0x0C |
| 82 |
#define AK09916_DEVICE_ID 0x09 |
| 83 |
#define AK09912_DEVICE_ID 0x04 |
| 84 |
#define AK09911_DEVICE_ID 0x05 |
| 85 |
|
| 86 |
#define AK09911_REG_INFO1 0x02 |
| 87 |
#define AK09911_REG_INFO2 0x03 |
| 88 |
|
| 89 |
#define AK09912_REG_ST1 0x10 |
| 90 |
|
| 91 |
#define AK09912_REG_ST1_DRDY_SHIFT 0 |
| 92 |
#define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT) |
| 93 |
|
| 94 |
#define AK09912_REG_HXL 0x11 |
| 95 |
#define AK09912_REG_HXH 0x12 |
| 96 |
#define AK09912_REG_HYL 0x13 |
| 97 |
#define AK09912_REG_HYH 0x14 |
| 98 |
#define AK09912_REG_HZL 0x15 |
| 99 |
#define AK09912_REG_HZH 0x16 |
| 100 |
#define AK09912_REG_TMPS 0x17 |
| 101 |
|
| 102 |
#define AK09912_REG_ST2 0x18 |
| 103 |
#define AK09912_REG_ST2_HOFL_SHIFT 3 |
| 104 |
#define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT) |
| 105 |
|
| 106 |
#define AK09912_REG_CNTL1 0x30 |
| 107 |
|
| 108 |
#define AK09912_REG_CNTL2 0x31 |
| 109 |
#define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00 |
| 110 |
#define AK09912_REG_CNTL_MODE_ONCE 0x01 |
| 111 |
#define AK09912_REG_CNTL_MODE_SELF_TEST 0x10 |
| 112 |
#define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F |
| 113 |
#define AK09912_REG_CNTL2_MODE_SHIFT 0 |
| 114 |
#define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT) |
| 115 |
|
| 116 |
#define AK09912_REG_CNTL3 0x32 |
| 117 |
|
| 118 |
#define AK09912_REG_TS1 0x33 |
| 119 |
#define AK09912_REG_TS2 0x34 |
| 120 |
#define AK09912_REG_TS3 0x35 |
| 121 |
#define AK09912_REG_I2CDIS 0x36 |
| 122 |
#define AK09912_REG_TS4 0x37 |
| 123 |
|
| 124 |
#define AK09912_REG_ASAX 0x60 |
| 125 |
#define AK09912_REG_ASAY 0x61 |
| 126 |
#define AK09912_REG_ASAZ 0x62 |
| 127 |
|
| 128 |
#define AK09912_MAX_REGS AK09912_REG_ASAZ |
| 129 |
|
| 130 |
/* |
| 131 |
* Miscellaneous values. |
| 132 |
*/ |
| 133 |
#define AK8975_MAX_CONVERSION_TIMEOUT 500 |
| 134 |
#define AK8975_CONVERSION_DONE_POLL_TIME 10 |
| 135 |
#define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000) |
| 136 |
|
| 137 |
/* |
| 138 |
* Precalculate scale factor (in Gauss units) for each axis and |
| 139 |
* store in the device data. |
| 140 |
* |
| 141 |
* This scale factor is axis-dependent, and is derived from 3 calibration |
| 142 |
* factors ASA(x), ASA(y), and ASA(z). |
| 143 |
* |
| 144 |
* These ASA values are read from the sensor device at start of day, and |
| 145 |
* cached in the device context struct. |
| 146 |
* |
| 147 |
* Adjusting the flux value with the sensitivity adjustment value should be |
| 148 |
* done via the following formula: |
| 149 |
* |
| 150 |
* Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 ) |
| 151 |
* where H is the raw value, ASA is the sensitivity adjustment, and Hadj |
| 152 |
* is the resultant adjusted value. |
| 153 |
* |
| 154 |
* We reduce the formula to: |
| 155 |
* |
| 156 |
* Hadj = H * (ASA + 128) / 256 |
| 157 |
* |
| 158 |
* H is in the range of -4096 to 4095. The magnetometer has a range of |
| 159 |
* +-1229uT. To go from the raw value to uT is: |
| 160 |
* |
| 161 |
* HuT = H * 1229/4096, or roughly, 3/10. |
| 162 |
* |
| 163 |
* Since 1uT = 0.01 gauss, our final scale factor becomes: |
| 164 |
* |
| 165 |
* Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100 |
| 166 |
* Hadj = H * ((ASA + 128) * 0.003) / 256 |
| 167 |
* |
| 168 |
* Since ASA doesn't change, we cache the resultant scale factor into the |
| 169 |
* device context in ak8975_setup(). |
| 170 |
* |
| 171 |
* Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we |
| 172 |
* multiply the stored scale value by 1e6. |
| 173 |
*/ |
| 174 |
static long ak8975_raw_to_gauss(u16 data) |
| 175 |
{ |
| 176 |
return (((long)data + 128) * 3000) / 256; |
| 177 |
} |
| 178 |
|
| 179 |
/* |
| 180 |
* For AK8963 and AK09911, same calculation, but the device is less sensitive: |
| 181 |
* |
| 182 |
* H is in the range of +-8190. The magnetometer has a range of |
| 183 |
* +-4912uT. To go from the raw value to uT is: |
| 184 |
* |
| 185 |
* HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10. |
| 186 |
*/ |
| 187 |
|
| 188 |
static long ak8963_09911_raw_to_gauss(u16 data) |
| 189 |
{ |
| 190 |
return (((long)data + 128) * 6000) / 256; |
| 191 |
} |
| 192 |
|
| 193 |
/* |
| 194 |
* For AK09912, same calculation, except the device is more sensitive: |
| 195 |
* |
| 196 |
* H is in the range of -32752 to 32752. The magnetometer has a range of |
| 197 |
* +-4912uT. To go from the raw value to uT is: |
| 198 |
* |
| 199 |
* HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10. |
| 200 |
*/ |
| 201 |
static long ak09912_raw_to_gauss(u16 data) |
| 202 |
{ |
| 203 |
return (((long)data + 128) * 1500) / 256; |
| 204 |
} |
| 205 |
|
| 206 |
/* Compatible Asahi Kasei Compass parts */ |
| 207 |
enum asahi_compass_chipset { |
| 208 |
AK8975, |
| 209 |
AK8963, |
| 210 |
AK09911, |
| 211 |
AK09912, |
| 212 |
AK09916, |
| 213 |
AK09918, |
| 214 |
}; |
| 215 |
|
| 216 |
enum ak_ctrl_reg_addr { |
| 217 |
ST1, |
| 218 |
ST2, |
| 219 |
CNTL, |
| 220 |
ASA_BASE, |
| 221 |
MAX_REGS, |
| 222 |
REGS_END, |
| 223 |
}; |
| 224 |
|
| 225 |
enum ak_ctrl_reg_mask { |
| 226 |
ST1_DRDY, |
| 227 |
ST2_HOFL, |
| 228 |
ST2_DERR, |
| 229 |
CNTL_MODE, |
| 230 |
MASK_END, |
| 231 |
}; |
| 232 |
|
| 233 |
enum ak_ctrl_mode { |
| 234 |
POWER_DOWN, |
| 235 |
MODE_ONCE, |
| 236 |
SELF_TEST, |
| 237 |
FUSE_ROM, |
| 238 |
MODE_END, |
| 239 |
}; |
| 240 |
|
| 241 |
struct ak_def { |
| 242 |
enum asahi_compass_chipset type; |
| 243 |
long (*raw_to_gauss)(u16 data); |
| 244 |
u16 range; |
| 245 |
u8 ctrl_regs[REGS_END]; |
| 246 |
u8 ctrl_masks[MASK_END]; |
| 247 |
u8 ctrl_modes[MODE_END]; |
| 248 |
u8 data_regs[3]; |
| 249 |
}; |
| 250 |
|
| 251 |
static const struct ak_def ak_def_array[] = { |
| 252 |
[AK8975] = { |
| 253 |
.type = AK8975, |
| 254 |
.raw_to_gauss = ak8975_raw_to_gauss, |
| 255 |
.range = 4096, |
| 256 |
.ctrl_regs = { |
| 257 |
AK8975_REG_ST1, |
| 258 |
AK8975_REG_ST2, |
| 259 |
AK8975_REG_CNTL, |
| 260 |
AK8975_REG_ASAX, |
| 261 |
AK8975_MAX_REGS}, |
| 262 |
.ctrl_masks = { |
| 263 |
AK8975_REG_ST1_DRDY_MASK, |
| 264 |
AK8975_REG_ST2_HOFL_MASK, |
| 265 |
AK8975_REG_ST2_DERR_MASK, |
| 266 |
AK8975_REG_CNTL_MODE_MASK}, |
| 267 |
.ctrl_modes = { |
| 268 |
AK8975_REG_CNTL_MODE_POWER_DOWN, |
| 269 |
AK8975_REG_CNTL_MODE_ONCE, |
| 270 |
AK8975_REG_CNTL_MODE_SELF_TEST, |
| 271 |
AK8975_REG_CNTL_MODE_FUSE_ROM}, |
| 272 |
.data_regs = { |
| 273 |
AK8975_REG_HXL, |
| 274 |
AK8975_REG_HYL, |
| 275 |
AK8975_REG_HZL}, |
| 276 |
}, |
| 277 |
[AK8963] = { |
| 278 |
.type = AK8963, |
| 279 |
.raw_to_gauss = ak8963_09911_raw_to_gauss, |
| 280 |
.range = 8190, |
| 281 |
.ctrl_regs = { |
| 282 |
AK8975_REG_ST1, |
| 283 |
AK8975_REG_ST2, |
| 284 |
AK8975_REG_CNTL, |
| 285 |
AK8975_REG_ASAX, |
| 286 |
AK8975_MAX_REGS}, |
| 287 |
.ctrl_masks = { |
| 288 |
AK8975_REG_ST1_DRDY_MASK, |
| 289 |
AK8975_REG_ST2_HOFL_MASK, |
| 290 |
0, |
| 291 |
AK8975_REG_CNTL_MODE_MASK}, |
| 292 |
.ctrl_modes = { |
| 293 |
AK8975_REG_CNTL_MODE_POWER_DOWN, |
| 294 |
AK8975_REG_CNTL_MODE_ONCE, |
| 295 |
AK8975_REG_CNTL_MODE_SELF_TEST, |
| 296 |
AK8975_REG_CNTL_MODE_FUSE_ROM}, |
| 297 |
.data_regs = { |
| 298 |
AK8975_REG_HXL, |
| 299 |
AK8975_REG_HYL, |
| 300 |
AK8975_REG_HZL}, |
| 301 |
}, |
| 302 |
[AK09911] = { |
| 303 |
.type = AK09911, |
| 304 |
.raw_to_gauss = ak8963_09911_raw_to_gauss, |
| 305 |
.range = 8192, |
| 306 |
.ctrl_regs = { |
| 307 |
AK09912_REG_ST1, |
| 308 |
AK09912_REG_ST2, |
| 309 |
AK09912_REG_CNTL2, |
| 310 |
AK09912_REG_ASAX, |
| 311 |
AK09912_MAX_REGS}, |
| 312 |
.ctrl_masks = { |
| 313 |
AK09912_REG_ST1_DRDY_MASK, |
| 314 |
AK09912_REG_ST2_HOFL_MASK, |
| 315 |
0, |
| 316 |
AK09912_REG_CNTL2_MODE_MASK}, |
| 317 |
.ctrl_modes = { |
| 318 |
AK09912_REG_CNTL_MODE_POWER_DOWN, |
| 319 |
AK09912_REG_CNTL_MODE_ONCE, |
| 320 |
AK09912_REG_CNTL_MODE_SELF_TEST, |
| 321 |
AK09912_REG_CNTL_MODE_FUSE_ROM}, |
| 322 |
.data_regs = { |
| 323 |
AK09912_REG_HXL, |
| 324 |
AK09912_REG_HYL, |
| 325 |
AK09912_REG_HZL}, |
| 326 |
}, |
| 327 |
[AK09912] = { |
| 328 |
.type = AK09912, |
| 329 |
.raw_to_gauss = ak09912_raw_to_gauss, |
| 330 |
.range = 32752, |
| 331 |
.ctrl_regs = { |
| 332 |
AK09912_REG_ST1, |
| 333 |
AK09912_REG_ST2, |
| 334 |
AK09912_REG_CNTL2, |
| 335 |
AK09912_REG_ASAX, |
| 336 |
AK09912_MAX_REGS}, |
| 337 |
.ctrl_masks = { |
| 338 |
AK09912_REG_ST1_DRDY_MASK, |
| 339 |
AK09912_REG_ST2_HOFL_MASK, |
| 340 |
0, |
| 341 |
AK09912_REG_CNTL2_MODE_MASK}, |
| 342 |
.ctrl_modes = { |
| 343 |
AK09912_REG_CNTL_MODE_POWER_DOWN, |
| 344 |
AK09912_REG_CNTL_MODE_ONCE, |
| 345 |
AK09912_REG_CNTL_MODE_SELF_TEST, |
| 346 |
AK09912_REG_CNTL_MODE_FUSE_ROM}, |
| 347 |
.data_regs = { |
| 348 |
AK09912_REG_HXL, |
| 349 |
AK09912_REG_HYL, |
| 350 |
AK09912_REG_HZL}, |
| 351 |
}, |
| 352 |
[AK09916] = { |
| 353 |
.type = AK09916, |
| 354 |
.raw_to_gauss = ak09912_raw_to_gauss, |
| 355 |
.range = 32752, |
| 356 |
.ctrl_regs = { |
| 357 |
AK09912_REG_ST1, |
| 358 |
AK09912_REG_ST2, |
| 359 |
AK09912_REG_CNTL2, |
| 360 |
AK09912_REG_ASAX, |
| 361 |
AK09912_MAX_REGS}, |
| 362 |
.ctrl_masks = { |
| 363 |
AK09912_REG_ST1_DRDY_MASK, |
| 364 |
AK09912_REG_ST2_HOFL_MASK, |
| 365 |
0, |
| 366 |
AK09912_REG_CNTL2_MODE_MASK}, |
| 367 |
.ctrl_modes = { |
| 368 |
AK09912_REG_CNTL_MODE_POWER_DOWN, |
| 369 |
AK09912_REG_CNTL_MODE_ONCE, |
| 370 |
AK09912_REG_CNTL_MODE_SELF_TEST, |
| 371 |
AK09912_REG_CNTL_MODE_FUSE_ROM}, |
| 372 |
.data_regs = { |
| 373 |
AK09912_REG_HXL, |
| 374 |
AK09912_REG_HYL, |
| 375 |
AK09912_REG_HZL}, |
| 376 |
}, |
| 377 |
[AK09918] = { |
| 378 |
/* ak09918 is register compatible with ak09912 this is for avoid |
| 379 |
* unknown id messages. |
| 380 |
*/ |
| 381 |
.type = AK09918, |
| 382 |
.raw_to_gauss = ak09912_raw_to_gauss, |
| 383 |
.range = 32752, |
| 384 |
.ctrl_regs = { |
| 385 |
AK09912_REG_ST1, |
| 386 |
AK09912_REG_ST2, |
| 387 |
AK09912_REG_CNTL2, |
| 388 |
AK09912_REG_ASAX, |
| 389 |
AK09912_MAX_REGS}, |
| 390 |
.ctrl_masks = { |
| 391 |
AK09912_REG_ST1_DRDY_MASK, |
| 392 |
AK09912_REG_ST2_HOFL_MASK, |
| 393 |
0, |
| 394 |
AK09912_REG_CNTL2_MODE_MASK}, |
| 395 |
.ctrl_modes = { |
| 396 |
AK09912_REG_CNTL_MODE_POWER_DOWN, |
| 397 |
AK09912_REG_CNTL_MODE_ONCE, |
| 398 |
AK09912_REG_CNTL_MODE_SELF_TEST, |
| 399 |
AK09912_REG_CNTL_MODE_FUSE_ROM}, |
| 400 |
.data_regs = { |
| 401 |
AK09912_REG_HXL, |
| 402 |
AK09912_REG_HYL, |
| 403 |
AK09912_REG_HZL}, |
| 404 |
} |
| 405 |
}; |
| 406 |
|
| 407 |
/* |
| 408 |
* Per-instance context data for the device. |
| 409 |
*/ |
| 410 |
struct ak8975_data { |
| 411 |
struct i2c_client *client; |
| 412 |
const struct ak_def *def; |
| 413 |
struct mutex lock; |
| 414 |
u8 asa[3]; |
| 415 |
long raw_to_gauss[3]; |
| 416 |
struct gpio_desc *eoc_gpiod; |
| 417 |
struct gpio_desc *reset_gpiod; |
| 418 |
int eoc_irq; |
| 419 |
wait_queue_head_t data_ready_queue; |
| 420 |
unsigned long flags; |
| 421 |
u8 cntl_cache; |
| 422 |
struct iio_mount_matrix orientation; |
| 423 |
struct regulator *vdd; |
| 424 |
struct regulator *vid; |
| 425 |
|
| 426 |
/* Ensure natural alignment of timestamp */ |
| 427 |
struct { |
| 428 |
s16 channels[3]; |
| 429 |
aligned_s64 ts; |
| 430 |
} scan; |
| 431 |
}; |
| 432 |
|
| 433 |
/* Enable attached power regulator if any. */ |
| 434 |
static int ak8975_power_on(const struct ak8975_data *data) |
| 435 |
{ |
| 436 |
int ret; |
| 437 |
|
| 438 |
ret = regulator_enable(data->vdd); |
| 439 |
if (ret) { |
| 440 |
dev_warn(&data->client->dev, |
| 441 |
"Failed to enable specified Vdd supply\n"); |
| 442 |
return ret; |
| 443 |
} |
| 444 |
ret = regulator_enable(data->vid); |
| 445 |
if (ret) { |
| 446 |
dev_warn(&data->client->dev, |
| 447 |
"Failed to enable specified Vid supply\n"); |
| 448 |
regulator_disable(data->vdd); |
| 449 |
return ret; |
| 450 |
} |
| 451 |
|
| 452 |
gpiod_set_value_cansleep(data->reset_gpiod, 0); |
| 453 |
|
| 454 |
/* |
| 455 |
* According to the datasheet the power supply rise time is 200us |
| 456 |
* and the minimum wait time before mode setting is 100us, in |
| 457 |
* total 300us. Add some margin and say minimum 500us here. |
| 458 |
*/ |
| 459 |
usleep_range(500, 1000); |
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
/* Disable attached power regulator if any. */ |
| 464 |
static void ak8975_power_off(const struct ak8975_data *data) |
| 465 |
{ |
| 466 |
gpiod_set_value_cansleep(data->reset_gpiod, 1); |
| 467 |
|
| 468 |
regulator_disable(data->vid); |
| 469 |
regulator_disable(data->vdd); |
| 470 |
} |
| 471 |
|
| 472 |
/* |
| 473 |
* Return 0 if the i2c device is the one we expect. |
| 474 |
* return a negative error number otherwise |
| 475 |
*/ |
| 476 |
static int ak8975_who_i_am(struct i2c_client *client, |
| 477 |
enum asahi_compass_chipset type) |
| 478 |
{ |
| 479 |
u8 wia_val[2]; |
| 480 |
int ret; |
| 481 |
|
| 482 |
/* |
| 483 |
* Signature for each device: |
| 484 |
* Device | WIA1 | WIA2 |
| 485 |
* AK09918 | DEVICE_ID_| AK09918_DEVICE_ID |
| 486 |
* AK09916 | DEVICE_ID_| AK09916_DEVICE_ID |
| 487 |
* AK09912 | DEVICE_ID | AK09912_DEVICE_ID |
| 488 |
* AK09911 | DEVICE_ID | AK09911_DEVICE_ID |
| 489 |
* AK8975 | DEVICE_ID | NA |
| 490 |
* AK8963 | DEVICE_ID | NA |
| 491 |
*/ |
| 492 |
ret = i2c_smbus_read_i2c_block_data_or_emulated( |
| 493 |
client, AK09912_REG_WIA1, 2, wia_val); |
| 494 |
if (ret < 0) { |
| 495 |
dev_err(&client->dev, "Error reading WIA\n"); |
| 496 |
return ret; |
| 497 |
} |
| 498 |
|
| 499 |
if (wia_val[0] != AK8975_DEVICE_ID) |
| 500 |
return -ENODEV; |
| 501 |
|
| 502 |
switch (type) { |
| 503 |
case AK8975: |
| 504 |
case AK8963: |
| 505 |
return 0; |
| 506 |
case AK09911: |
| 507 |
if (wia_val[1] == AK09911_DEVICE_ID) |
| 508 |
return 0; |
| 509 |
break; |
| 510 |
case AK09912: |
| 511 |
if (wia_val[1] == AK09912_DEVICE_ID) |
| 512 |
return 0; |
| 513 |
break; |
| 514 |
case AK09916: |
| 515 |
if (wia_val[1] == AK09916_DEVICE_ID) |
| 516 |
return 0; |
| 517 |
break; |
| 518 |
case AK09918: |
| 519 |
if (wia_val[1] == AK09918_DEVICE_ID) |
| 520 |
return 0; |
| 521 |
break; |
| 522 |
} |
| 523 |
|
| 524 |
dev_info(&client->dev, "Device ID %x is unknown.\n", wia_val[1]); |
| 525 |
/* |
| 526 |
* Let driver to probe on unknown id for support more register |
| 527 |
* compatible variants. |
| 528 |
*/ |
| 529 |
return 0; |
| 530 |
} |
| 531 |
|
| 532 |
/* |
| 533 |
* Helper function to write to CNTL register. |
| 534 |
*/ |
| 535 |
static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode) |
| 536 |
{ |
| 537 |
u8 regval; |
| 538 |
int ret; |
| 539 |
|
| 540 |
regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) | |
| 541 |
data->def->ctrl_modes[mode]; |
| 542 |
ret = i2c_smbus_write_byte_data(data->client, |
| 543 |
data->def->ctrl_regs[CNTL], regval); |
| 544 |
if (ret < 0) { |
| 545 |
return ret; |
| 546 |
} |
| 547 |
data->cntl_cache = regval; |
| 548 |
/* After mode change wait atleast 100us */ |
| 549 |
usleep_range(100, 500); |
| 550 |
|
| 551 |
return 0; |
| 552 |
} |
| 553 |
|
| 554 |
/* |
| 555 |
* Handle data ready irq |
| 556 |
*/ |
| 557 |
static irqreturn_t ak8975_irq_handler(int irq, void *data) |
| 558 |
{ |
| 559 |
struct ak8975_data *ak8975 = data; |
| 560 |
|
| 561 |
set_bit(0, &ak8975->flags); |
| 562 |
wake_up(&ak8975->data_ready_queue); |
| 563 |
|
| 564 |
return IRQ_HANDLED; |
| 565 |
} |
| 566 |
|
| 567 |
/* |
| 568 |
* Install data ready interrupt handler |
| 569 |
*/ |
| 570 |
static int ak8975_setup_irq(struct ak8975_data *data) |
| 571 |
{ |
| 572 |
struct i2c_client *client = data->client; |
| 573 |
int rc; |
| 574 |
int irq; |
| 575 |
|
| 576 |
init_waitqueue_head(&data->data_ready_queue); |
| 577 |
clear_bit(0, &data->flags); |
| 578 |
if (client->irq) |
| 579 |
irq = client->irq; |
| 580 |
else |
| 581 |
irq = gpiod_to_irq(data->eoc_gpiod); |
| 582 |
|
| 583 |
rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler, |
| 584 |
IRQF_TRIGGER_RISING, |
| 585 |
dev_name(&client->dev), data); |
| 586 |
if (rc < 0) { |
| 587 |
dev_err(&client->dev, "irq %d request failed: %d\n", irq, rc); |
| 588 |
return rc; |
| 589 |
} |
| 590 |
|
| 591 |
data->eoc_irq = irq; |
| 592 |
|
| 593 |
return rc; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
/* |
| 598 |
* Perform some start-of-day setup, including reading the asa calibration |
| 599 |
* values and caching them. |
| 600 |
*/ |
| 601 |
static int ak8975_setup(struct i2c_client *client) |
| 602 |
{ |
| 603 |
struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 604 |
struct ak8975_data *data = iio_priv(indio_dev); |
| 605 |
int ret; |
| 606 |
|
| 607 |
/* Write the fused rom access mode. */ |
| 608 |
ret = ak8975_set_mode(data, FUSE_ROM); |
| 609 |
if (ret < 0) { |
| 610 |
dev_err(&client->dev, "Error in setting fuse access mode\n"); |
| 611 |
return ret; |
| 612 |
} |
| 613 |
|
| 614 |
/* Get asa data and store in the device data. */ |
| 615 |
ret = i2c_smbus_read_i2c_block_data_or_emulated( |
| 616 |
client, data->def->ctrl_regs[ASA_BASE], |
| 617 |
3, data->asa); |
| 618 |
if (ret < 0) { |
| 619 |
dev_err(&client->dev, "Not able to read asa data\n"); |
| 620 |
return ret; |
| 621 |
} |
| 622 |
|
| 623 |
/* After reading fuse ROM data set power-down mode */ |
| 624 |
ret = ak8975_set_mode(data, POWER_DOWN); |
| 625 |
if (ret < 0) { |
| 626 |
dev_err(&client->dev, "Error in setting power-down mode\n"); |
| 627 |
return ret; |
| 628 |
} |
| 629 |
|
| 630 |
if (data->eoc_gpiod || client->irq > 0) { |
| 631 |
ret = ak8975_setup_irq(data); |
| 632 |
if (ret < 0) { |
| 633 |
dev_err(&client->dev, |
| 634 |
"Error setting data ready interrupt\n"); |
| 635 |
return ret; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]); |
| 640 |
data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]); |
| 641 |
data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]); |
| 642 |
|
| 643 |
return 0; |
| 644 |
} |
| 645 |
|
| 646 |
static int wait_conversion_complete_gpio(struct ak8975_data *data) |
| 647 |
{ |
| 648 |
struct i2c_client *client = data->client; |
| 649 |
u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; |
| 650 |
int ret; |
| 651 |
|
| 652 |
/* Wait for the conversion to complete. */ |
| 653 |
while (timeout_ms) { |
| 654 |
msleep(AK8975_CONVERSION_DONE_POLL_TIME); |
| 655 |
if (gpiod_get_value(data->eoc_gpiod)) |
| 656 |
break; |
| 657 |
timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; |
| 658 |
} |
| 659 |
if (!timeout_ms) { |
| 660 |
dev_err(&client->dev, "Conversion timeout happened\n"); |
| 661 |
return -EINVAL; |
| 662 |
} |
| 663 |
|
| 664 |
ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]); |
| 665 |
if (ret < 0) |
| 666 |
dev_err(&client->dev, "Error in reading ST1\n"); |
| 667 |
|
| 668 |
return ret; |
| 669 |
} |
| 670 |
|
| 671 |
static int wait_conversion_complete_polled(struct ak8975_data *data) |
| 672 |
{ |
| 673 |
struct i2c_client *client = data->client; |
| 674 |
u8 read_status; |
| 675 |
u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; |
| 676 |
int ret; |
| 677 |
|
| 678 |
/* Wait for the conversion to complete. */ |
| 679 |
while (timeout_ms) { |
| 680 |
msleep(AK8975_CONVERSION_DONE_POLL_TIME); |
| 681 |
ret = i2c_smbus_read_byte_data(client, |
| 682 |
data->def->ctrl_regs[ST1]); |
| 683 |
if (ret < 0) { |
| 684 |
dev_err(&client->dev, "Error in reading ST1\n"); |
| 685 |
return ret; |
| 686 |
} |
| 687 |
read_status = ret; |
| 688 |
if (read_status) |
| 689 |
break; |
| 690 |
timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; |
| 691 |
} |
| 692 |
if (!timeout_ms) { |
| 693 |
dev_err(&client->dev, "Conversion timeout happened\n"); |
| 694 |
return -EINVAL; |
| 695 |
} |
| 696 |
|
| 697 |
return read_status; |
| 698 |
} |
| 699 |
|
| 700 |
/* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */ |
| 701 |
static int wait_conversion_complete_interrupt(struct ak8975_data *data) |
| 702 |
{ |
| 703 |
int ret; |
| 704 |
|
| 705 |
ret = wait_event_timeout(data->data_ready_queue, |
| 706 |
test_bit(0, &data->flags), |
| 707 |
AK8975_DATA_READY_TIMEOUT); |
| 708 |
clear_bit(0, &data->flags); |
| 709 |
|
| 710 |
return ret > 0 ? 0 : -ETIME; |
| 711 |
} |
| 712 |
|
| 713 |
static int ak8975_start_read_axis(struct ak8975_data *data, |
| 714 |
const struct i2c_client *client) |
| 715 |
{ |
| 716 |
/* Set up the device for taking a sample. */ |
| 717 |
int ret = ak8975_set_mode(data, MODE_ONCE); |
| 718 |
|
| 719 |
if (ret < 0) { |
| 720 |
dev_err(&client->dev, "Error in setting operating mode\n"); |
| 721 |
return ret; |
| 722 |
} |
| 723 |
|
| 724 |
/* Wait for the conversion to complete. */ |
| 725 |
if (data->eoc_irq) |
| 726 |
ret = wait_conversion_complete_interrupt(data); |
| 727 |
else if (data->eoc_gpiod) |
| 728 |
ret = wait_conversion_complete_gpio(data); |
| 729 |
else |
| 730 |
ret = wait_conversion_complete_polled(data); |
| 731 |
if (ret < 0) |
| 732 |
return ret; |
| 733 |
|
| 734 |
/* Return with zero if the data is ready. */ |
| 735 |
return !data->def->ctrl_regs[ST1_DRDY]; |
| 736 |
} |
| 737 |
|
| 738 |
/* Retrieve raw flux value for one of the x, y, or z axis. */ |
| 739 |
static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) |
| 740 |
{ |
| 741 |
struct ak8975_data *data = iio_priv(indio_dev); |
| 742 |
const struct i2c_client *client = data->client; |
| 743 |
const struct ak_def *def = data->def; |
| 744 |
__le16 rval; |
| 745 |
u16 buff; |
| 746 |
int ret; |
| 747 |
|
| 748 |
pm_runtime_get_sync(&data->client->dev); |
| 749 |
|
| 750 |
mutex_lock(&data->lock); |
| 751 |
|
| 752 |
ret = ak8975_start_read_axis(data, client); |
| 753 |
if (ret) |
| 754 |
goto exit; |
| 755 |
|
| 756 |
ret = i2c_smbus_read_i2c_block_data_or_emulated( |
| 757 |
client, def->data_regs[index], |
| 758 |
sizeof(rval), (u8*)&rval); |
| 759 |
if (ret < 0) |
| 760 |
goto exit; |
| 761 |
|
| 762 |
/* Read out ST2 for release lock on measurment data. */ |
| 763 |
ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST2]); |
| 764 |
if (ret < 0) { |
| 765 |
dev_err(&client->dev, "Error in reading ST2\n"); |
| 766 |
goto exit; |
| 767 |
} |
| 768 |
|
| 769 |
if (ret & (data->def->ctrl_masks[ST2_DERR] | |
| 770 |
data->def->ctrl_masks[ST2_HOFL])) { |
| 771 |
dev_err(&client->dev, "ST2 status error 0x%x\n", ret); |
| 772 |
ret = -EINVAL; |
| 773 |
goto exit; |
| 774 |
} |
| 775 |
|
| 776 |
mutex_unlock(&data->lock); |
| 777 |
|
| 778 |
pm_runtime_put_autosuspend(&data->client->dev); |
| 779 |
|
| 780 |
/* Swap bytes and convert to valid range. */ |
| 781 |
buff = le16_to_cpu(rval); |
| 782 |
*val = clamp_t(s16, buff, -def->range, def->range); |
| 783 |
return IIO_VAL_INT; |
| 784 |
|
| 785 |
exit: |
| 786 |
mutex_unlock(&data->lock); |
| 787 |
dev_err(&client->dev, "Error in reading axis\n"); |
| 788 |
return ret; |
| 789 |
} |
| 790 |
|
| 791 |
static int ak8975_read_raw(struct iio_dev *indio_dev, |
| 792 |
struct iio_chan_spec const *chan, |
| 793 |
int *val, int *val2, |
| 794 |
long mask) |
| 795 |
{ |
| 796 |
struct ak8975_data *data = iio_priv(indio_dev); |
| 797 |
|
| 798 |
switch (mask) { |
| 799 |
case IIO_CHAN_INFO_RAW: |
| 800 |
return ak8975_read_axis(indio_dev, chan->address, val); |
| 801 |
case IIO_CHAN_INFO_SCALE: |
| 802 |
*val = 0; |
| 803 |
*val2 = data->raw_to_gauss[chan->address]; |
| 804 |
return IIO_VAL_INT_PLUS_MICRO; |
| 805 |
} |
| 806 |
return -EINVAL; |
| 807 |
} |
| 808 |
|
| 809 |
static const struct iio_mount_matrix * |
| 810 |
ak8975_get_mount_matrix(const struct iio_dev *indio_dev, |
| 811 |
const struct iio_chan_spec *chan) |
| 812 |
{ |
| 813 |
struct ak8975_data *data = iio_priv(indio_dev); |
| 814 |
|
| 815 |
return &data->orientation; |
| 816 |
} |
| 817 |
|
| 818 |
static const struct iio_chan_spec_ext_info ak8975_ext_info[] = { |
| 819 |
IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix), |
| 820 |
{ } |
| 821 |
}; |
| 822 |
|
| 823 |
#define AK8975_CHANNEL(axis, index) \ |
| 824 |
{ \ |
| 825 |
.type = IIO_MAGN, \ |
| 826 |
.modified = 1, \ |
| 827 |
.channel2 = IIO_MOD_##axis, \ |
| 828 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 829 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 830 |
.address = index, \ |
| 831 |
.scan_index = index, \ |
| 832 |
.scan_type = { \ |
| 833 |
.sign = 's', \ |
| 834 |
.realbits = 16, \ |
| 835 |
.storagebits = 16, \ |
| 836 |
.endianness = IIO_CPU \ |
| 837 |
}, \ |
| 838 |
.ext_info = ak8975_ext_info, \ |
| 839 |
} |
| 840 |
|
| 841 |
static const struct iio_chan_spec ak8975_channels[] = { |
| 842 |
AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), |
| 843 |
IIO_CHAN_SOFT_TIMESTAMP(3), |
| 844 |
}; |
| 845 |
|
| 846 |
static const unsigned long ak8975_scan_masks[] = { 0x7, 0 }; |
| 847 |
|
| 848 |
static const struct iio_info ak8975_info = { |
| 849 |
.read_raw = &ak8975_read_raw, |
| 850 |
}; |
| 851 |
|
| 852 |
static void ak8975_fill_buffer(struct iio_dev *indio_dev) |
| 853 |
{ |
| 854 |
struct ak8975_data *data = iio_priv(indio_dev); |
| 855 |
const struct i2c_client *client = data->client; |
| 856 |
const struct ak_def *def = data->def; |
| 857 |
int ret; |
| 858 |
__le16 fval[3]; |
| 859 |
|
| 860 |
mutex_lock(&data->lock); |
| 861 |
|
| 862 |
ret = ak8975_start_read_axis(data, client); |
| 863 |
if (ret) |
| 864 |
goto unlock; |
| 865 |
|
| 866 |
/* |
| 867 |
* For each axis, read the flux value from the appropriate register |
| 868 |
* (the register is specified in the iio device attributes). |
| 869 |
*/ |
| 870 |
ret = i2c_smbus_read_i2c_block_data_or_emulated(client, |
| 871 |
def->data_regs[0], |
| 872 |
3 * sizeof(fval[0]), |
| 873 |
(u8 *)fval); |
| 874 |
if (ret < 0) |
| 875 |
goto unlock; |
| 876 |
|
| 877 |
mutex_unlock(&data->lock); |
| 878 |
|
| 879 |
/* Clamp to valid range. */ |
| 880 |
data->scan.channels[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range); |
| 881 |
data->scan.channels[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range); |
| 882 |
data->scan.channels[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range); |
| 883 |
|
| 884 |
iio_push_to_buffers_with_ts(indio_dev, &data->scan, sizeof(data->scan), |
| 885 |
iio_get_time_ns(indio_dev)); |
| 886 |
|
| 887 |
return; |
| 888 |
|
| 889 |
unlock: |
| 890 |
mutex_unlock(&data->lock); |
| 891 |
dev_err(&client->dev, "Error in reading axes block\n"); |
| 892 |
} |
| 893 |
|
| 894 |
static irqreturn_t ak8975_handle_trigger(int irq, void *p) |
| 895 |
{ |
| 896 |
const struct iio_poll_func *pf = p; |
| 897 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 898 |
|
| 899 |
ak8975_fill_buffer(indio_dev); |
| 900 |
iio_trigger_notify_done(indio_dev->trig); |
| 901 |
return IRQ_HANDLED; |
| 902 |
} |
| 903 |
|
| 904 |
static int ak8975_probe(struct i2c_client *client) |
| 905 |
{ |
| 906 |
const struct i2c_device_id *id = i2c_client_get_device_id(client); |
| 907 |
struct ak8975_data *data; |
| 908 |
struct iio_dev *indio_dev; |
| 909 |
struct gpio_desc *eoc_gpiod; |
| 910 |
struct gpio_desc *reset_gpiod; |
| 911 |
int err; |
| 912 |
const char *name = NULL; |
| 913 |
|
| 914 |
/* |
| 915 |
* Grab and set up the supplied GPIO. |
| 916 |
* We may not have a GPIO based IRQ to scan, that is fine, we will |
| 917 |
* poll if so. |
| 918 |
*/ |
| 919 |
eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN); |
| 920 |
if (IS_ERR(eoc_gpiod)) |
| 921 |
return PTR_ERR(eoc_gpiod); |
| 922 |
if (eoc_gpiod) |
| 923 |
gpiod_set_consumer_name(eoc_gpiod, "ak_8975"); |
| 924 |
|
| 925 |
/* |
| 926 |
* According to AK09911 datasheet, if reset GPIO is provided then |
| 927 |
* deassert reset on ak8975_power_on() and assert reset on |
| 928 |
* ak8975_power_off(). |
| 929 |
*/ |
| 930 |
reset_gpiod = devm_gpiod_get_optional(&client->dev, |
| 931 |
"reset", GPIOD_OUT_HIGH); |
| 932 |
if (IS_ERR(reset_gpiod)) |
| 933 |
return PTR_ERR(reset_gpiod); |
| 934 |
|
| 935 |
/* Register with IIO */ |
| 936 |
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 937 |
if (indio_dev == NULL) |
| 938 |
return -ENOMEM; |
| 939 |
|
| 940 |
data = iio_priv(indio_dev); |
| 941 |
i2c_set_clientdata(client, indio_dev); |
| 942 |
|
| 943 |
data->client = client; |
| 944 |
data->eoc_gpiod = eoc_gpiod; |
| 945 |
data->reset_gpiod = reset_gpiod; |
| 946 |
data->eoc_irq = 0; |
| 947 |
|
| 948 |
err = iio_read_mount_matrix(&client->dev, &data->orientation); |
| 949 |
if (err) |
| 950 |
return err; |
| 951 |
|
| 952 |
/* id will be NULL when enumerated via ACPI */ |
| 953 |
data->def = i2c_get_match_data(client); |
| 954 |
if (!data->def) |
| 955 |
return -ENODEV; |
| 956 |
|
| 957 |
/* If enumerated via firmware node, fix the ABI */ |
| 958 |
if (dev_fwnode(&client->dev)) |
| 959 |
name = dev_name(&client->dev); |
| 960 |
else |
| 961 |
name = id->name; |
| 962 |
|
| 963 |
/* Fetch the regulators */ |
| 964 |
data->vdd = devm_regulator_get(&client->dev, "vdd"); |
| 965 |
if (IS_ERR(data->vdd)) |
| 966 |
return PTR_ERR(data->vdd); |
| 967 |
data->vid = devm_regulator_get(&client->dev, "vid"); |
| 968 |
if (IS_ERR(data->vid)) |
| 969 |
return PTR_ERR(data->vid); |
| 970 |
|
| 971 |
err = ak8975_power_on(data); |
| 972 |
if (err) |
| 973 |
return err; |
| 974 |
|
| 975 |
err = ak8975_who_i_am(client, data->def->type); |
| 976 |
if (err < 0) { |
| 977 |
dev_err(&client->dev, "Unexpected device\n"); |
| 978 |
goto power_off; |
| 979 |
} |
| 980 |
dev_dbg(&client->dev, "Asahi compass chip %s\n", name); |
| 981 |
|
| 982 |
/* Perform some basic start-of-day setup of the device. */ |
| 983 |
err = ak8975_setup(client); |
| 984 |
if (err < 0) { |
| 985 |
dev_err(&client->dev, "%s initialization fails\n", name); |
| 986 |
goto power_off; |
| 987 |
} |
| 988 |
|
| 989 |
mutex_init(&data->lock); |
| 990 |
indio_dev->channels = ak8975_channels; |
| 991 |
indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); |
| 992 |
indio_dev->info = &ak8975_info; |
| 993 |
indio_dev->available_scan_masks = ak8975_scan_masks; |
| 994 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 995 |
indio_dev->name = name; |
| 996 |
|
| 997 |
err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger, |
| 998 |
NULL); |
| 999 |
if (err) { |
| 1000 |
dev_err(&client->dev, "triggered buffer setup failed\n"); |
| 1001 |
goto power_off; |
| 1002 |
} |
| 1003 |
|
| 1004 |
err = iio_device_register(indio_dev); |
| 1005 |
if (err) { |
| 1006 |
dev_err(&client->dev, "device register failed\n"); |
| 1007 |
goto cleanup_buffer; |
| 1008 |
} |
| 1009 |
|
| 1010 |
/* Enable runtime PM */ |
| 1011 |
pm_runtime_get_noresume(&client->dev); |
| 1012 |
pm_runtime_set_active(&client->dev); |
| 1013 |
pm_runtime_enable(&client->dev); |
| 1014 |
/* |
| 1015 |
* The device comes online in 500us, so add two orders of magnitude |
| 1016 |
* of delay before autosuspending: 50 ms. |
| 1017 |
*/ |
| 1018 |
pm_runtime_set_autosuspend_delay(&client->dev, 50); |
| 1019 |
pm_runtime_use_autosuspend(&client->dev); |
| 1020 |
pm_runtime_put(&client->dev); |
| 1021 |
|
| 1022 |
return 0; |
| 1023 |
|
| 1024 |
cleanup_buffer: |
| 1025 |
iio_triggered_buffer_cleanup(indio_dev); |
| 1026 |
power_off: |
| 1027 |
ak8975_power_off(data); |
| 1028 |
return err; |
| 1029 |
} |
| 1030 |
|
| 1031 |
static void ak8975_remove(struct i2c_client *client) |
| 1032 |
{ |
| 1033 |
struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 1034 |
struct ak8975_data *data = iio_priv(indio_dev); |
| 1035 |
|
| 1036 |
pm_runtime_get_sync(&client->dev); |
| 1037 |
pm_runtime_put_noidle(&client->dev); |
| 1038 |
pm_runtime_disable(&client->dev); |
| 1039 |
iio_device_unregister(indio_dev); |
| 1040 |
iio_triggered_buffer_cleanup(indio_dev); |
| 1041 |
ak8975_set_mode(data, POWER_DOWN); |
| 1042 |
ak8975_power_off(data); |
| 1043 |
} |
| 1044 |
|
| 1045 |
static int ak8975_runtime_suspend(struct device *dev) |
| 1046 |
{ |
| 1047 |
struct i2c_client *client = to_i2c_client(dev); |
| 1048 |
struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 1049 |
struct ak8975_data *data = iio_priv(indio_dev); |
| 1050 |
int ret; |
| 1051 |
|
| 1052 |
/* Set the device in power down if it wasn't already */ |
| 1053 |
ret = ak8975_set_mode(data, POWER_DOWN); |
| 1054 |
if (ret < 0) { |
| 1055 |
dev_err(&client->dev, "Error in setting power-down mode\n"); |
| 1056 |
return ret; |
| 1057 |
} |
| 1058 |
/* Next cut the regulators */ |
| 1059 |
ak8975_power_off(data); |
| 1060 |
|
| 1061 |
return 0; |
| 1062 |
} |
| 1063 |
|
| 1064 |
static int ak8975_runtime_resume(struct device *dev) |
| 1065 |
{ |
| 1066 |
struct i2c_client *client = to_i2c_client(dev); |
| 1067 |
struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 1068 |
struct ak8975_data *data = iio_priv(indio_dev); |
| 1069 |
int ret; |
| 1070 |
|
| 1071 |
/* Take up the regulators */ |
| 1072 |
ak8975_power_on(data); |
| 1073 |
/* |
| 1074 |
* We come up in powered down mode, the reading routines will |
| 1075 |
* put us in the mode to read values later. |
| 1076 |
*/ |
| 1077 |
ret = ak8975_set_mode(data, POWER_DOWN); |
| 1078 |
if (ret < 0) { |
| 1079 |
dev_err(&client->dev, "Error in setting power-down mode\n"); |
| 1080 |
return ret; |
| 1081 |
} |
| 1082 |
|
| 1083 |
return 0; |
| 1084 |
} |
| 1085 |
|
| 1086 |
static DEFINE_RUNTIME_DEV_PM_OPS(ak8975_dev_pm_ops, ak8975_runtime_suspend, |
| 1087 |
ak8975_runtime_resume, NULL); |
| 1088 |
|
| 1089 |
static const struct acpi_device_id ak_acpi_match[] = { |
| 1090 |
{"AK8963", (kernel_ulong_t)&ak_def_array[AK8963] }, |
| 1091 |
{"AK8975", (kernel_ulong_t)&ak_def_array[AK8975] }, |
| 1092 |
{"AK009911", (kernel_ulong_t)&ak_def_array[AK09911] }, |
| 1093 |
{"AK09911", (kernel_ulong_t)&ak_def_array[AK09911] }, |
| 1094 |
{"AK09912", (kernel_ulong_t)&ak_def_array[AK09912] }, |
| 1095 |
{"AKM9911", (kernel_ulong_t)&ak_def_array[AK09911] }, |
| 1096 |
{"INVN6500", (kernel_ulong_t)&ak_def_array[AK8963] }, |
| 1097 |
{ } |
| 1098 |
}; |
| 1099 |
MODULE_DEVICE_TABLE(acpi, ak_acpi_match); |
| 1100 |
|
| 1101 |
static const struct i2c_device_id ak8975_id[] = { |
| 1102 |
{"AK8963", (kernel_ulong_t)&ak_def_array[AK8963] }, |
| 1103 |
{"ak8963", (kernel_ulong_t)&ak_def_array[AK8963] }, |
| 1104 |
{"ak8975", (kernel_ulong_t)&ak_def_array[AK8975] }, |
| 1105 |
{"ak09911", (kernel_ulong_t)&ak_def_array[AK09911] }, |
| 1106 |
{"ak09912", (kernel_ulong_t)&ak_def_array[AK09912] }, |
| 1107 |
{"ak09916", (kernel_ulong_t)&ak_def_array[AK09916] }, |
| 1108 |
{"ak09918", (kernel_ulong_t)&ak_def_array[AK09918] }, |
| 1109 |
{ } |
| 1110 |
}; |
| 1111 |
MODULE_DEVICE_TABLE(i2c, ak8975_id); |
| 1112 |
|
| 1113 |
static const struct of_device_id ak8975_of_match[] = { |
| 1114 |
{ .compatible = "asahi-kasei,ak8975", .data = &ak_def_array[AK8975] }, |
| 1115 |
{ .compatible = "ak8975", .data = &ak_def_array[AK8975] }, |
| 1116 |
{ .compatible = "asahi-kasei,ak8963", .data = &ak_def_array[AK8963] }, |
| 1117 |
{ .compatible = "ak8963", .data = &ak_def_array[AK8963] }, |
| 1118 |
{ .compatible = "asahi-kasei,ak09911", .data = &ak_def_array[AK09911] }, |
| 1119 |
{ .compatible = "ak09911", .data = &ak_def_array[AK09911] }, |
| 1120 |
{ .compatible = "asahi-kasei,ak09912", .data = &ak_def_array[AK09912] }, |
| 1121 |
{ .compatible = "ak09912", .data = &ak_def_array[AK09912] }, |
| 1122 |
{ .compatible = "asahi-kasei,ak09916", .data = &ak_def_array[AK09916] }, |
| 1123 |
{ .compatible = "asahi-kasei,ak09918", .data = &ak_def_array[AK09918] }, |
| 1124 |
{ } |
| 1125 |
}; |
| 1126 |
MODULE_DEVICE_TABLE(of, ak8975_of_match); |
| 1127 |
|
| 1128 |
static struct i2c_driver ak8975_driver = { |
| 1129 |
.driver = { |
| 1130 |
.name = "ak8975", |
| 1131 |
.pm = pm_ptr(&ak8975_dev_pm_ops), |
| 1132 |
.of_match_table = ak8975_of_match, |
| 1133 |
.acpi_match_table = ak_acpi_match, |
| 1134 |
}, |
| 1135 |
.probe = ak8975_probe, |
| 1136 |
.remove = ak8975_remove, |
| 1137 |
.id_table = ak8975_id, |
| 1138 |
}; |
| 1139 |
module_i2c_driver(ak8975_driver); |
| 1140 |
|
| 1141 |
MODULE_AUTHOR("Laxman Dewangan <[email protected]>"); |
| 1142 |
MODULE_DESCRIPTION("AK8975 magnetometer driver"); |
| 1143 |
MODULE_LICENSE("GPL"); |