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> |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 34 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 35 | #include <linux/iio/iio.h> |
| 36 | #include <linux/iio/sysfs.h> |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 37 | /* |
| 38 | * Register definitions, as well as various shifts and masks to get at the |
| 39 | * individual fields of the registers. |
| 40 | */ |
| 41 | #define AK8975_REG_WIA 0x00 |
| 42 | #define AK8975_DEVICE_ID 0x48 |
| 43 | |
| 44 | #define AK8975_REG_INFO 0x01 |
| 45 | |
| 46 | #define AK8975_REG_ST1 0x02 |
| 47 | #define AK8975_REG_ST1_DRDY_SHIFT 0 |
| 48 | #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT) |
| 49 | |
| 50 | #define AK8975_REG_HXL 0x03 |
| 51 | #define AK8975_REG_HXH 0x04 |
| 52 | #define AK8975_REG_HYL 0x05 |
| 53 | #define AK8975_REG_HYH 0x06 |
| 54 | #define AK8975_REG_HZL 0x07 |
| 55 | #define AK8975_REG_HZH 0x08 |
| 56 | #define AK8975_REG_ST2 0x09 |
| 57 | #define AK8975_REG_ST2_DERR_SHIFT 2 |
| 58 | #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT) |
| 59 | |
| 60 | #define AK8975_REG_ST2_HOFL_SHIFT 3 |
| 61 | #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT) |
| 62 | |
| 63 | #define AK8975_REG_CNTL 0x0A |
| 64 | #define AK8975_REG_CNTL_MODE_SHIFT 0 |
| 65 | #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT) |
| 66 | #define AK8975_REG_CNTL_MODE_POWER_DOWN 0 |
| 67 | #define AK8975_REG_CNTL_MODE_ONCE 1 |
| 68 | #define AK8975_REG_CNTL_MODE_SELF_TEST 8 |
| 69 | #define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF |
| 70 | |
| 71 | #define AK8975_REG_RSVC 0x0B |
| 72 | #define AK8975_REG_ASTC 0x0C |
| 73 | #define AK8975_REG_TS1 0x0D |
| 74 | #define AK8975_REG_TS2 0x0E |
| 75 | #define AK8975_REG_I2CDIS 0x0F |
| 76 | #define AK8975_REG_ASAX 0x10 |
| 77 | #define AK8975_REG_ASAY 0x11 |
| 78 | #define AK8975_REG_ASAZ 0x12 |
| 79 | |
| 80 | #define AK8975_MAX_REGS AK8975_REG_ASAZ |
| 81 | |
| 82 | /* |
| 83 | * Miscellaneous values. |
| 84 | */ |
| 85 | #define AK8975_MAX_CONVERSION_TIMEOUT 500 |
| 86 | #define AK8975_CONVERSION_DONE_POLL_TIME 10 |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 87 | #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000) |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * Per-instance context data for the device. |
| 91 | */ |
| 92 | struct ak8975_data { |
| 93 | struct i2c_client *client; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 94 | struct attribute_group attrs; |
| 95 | struct mutex lock; |
| 96 | u8 asa[3]; |
| 97 | long raw_to_gauss[3]; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 98 | u8 reg_cache[AK8975_MAX_REGS]; |
| 99 | int eoc_gpio; |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 100 | int eoc_irq; |
| 101 | wait_queue_head_t data_ready_queue; |
| 102 | unsigned long flags; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 105 | static const int ak8975_index_to_reg[] = { |
| 106 | AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL, |
| 107 | }; |
| 108 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 109 | /* |
| 110 | * Helper function to write to the I2C device's registers. |
| 111 | */ |
| 112 | static int ak8975_write_data(struct i2c_client *client, |
| 113 | u8 reg, u8 val, u8 mask, u8 shift) |
| 114 | { |
Preetham Chandru | 40f32d9 | 2012-04-09 18:05:01 +0530 | [diff] [blame] | 115 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 116 | struct ak8975_data *data = iio_priv(indio_dev); |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 117 | u8 regval; |
| 118 | int ret; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 119 | |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 120 | regval = (data->reg_cache[reg] & ~mask) | (val << shift); |
| 121 | ret = i2c_smbus_write_byte_data(client, reg, regval); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 122 | if (ret < 0) { |
| 123 | dev_err(&client->dev, "Write to device fails status %x\n", ret); |
| 124 | return ret; |
| 125 | } |
| 126 | data->reg_cache[reg] = regval; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 132 | * Handle data ready irq |
| 133 | */ |
| 134 | static irqreturn_t ak8975_irq_handler(int irq, void *data) |
| 135 | { |
| 136 | struct ak8975_data *ak8975 = data; |
| 137 | |
| 138 | set_bit(0, &ak8975->flags); |
| 139 | wake_up(&ak8975->data_ready_queue); |
| 140 | |
| 141 | return IRQ_HANDLED; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Install data ready interrupt handler |
| 146 | */ |
| 147 | static int ak8975_setup_irq(struct ak8975_data *data) |
| 148 | { |
| 149 | struct i2c_client *client = data->client; |
| 150 | int rc; |
| 151 | int irq; |
| 152 | |
| 153 | if (client->irq) |
| 154 | irq = client->irq; |
| 155 | else |
| 156 | irq = gpio_to_irq(data->eoc_gpio); |
| 157 | |
| 158 | rc = request_irq(irq, ak8975_irq_handler, |
| 159 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 160 | dev_name(&client->dev), data); |
| 161 | if (rc < 0) { |
| 162 | dev_err(&client->dev, |
| 163 | "irq %d request failed, (gpio %d): %d\n", |
| 164 | irq, data->eoc_gpio, rc); |
| 165 | return rc; |
| 166 | } |
| 167 | |
| 168 | init_waitqueue_head(&data->data_ready_queue); |
| 169 | clear_bit(0, &data->flags); |
| 170 | data->eoc_irq = irq; |
| 171 | |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 177 | * Perform some start-of-day setup, including reading the asa calibration |
| 178 | * values and caching them. |
| 179 | */ |
| 180 | static int ak8975_setup(struct i2c_client *client) |
| 181 | { |
Preetham Chandru | 40f32d9 | 2012-04-09 18:05:01 +0530 | [diff] [blame] | 182 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 183 | struct ak8975_data *data = iio_priv(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 184 | u8 device_id; |
| 185 | int ret; |
| 186 | |
| 187 | /* Confirm that the device we're talking to is really an AK8975. */ |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 188 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 189 | if (ret < 0) { |
| 190 | dev_err(&client->dev, "Error reading WIA\n"); |
| 191 | return ret; |
| 192 | } |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 193 | device_id = ret; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 194 | if (device_id != AK8975_DEVICE_ID) { |
| 195 | dev_err(&client->dev, "Device ak8975 not found\n"); |
| 196 | return -ENODEV; |
| 197 | } |
| 198 | |
| 199 | /* Write the fused rom access mode. */ |
| 200 | ret = ak8975_write_data(client, |
| 201 | AK8975_REG_CNTL, |
| 202 | AK8975_REG_CNTL_MODE_FUSE_ROM, |
| 203 | AK8975_REG_CNTL_MODE_MASK, |
| 204 | AK8975_REG_CNTL_MODE_SHIFT); |
| 205 | if (ret < 0) { |
| 206 | dev_err(&client->dev, "Error in setting fuse access mode\n"); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | /* Get asa data and store in the device data. */ |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 211 | ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX, |
| 212 | 3, data->asa); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 213 | if (ret < 0) { |
| 214 | dev_err(&client->dev, "Not able to read asa data\n"); |
| 215 | return ret; |
| 216 | } |
| 217 | |
Leed Aguilar | 040f3e5 | 2012-06-06 16:14:56 -0400 | [diff] [blame] | 218 | /* After reading fuse ROM data set power-down mode */ |
| 219 | ret = ak8975_write_data(client, |
| 220 | AK8975_REG_CNTL, |
| 221 | AK8975_REG_CNTL_MODE_POWER_DOWN, |
| 222 | AK8975_REG_CNTL_MODE_MASK, |
| 223 | AK8975_REG_CNTL_MODE_SHIFT); |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 224 | |
| 225 | if (data->eoc_gpio > 0 || client->irq) { |
| 226 | ret = ak8975_setup_irq(data); |
| 227 | if (ret < 0) { |
| 228 | dev_err(&client->dev, |
| 229 | "Error setting data ready interrupt\n"); |
| 230 | return ret; |
| 231 | } |
| 232 | } |
| 233 | |
Leed Aguilar | 040f3e5 | 2012-06-06 16:14:56 -0400 | [diff] [blame] | 234 | if (ret < 0) { |
| 235 | dev_err(&client->dev, "Error in setting power-down mode\n"); |
| 236 | return ret; |
| 237 | } |
| 238 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 239 | /* |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 240 | * Precalculate scale factor (in Gauss units) for each axis and |
| 241 | * store in the device data. |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 242 | * |
| 243 | * This scale factor is axis-dependent, and is derived from 3 calibration |
| 244 | * factors ASA(x), ASA(y), and ASA(z). |
| 245 | * |
| 246 | * These ASA values are read from the sensor device at start of day, and |
| 247 | * cached in the device context struct. |
| 248 | * |
| 249 | * Adjusting the flux value with the sensitivity adjustment value should be |
| 250 | * done via the following formula: |
| 251 | * |
| 252 | * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 ) |
| 253 | * |
| 254 | * where H is the raw value, ASA is the sensitivity adjustment, and Hadj |
| 255 | * is the resultant adjusted value. |
| 256 | * |
| 257 | * We reduce the formula to: |
| 258 | * |
| 259 | * Hadj = H * (ASA + 128) / 256 |
| 260 | * |
| 261 | * H is in the range of -4096 to 4095. The magnetometer has a range of |
| 262 | * +-1229uT. To go from the raw value to uT is: |
| 263 | * |
| 264 | * HuT = H * 1229/4096, or roughly, 3/10. |
| 265 | * |
| 266 | * Since 1uT = 100 gauss, our final scale factor becomes: |
| 267 | * |
| 268 | * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100 |
| 269 | * Hadj = H * ((ASA + 128) * 30 / 256 |
| 270 | * |
| 271 | * Since ASA doesn't change, we cache the resultant scale factor into the |
| 272 | * device context in ak8975_setup(). |
| 273 | */ |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 274 | data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8; |
| 275 | data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8; |
| 276 | data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8; |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 281 | static int wait_conversion_complete_gpio(struct ak8975_data *data) |
| 282 | { |
| 283 | struct i2c_client *client = data->client; |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 284 | u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; |
| 285 | int ret; |
| 286 | |
| 287 | /* Wait for the conversion to complete. */ |
| 288 | while (timeout_ms) { |
| 289 | msleep(AK8975_CONVERSION_DONE_POLL_TIME); |
| 290 | if (gpio_get_value(data->eoc_gpio)) |
| 291 | break; |
| 292 | timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; |
| 293 | } |
| 294 | if (!timeout_ms) { |
| 295 | dev_err(&client->dev, "Conversion timeout happened\n"); |
| 296 | return -EINVAL; |
| 297 | } |
| 298 | |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 299 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1); |
| 300 | if (ret < 0) |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 301 | dev_err(&client->dev, "Error in reading ST1\n"); |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 302 | |
| 303 | return ret; |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static int wait_conversion_complete_polled(struct ak8975_data *data) |
| 307 | { |
| 308 | struct i2c_client *client = data->client; |
| 309 | u8 read_status; |
| 310 | u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; |
| 311 | int ret; |
| 312 | |
| 313 | /* Wait for the conversion to complete. */ |
| 314 | while (timeout_ms) { |
| 315 | msleep(AK8975_CONVERSION_DONE_POLL_TIME); |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 316 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1); |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 317 | if (ret < 0) { |
| 318 | dev_err(&client->dev, "Error in reading ST1\n"); |
| 319 | return ret; |
| 320 | } |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 321 | read_status = ret; |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 322 | if (read_status) |
| 323 | break; |
| 324 | timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; |
| 325 | } |
| 326 | if (!timeout_ms) { |
| 327 | dev_err(&client->dev, "Conversion timeout happened\n"); |
| 328 | return -EINVAL; |
| 329 | } |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 330 | |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 331 | return read_status; |
| 332 | } |
| 333 | |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 334 | /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */ |
| 335 | static int wait_conversion_complete_interrupt(struct ak8975_data *data) |
| 336 | { |
| 337 | int ret; |
| 338 | |
| 339 | ret = wait_event_timeout(data->data_ready_queue, |
| 340 | test_bit(0, &data->flags), |
| 341 | AK8975_DATA_READY_TIMEOUT); |
| 342 | clear_bit(0, &data->flags); |
| 343 | |
| 344 | return ret > 0 ? 0 : -ETIME; |
| 345 | } |
| 346 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 347 | /* |
| 348 | * Emits the raw flux value for the x, y, or z axis. |
| 349 | */ |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 350 | 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] | 351 | { |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 352 | struct ak8975_data *data = iio_priv(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 353 | struct i2c_client *client = data->client; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 354 | u16 meas_reg; |
| 355 | s16 raw; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 356 | int ret; |
| 357 | |
| 358 | mutex_lock(&data->lock); |
| 359 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 360 | /* Set up the device for taking a sample. */ |
| 361 | ret = ak8975_write_data(client, |
| 362 | AK8975_REG_CNTL, |
| 363 | AK8975_REG_CNTL_MODE_ONCE, |
| 364 | AK8975_REG_CNTL_MODE_MASK, |
| 365 | AK8975_REG_CNTL_MODE_SHIFT); |
| 366 | if (ret < 0) { |
| 367 | dev_err(&client->dev, "Error in setting operating mode\n"); |
| 368 | goto exit; |
| 369 | } |
| 370 | |
| 371 | /* Wait for the conversion to complete. */ |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 372 | if (data->eoc_irq) |
| 373 | ret = wait_conversion_complete_interrupt(data); |
| 374 | else if (gpio_is_valid(data->eoc_gpio)) |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 375 | ret = wait_conversion_complete_gpio(data); |
| 376 | else |
| 377 | ret = wait_conversion_complete_polled(data); |
| 378 | if (ret < 0) |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 379 | goto exit; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 380 | |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 381 | /* This will be executed only for non-interrupt based waiting case */ |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 382 | if (ret & AK8975_REG_ST1_DRDY_MASK) { |
| 383 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 384 | if (ret < 0) { |
| 385 | dev_err(&client->dev, "Error in reading ST2\n"); |
| 386 | goto exit; |
| 387 | } |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 388 | if (ret & (AK8975_REG_ST2_DERR_MASK | |
| 389 | AK8975_REG_ST2_HOFL_MASK)) { |
| 390 | dev_err(&client->dev, "ST2 status error 0x%x\n", ret); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 391 | ret = -EINVAL; |
| 392 | goto exit; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /* Read the flux value from the appropriate register |
| 397 | (the register is specified in the iio device attributes). */ |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 398 | ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 399 | if (ret < 0) { |
| 400 | dev_err(&client->dev, "Read axis data fails\n"); |
| 401 | goto exit; |
| 402 | } |
Jonathan Cameron | c411f60 | 2013-03-24 16:58:00 +0000 | [diff] [blame] | 403 | meas_reg = ret; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 404 | |
| 405 | mutex_unlock(&data->lock); |
| 406 | |
| 407 | /* Endian conversion of the measured values. */ |
| 408 | raw = (s16) (le16_to_cpu(meas_reg)); |
| 409 | |
| 410 | /* Clamp to valid range. */ |
| 411 | raw = clamp_t(s16, raw, -4096, 4095); |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 412 | *val = raw; |
| 413 | return IIO_VAL_INT; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 414 | |
| 415 | exit: |
| 416 | mutex_unlock(&data->lock); |
| 417 | return ret; |
| 418 | } |
| 419 | |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 420 | static int ak8975_read_raw(struct iio_dev *indio_dev, |
| 421 | struct iio_chan_spec const *chan, |
| 422 | int *val, int *val2, |
| 423 | long mask) |
| 424 | { |
| 425 | struct ak8975_data *data = iio_priv(indio_dev); |
| 426 | |
| 427 | switch (mask) { |
Jonathan Cameron | 4d9948b | 2012-04-15 17:41:23 +0100 | [diff] [blame] | 428 | case IIO_CHAN_INFO_RAW: |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 429 | return ak8975_read_axis(indio_dev, chan->address, val); |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 430 | case IIO_CHAN_INFO_SCALE: |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 431 | *val = data->raw_to_gauss[chan->address]; |
| 432 | return IIO_VAL_INT; |
| 433 | } |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
| 437 | #define AK8975_CHANNEL(axis, index) \ |
| 438 | { \ |
| 439 | .type = IIO_MAGN, \ |
| 440 | .modified = 1, \ |
| 441 | .channel2 = IIO_MOD_##axis, \ |
Jonathan Cameron | 3a0b442 | 2013-02-27 19:40:48 +0000 | [diff] [blame] | 442 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 443 | BIT(IIO_CHAN_INFO_SCALE), \ |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 444 | .address = index, \ |
| 445 | } |
| 446 | |
| 447 | static const struct iio_chan_spec ak8975_channels[] = { |
| 448 | AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), |
| 449 | }; |
| 450 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 451 | static const struct iio_info ak8975_info = { |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 452 | .read_raw = &ak8975_read_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 453 | .driver_module = THIS_MODULE, |
| 454 | }; |
| 455 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 456 | static int ak8975_probe(struct i2c_client *client, |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 457 | const struct i2c_device_id *id) |
| 458 | { |
| 459 | struct ak8975_data *data; |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 460 | struct iio_dev *indio_dev; |
| 461 | int eoc_gpio; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 462 | int err; |
| 463 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 464 | /* Grab and set up the supplied GPIO. */ |
Jacek Anaszewski | f4b7f75 | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 465 | if (client->dev.platform_data) |
Jonathan Cameron | f6d838d | 2011-09-21 11:15:59 +0100 | [diff] [blame] | 466 | eoc_gpio = *(int *)(client->dev.platform_data); |
Jacek Anaszewski | f4b7f75 | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 467 | else if (client->dev.of_node) |
| 468 | eoc_gpio = of_get_gpio(client->dev.of_node, 0); |
| 469 | else |
| 470 | eoc_gpio = -1; |
| 471 | |
| 472 | if (eoc_gpio == -EPROBE_DEFER) |
| 473 | return -EPROBE_DEFER; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 474 | |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 475 | /* We may not have a GPIO based IRQ to scan, that is fine, we will |
| 476 | poll if so */ |
Stephen Warren | 7c6c936 | 2011-09-21 11:16:00 +0100 | [diff] [blame] | 477 | if (gpio_is_valid(eoc_gpio)) { |
Leed Aguilar | 82f2acd | 2012-06-06 16:15:40 -0400 | [diff] [blame] | 478 | err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975"); |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 479 | if (err < 0) { |
| 480 | dev_err(&client->dev, |
| 481 | "failed to request GPIO %d, error %d\n", |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 482 | eoc_gpio, err); |
| 483 | goto exit; |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 484 | } |
Stephen Warren | 7c6c936 | 2011-09-21 11:16:00 +0100 | [diff] [blame] | 485 | } |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 486 | |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 487 | /* Register with IIO */ |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 488 | indio_dev = iio_device_alloc(sizeof(*data)); |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 489 | if (indio_dev == NULL) { |
| 490 | err = -ENOMEM; |
| 491 | goto exit_gpio; |
| 492 | } |
| 493 | data = iio_priv(indio_dev); |
Preetham Chandru | 40f32d9 | 2012-04-09 18:05:01 +0530 | [diff] [blame] | 494 | i2c_set_clientdata(client, indio_dev); |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 495 | |
| 496 | data->client = client; |
| 497 | data->eoc_gpio = eoc_gpio; |
| 498 | data->eoc_irq = 0; |
| 499 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 500 | /* Perform some basic start-of-day setup of the device. */ |
| 501 | err = ak8975_setup(client); |
| 502 | if (err < 0) { |
| 503 | dev_err(&client->dev, "AK8975 initialization fails\n"); |
Stephen Warren | ad31d25 | 2011-09-21 11:16:01 +0100 | [diff] [blame] | 504 | goto exit_free_iio; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 507 | data->client = client; |
| 508 | mutex_init(&data->lock); |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 509 | data->eoc_gpio = eoc_gpio; |
| 510 | indio_dev->dev.parent = &client->dev; |
Jonathan Cameron | 694e1b5 | 2011-08-12 17:48:03 +0100 | [diff] [blame] | 511 | indio_dev->channels = ak8975_channels; |
| 512 | indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 513 | indio_dev->info = &ak8975_info; |
| 514 | indio_dev->modes = INDIO_DIRECT_MODE; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 515 | |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 516 | err = iio_device_register(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 517 | if (err < 0) |
| 518 | goto exit_free_iio; |
| 519 | |
| 520 | return 0; |
| 521 | |
| 522 | exit_free_iio: |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 523 | iio_device_free(indio_dev); |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 524 | if (data->eoc_irq) |
| 525 | free_irq(data->eoc_irq, data); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 526 | exit_gpio: |
Stephen Warren | 7c6c936 | 2011-09-21 11:16:00 +0100 | [diff] [blame] | 527 | if (gpio_is_valid(eoc_gpio)) |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 528 | gpio_free(eoc_gpio); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 529 | exit: |
| 530 | return err; |
| 531 | } |
| 532 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 533 | static int ak8975_remove(struct i2c_client *client) |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 534 | { |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 535 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 536 | struct ak8975_data *data = iio_priv(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 537 | |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 538 | iio_device_unregister(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 539 | |
Jacek Anaszewski | 94a6d5c | 2013-05-07 11:41:00 +0100 | [diff] [blame] | 540 | if (data->eoc_irq) |
| 541 | free_irq(data->eoc_irq, data); |
| 542 | |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 543 | if (gpio_is_valid(data->eoc_gpio)) |
| 544 | gpio_free(data->eoc_gpio); |
| 545 | |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 546 | iio_device_free(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static const struct i2c_device_id ak8975_id[] = { |
| 552 | {"ak8975", 0}, |
| 553 | {} |
| 554 | }; |
| 555 | |
| 556 | MODULE_DEVICE_TABLE(i2c, ak8975_id); |
| 557 | |
Olof Johansson | 54461c3 | 2011-12-22 18:46:42 -0800 | [diff] [blame] | 558 | static const struct of_device_id ak8975_of_match[] = { |
| 559 | { .compatible = "asahi-kasei,ak8975", }, |
| 560 | { .compatible = "ak8975", }, |
| 561 | { } |
| 562 | }; |
| 563 | MODULE_DEVICE_TABLE(of, ak8975_of_match); |
| 564 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 565 | static struct i2c_driver ak8975_driver = { |
| 566 | .driver = { |
| 567 | .name = "ak8975", |
Olof Johansson | 54461c3 | 2011-12-22 18:46:42 -0800 | [diff] [blame] | 568 | .of_match_table = ak8975_of_match, |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 569 | }, |
| 570 | .probe = ak8975_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 571 | .remove = ak8975_remove, |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 572 | .id_table = ak8975_id, |
| 573 | }; |
Lars-Peter Clausen | 6e5af18 | 2011-11-16 10:13:38 +0100 | [diff] [blame] | 574 | module_i2c_driver(ak8975_driver); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 575 | |
| 576 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); |
| 577 | MODULE_DESCRIPTION("AK8975 magnetometer driver"); |
| 578 | MODULE_LICENSE("GPL"); |