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