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