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 | |
| 33 | #include "../iio.h" |
| 34 | #include "magnet.h" |
| 35 | |
| 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]; |
| 96 | unsigned long mode; |
| 97 | u8 reg_cache[AK8975_MAX_REGS]; |
| 98 | int eoc_gpio; |
| 99 | int eoc_irq; |
| 100 | }; |
| 101 | |
| 102 | /* |
| 103 | * Helper function to write to the I2C device's registers. |
| 104 | */ |
| 105 | static int ak8975_write_data(struct i2c_client *client, |
| 106 | u8 reg, u8 val, u8 mask, u8 shift) |
| 107 | { |
| 108 | u8 regval; |
| 109 | struct i2c_msg msg; |
| 110 | u8 w_data[2]; |
| 111 | int ret = 0; |
| 112 | |
| 113 | struct ak8975_data *data = i2c_get_clientdata(client); |
| 114 | |
| 115 | regval = data->reg_cache[reg]; |
| 116 | regval &= ~mask; |
| 117 | regval |= val << shift; |
| 118 | |
| 119 | w_data[0] = reg; |
| 120 | w_data[1] = regval; |
| 121 | |
| 122 | msg.addr = client->addr; |
| 123 | msg.flags = 0; |
| 124 | msg.len = 2; |
| 125 | msg.buf = w_data; |
| 126 | |
| 127 | ret = i2c_transfer(client->adapter, &msg, 1); |
| 128 | if (ret < 0) { |
| 129 | dev_err(&client->dev, "Write to device fails status %x\n", ret); |
| 130 | return ret; |
| 131 | } |
| 132 | data->reg_cache[reg] = regval; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Helper function to read a contiguous set of the I2C device's registers. |
| 139 | */ |
| 140 | static int ak8975_read_data(struct i2c_client *client, |
| 141 | u8 reg, u8 length, u8 *buffer) |
| 142 | { |
| 143 | struct i2c_msg msg[2]; |
| 144 | u8 w_data[2]; |
| 145 | int ret; |
| 146 | |
| 147 | w_data[0] = reg; |
| 148 | |
| 149 | msg[0].addr = client->addr; |
| 150 | msg[0].flags = I2C_M_NOSTART; /* set repeated start and write */ |
| 151 | msg[0].len = 1; |
| 152 | msg[0].buf = w_data; |
| 153 | |
| 154 | msg[1].addr = client->addr; |
| 155 | msg[1].flags = I2C_M_RD; |
| 156 | msg[1].len = length; |
| 157 | msg[1].buf = buffer; |
| 158 | |
| 159 | ret = i2c_transfer(client->adapter, msg, 2); |
| 160 | if (ret < 0) { |
| 161 | dev_err(&client->dev, "Read from device fails\n"); |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Perform some start-of-day setup, including reading the asa calibration |
| 170 | * values and caching them. |
| 171 | */ |
| 172 | static int ak8975_setup(struct i2c_client *client) |
| 173 | { |
| 174 | struct ak8975_data *data = i2c_get_clientdata(client); |
| 175 | u8 device_id; |
| 176 | int ret; |
| 177 | |
| 178 | /* Confirm that the device we're talking to is really an AK8975. */ |
| 179 | ret = ak8975_read_data(client, AK8975_REG_WIA, 1, &device_id); |
| 180 | if (ret < 0) { |
| 181 | dev_err(&client->dev, "Error reading WIA\n"); |
| 182 | return ret; |
| 183 | } |
| 184 | if (device_id != AK8975_DEVICE_ID) { |
| 185 | dev_err(&client->dev, "Device ak8975 not found\n"); |
| 186 | return -ENODEV; |
| 187 | } |
| 188 | |
| 189 | /* Write the fused rom access mode. */ |
| 190 | ret = ak8975_write_data(client, |
| 191 | AK8975_REG_CNTL, |
| 192 | AK8975_REG_CNTL_MODE_FUSE_ROM, |
| 193 | AK8975_REG_CNTL_MODE_MASK, |
| 194 | AK8975_REG_CNTL_MODE_SHIFT); |
| 195 | if (ret < 0) { |
| 196 | dev_err(&client->dev, "Error in setting fuse access mode\n"); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | /* Get asa data and store in the device data. */ |
| 201 | ret = ak8975_read_data(client, AK8975_REG_ASAX, 3, data->asa); |
| 202 | if (ret < 0) { |
| 203 | dev_err(&client->dev, "Not able to read asa data\n"); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | /* Precalculate scale factor for each axis and |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 208 | store in the device data. */ |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 209 | data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8; |
| 210 | data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8; |
| 211 | data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Shows the device's mode. 0 = off, 1 = on. |
| 218 | */ |
| 219 | static ssize_t show_mode(struct device *dev, struct device_attribute *devattr, |
| 220 | char *buf) |
| 221 | { |
| 222 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 223 | struct ak8975_data *data = iio_priv(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 224 | |
| 225 | return sprintf(buf, "%lu\n", data->mode); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Sets the device's mode. 0 = off, 1 = on. The device's mode must be on |
| 230 | * for the magn raw attributes to be available. |
| 231 | */ |
| 232 | static ssize_t store_mode(struct device *dev, struct device_attribute *devattr, |
| 233 | const char *buf, size_t count) |
| 234 | { |
| 235 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 236 | struct ak8975_data *data = iio_priv(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 237 | struct i2c_client *client = data->client; |
| 238 | unsigned long oval; |
| 239 | int ret; |
| 240 | |
| 241 | /* Convert mode string and do some basic sanity checking on it. |
| 242 | only 0 or 1 are valid. */ |
| 243 | if (strict_strtoul(buf, 10, &oval)) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | if (oval > 1) { |
| 247 | dev_err(dev, "mode value is not supported\n"); |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | |
| 251 | mutex_lock(&data->lock); |
| 252 | |
| 253 | /* Write the mode to the device. */ |
| 254 | if (data->mode != oval) { |
| 255 | ret = ak8975_write_data(client, |
| 256 | AK8975_REG_CNTL, |
| 257 | (u8)oval, |
| 258 | AK8975_REG_CNTL_MODE_MASK, |
| 259 | AK8975_REG_CNTL_MODE_SHIFT); |
| 260 | |
| 261 | if (ret < 0) { |
| 262 | dev_err(&client->dev, "Error in setting mode\n"); |
| 263 | mutex_unlock(&data->lock); |
| 264 | return ret; |
| 265 | } |
| 266 | data->mode = oval; |
| 267 | } |
| 268 | |
| 269 | mutex_unlock(&data->lock); |
| 270 | |
| 271 | return count; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Emits the scale factor to bring the raw value into Gauss units. |
| 276 | * |
| 277 | * This scale factor is axis-dependent, and is derived from 3 calibration |
| 278 | * factors ASA(x), ASA(y), and ASA(z). |
| 279 | * |
| 280 | * These ASA values are read from the sensor device at start of day, and |
| 281 | * cached in the device context struct. |
| 282 | * |
| 283 | * Adjusting the flux value with the sensitivity adjustment value should be |
| 284 | * done via the following formula: |
| 285 | * |
| 286 | * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 ) |
| 287 | * |
| 288 | * where H is the raw value, ASA is the sensitivity adjustment, and Hadj |
| 289 | * is the resultant adjusted value. |
| 290 | * |
| 291 | * We reduce the formula to: |
| 292 | * |
| 293 | * Hadj = H * (ASA + 128) / 256 |
| 294 | * |
| 295 | * H is in the range of -4096 to 4095. The magnetometer has a range of |
| 296 | * +-1229uT. To go from the raw value to uT is: |
| 297 | * |
| 298 | * HuT = H * 1229/4096, or roughly, 3/10. |
| 299 | * |
| 300 | * Since 1uT = 100 gauss, our final scale factor becomes: |
| 301 | * |
| 302 | * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100 |
| 303 | * Hadj = H * ((ASA + 128) * 30 / 256 |
| 304 | * |
| 305 | * Since ASA doesn't change, we cache the resultant scale factor into the |
| 306 | * device context in ak8975_setup(). |
| 307 | */ |
| 308 | static ssize_t show_scale(struct device *dev, struct device_attribute *devattr, |
| 309 | char *buf) |
| 310 | { |
| 311 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 312 | struct ak8975_data *data = iio_priv(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 313 | struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr); |
| 314 | |
| 315 | return sprintf(buf, "%ld\n", data->raw_to_gauss[this_attr->address]); |
| 316 | } |
| 317 | |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 318 | static int wait_conversion_complete_gpio(struct ak8975_data *data) |
| 319 | { |
| 320 | struct i2c_client *client = data->client; |
| 321 | u8 read_status; |
| 322 | u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; |
| 323 | int ret; |
| 324 | |
| 325 | /* Wait for the conversion to complete. */ |
| 326 | while (timeout_ms) { |
| 327 | msleep(AK8975_CONVERSION_DONE_POLL_TIME); |
| 328 | if (gpio_get_value(data->eoc_gpio)) |
| 329 | break; |
| 330 | timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; |
| 331 | } |
| 332 | if (!timeout_ms) { |
| 333 | dev_err(&client->dev, "Conversion timeout happened\n"); |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | |
| 337 | ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status); |
| 338 | if (ret < 0) { |
| 339 | dev_err(&client->dev, "Error in reading ST1\n"); |
| 340 | return ret; |
| 341 | } |
| 342 | return read_status; |
| 343 | } |
| 344 | |
| 345 | static int wait_conversion_complete_polled(struct ak8975_data *data) |
| 346 | { |
| 347 | struct i2c_client *client = data->client; |
| 348 | u8 read_status; |
| 349 | u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; |
| 350 | int ret; |
| 351 | |
| 352 | /* Wait for the conversion to complete. */ |
| 353 | while (timeout_ms) { |
| 354 | msleep(AK8975_CONVERSION_DONE_POLL_TIME); |
| 355 | ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status); |
| 356 | if (ret < 0) { |
| 357 | dev_err(&client->dev, "Error in reading ST1\n"); |
| 358 | return ret; |
| 359 | } |
| 360 | if (read_status) |
| 361 | break; |
| 362 | timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; |
| 363 | } |
| 364 | if (!timeout_ms) { |
| 365 | dev_err(&client->dev, "Conversion timeout happened\n"); |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | return read_status; |
| 369 | } |
| 370 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 371 | /* |
| 372 | * Emits the raw flux value for the x, y, or z axis. |
| 373 | */ |
| 374 | static ssize_t show_raw(struct device *dev, struct device_attribute *devattr, |
| 375 | char *buf) |
| 376 | { |
| 377 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 378 | struct ak8975_data *data = iio_priv(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 379 | struct i2c_client *client = data->client; |
| 380 | struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 381 | u16 meas_reg; |
| 382 | s16 raw; |
| 383 | u8 read_status; |
| 384 | int ret; |
| 385 | |
| 386 | mutex_lock(&data->lock); |
| 387 | |
| 388 | if (data->mode == 0) { |
| 389 | dev_err(&client->dev, "Operating mode is in power down mode\n"); |
| 390 | ret = -EBUSY; |
| 391 | goto exit; |
| 392 | } |
| 393 | |
| 394 | /* Set up the device for taking a sample. */ |
| 395 | ret = ak8975_write_data(client, |
| 396 | AK8975_REG_CNTL, |
| 397 | AK8975_REG_CNTL_MODE_ONCE, |
| 398 | AK8975_REG_CNTL_MODE_MASK, |
| 399 | AK8975_REG_CNTL_MODE_SHIFT); |
| 400 | if (ret < 0) { |
| 401 | dev_err(&client->dev, "Error in setting operating mode\n"); |
| 402 | goto exit; |
| 403 | } |
| 404 | |
| 405 | /* Wait for the conversion to complete. */ |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 406 | if (data->eoc_gpio) |
| 407 | ret = wait_conversion_complete_gpio(data); |
| 408 | else |
| 409 | ret = wait_conversion_complete_polled(data); |
| 410 | if (ret < 0) |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 411 | goto exit; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 412 | |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 413 | read_status = ret; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 414 | |
| 415 | if (read_status & AK8975_REG_ST1_DRDY_MASK) { |
| 416 | ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status); |
| 417 | if (ret < 0) { |
| 418 | dev_err(&client->dev, "Error in reading ST2\n"); |
| 419 | goto exit; |
| 420 | } |
| 421 | if (read_status & (AK8975_REG_ST2_DERR_MASK | |
| 422 | AK8975_REG_ST2_HOFL_MASK)) { |
| 423 | dev_err(&client->dev, "ST2 status error 0x%x\n", |
| 424 | read_status); |
| 425 | ret = -EINVAL; |
| 426 | goto exit; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /* Read the flux value from the appropriate register |
| 431 | (the register is specified in the iio device attributes). */ |
| 432 | ret = ak8975_read_data(client, this_attr->address, 2, (u8 *)&meas_reg); |
| 433 | if (ret < 0) { |
| 434 | dev_err(&client->dev, "Read axis data fails\n"); |
| 435 | goto exit; |
| 436 | } |
| 437 | |
| 438 | mutex_unlock(&data->lock); |
| 439 | |
| 440 | /* Endian conversion of the measured values. */ |
| 441 | raw = (s16) (le16_to_cpu(meas_reg)); |
| 442 | |
| 443 | /* Clamp to valid range. */ |
| 444 | raw = clamp_t(s16, raw, -4096, 4095); |
| 445 | |
| 446 | return sprintf(buf, "%d\n", raw); |
| 447 | |
| 448 | exit: |
| 449 | mutex_unlock(&data->lock); |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode, store_mode, 0); |
| 454 | static IIO_DEV_ATTR_MAGN_X_SCALE(S_IRUGO, show_scale, NULL, 0); |
| 455 | static IIO_DEV_ATTR_MAGN_Y_SCALE(S_IRUGO, show_scale, NULL, 1); |
| 456 | static IIO_DEV_ATTR_MAGN_Z_SCALE(S_IRUGO, show_scale, NULL, 2); |
| 457 | static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL); |
| 458 | static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL); |
| 459 | static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL); |
| 460 | |
| 461 | static struct attribute *ak8975_attr[] = { |
| 462 | &iio_dev_attr_mode.dev_attr.attr, |
| 463 | &iio_dev_attr_magn_x_scale.dev_attr.attr, |
| 464 | &iio_dev_attr_magn_y_scale.dev_attr.attr, |
| 465 | &iio_dev_attr_magn_z_scale.dev_attr.attr, |
| 466 | &iio_dev_attr_magn_x_raw.dev_attr.attr, |
| 467 | &iio_dev_attr_magn_y_raw.dev_attr.attr, |
| 468 | &iio_dev_attr_magn_z_raw.dev_attr.attr, |
| 469 | NULL |
| 470 | }; |
| 471 | |
| 472 | static struct attribute_group ak8975_attr_group = { |
| 473 | .attrs = ak8975_attr, |
| 474 | }; |
| 475 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 476 | static const struct iio_info ak8975_info = { |
| 477 | .attrs = &ak8975_attr_group, |
| 478 | .driver_module = THIS_MODULE, |
| 479 | }; |
| 480 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 481 | static int ak8975_probe(struct i2c_client *client, |
| 482 | const struct i2c_device_id *id) |
| 483 | { |
| 484 | struct ak8975_data *data; |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 485 | struct iio_dev *indio_dev; |
| 486 | int eoc_gpio; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 487 | int err; |
| 488 | |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 489 | /* Grab and set up the supplied GPIO. */ |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 490 | eoc_gpio = irq_to_gpio(client->irq); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 491 | |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 492 | /* We may not have a GPIO based IRQ to scan, that is fine, we will |
| 493 | poll if so */ |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 494 | if (eoc_gpio > 0) { |
| 495 | err = gpio_request(eoc_gpio, "ak_8975"); |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 496 | if (err < 0) { |
| 497 | dev_err(&client->dev, |
| 498 | "failed to request GPIO %d, error %d\n", |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 499 | eoc_gpio, err); |
| 500 | goto exit; |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 501 | } |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 502 | |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 503 | err = gpio_direction_input(eoc_gpio); |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 504 | if (err < 0) { |
| 505 | dev_err(&client->dev, |
| 506 | "Failed to configure input direction for GPIO %d, error %d\n", |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 507 | eoc_gpio, err); |
Alan Cox | 01fbb47 | 2011-04-06 13:31:40 +0100 | [diff] [blame] | 508 | goto exit_gpio; |
| 509 | } |
| 510 | } else |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 511 | eoc_gpio = 0; /* No GPIO available */ |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 512 | |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 513 | /* Register with IIO */ |
| 514 | indio_dev = iio_allocate_device(sizeof(*data)); |
| 515 | if (indio_dev == NULL) { |
| 516 | err = -ENOMEM; |
| 517 | goto exit_gpio; |
| 518 | } |
| 519 | data = iio_priv(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 520 | /* Perform some basic start-of-day setup of the device. */ |
| 521 | err = ak8975_setup(client); |
| 522 | if (err < 0) { |
| 523 | dev_err(&client->dev, "AK8975 initialization fails\n"); |
| 524 | goto exit_gpio; |
| 525 | } |
| 526 | |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 527 | i2c_set_clientdata(client, indio_dev); |
| 528 | data->client = client; |
| 529 | mutex_init(&data->lock); |
| 530 | data->eoc_irq = client->irq; |
| 531 | data->eoc_gpio = eoc_gpio; |
| 532 | indio_dev->dev.parent = &client->dev; |
| 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: |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 543 | iio_free_device(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 544 | exit_gpio: |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 545 | if (eoc_gpio) |
| 546 | gpio_free(eoc_gpio); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 547 | exit: |
| 548 | return err; |
| 549 | } |
| 550 | |
| 551 | static int ak8975_remove(struct i2c_client *client) |
| 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); |
| 555 | int eoc_gpio = data->eoc_gpio; |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 556 | |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 557 | iio_device_unregister(indio_dev); |
| 558 | iio_free_device(indio_dev); |
Andrew Chew | 3285aae | 2010-09-08 22:02:17 -0700 | [diff] [blame] | 559 | |
Jonathan Cameron | 338473c | 2011-06-27 13:07:54 +0100 | [diff] [blame] | 560 | if (eoc_gpio) |
| 561 | gpio_free(eoc_gpio); |
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 | |
| 573 | static struct i2c_driver ak8975_driver = { |
| 574 | .driver = { |
| 575 | .name = "ak8975", |
| 576 | }, |
| 577 | .probe = ak8975_probe, |
| 578 | .remove = __devexit_p(ak8975_remove), |
| 579 | .id_table = ak8975_id, |
| 580 | }; |
| 581 | |
| 582 | static int __init ak8975_init(void) |
| 583 | { |
| 584 | return i2c_add_driver(&ak8975_driver); |
| 585 | } |
| 586 | |
| 587 | static void __exit ak8975_exit(void) |
| 588 | { |
| 589 | i2c_del_driver(&ak8975_driver); |
| 590 | } |
| 591 | |
| 592 | module_init(ak8975_init); |
| 593 | module_exit(ak8975_exit); |
| 594 | |
| 595 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); |
| 596 | MODULE_DESCRIPTION("AK8975 magnetometer driver"); |
| 597 | MODULE_LICENSE("GPL"); |