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