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