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