| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* MMC35240 - MEMSIC 3-axis Magnetic Sensor |
| 4 |
* |
| 5 |
* Copyright (c) 2015, Intel Corporation. |
| 6 |
* |
| 7 |
* IIO driver for MMC35240 (7-bit I2C slave address 0x30). |
| 8 |
* |
| 9 |
* TODO: offset, ACPI, continuous measurement mode, PM |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/mod_devicetable.h> |
| 14 |
#include <linux/init.h> |
| 15 |
#include <linux/i2c.h> |
| 16 |
#include <linux/delay.h> |
| 17 |
#include <linux/regmap.h> |
| 18 |
#include <linux/pm.h> |
| 19 |
|
| 20 |
#include <linux/iio/iio.h> |
| 21 |
#include <linux/iio/sysfs.h> |
| 22 |
|
| 23 |
#define MMC35240_DRV_NAME "mmc35240" |
| 24 |
|
| 25 |
#define MMC35240_REG_XOUT_L 0x00 |
| 26 |
#define MMC35240_REG_XOUT_H 0x01 |
| 27 |
#define MMC35240_REG_YOUT_L 0x02 |
| 28 |
#define MMC35240_REG_YOUT_H 0x03 |
| 29 |
#define MMC35240_REG_ZOUT_L 0x04 |
| 30 |
#define MMC35240_REG_ZOUT_H 0x05 |
| 31 |
|
| 32 |
#define MMC35240_REG_STATUS 0x06 |
| 33 |
#define MMC35240_REG_CTRL0 0x07 |
| 34 |
#define MMC35240_REG_CTRL1 0x08 |
| 35 |
|
| 36 |
#define MMC35240_REG_ID 0x20 |
| 37 |
|
| 38 |
#define MMC35240_STATUS_MEAS_DONE_BIT BIT(0) |
| 39 |
|
| 40 |
#define MMC35240_CTRL0_REFILL_BIT BIT(7) |
| 41 |
#define MMC35240_CTRL0_RESET_BIT BIT(6) |
| 42 |
#define MMC35240_CTRL0_SET_BIT BIT(5) |
| 43 |
#define MMC35240_CTRL0_CMM_BIT BIT(1) |
| 44 |
#define MMC35240_CTRL0_TM_BIT BIT(0) |
| 45 |
|
| 46 |
/* output resolution bits */ |
| 47 |
#define MMC35240_CTRL1_BW0_BIT BIT(0) |
| 48 |
#define MMC35240_CTRL1_BW1_BIT BIT(1) |
| 49 |
|
| 50 |
#define MMC35240_CTRL1_BW_MASK (MMC35240_CTRL1_BW0_BIT | \ |
| 51 |
MMC35240_CTRL1_BW1_BIT) |
| 52 |
#define MMC35240_CTRL1_BW_SHIFT 0 |
| 53 |
|
| 54 |
#define MMC35240_WAIT_CHARGE_PUMP 50000 /* us */ |
| 55 |
#define MMC35240_WAIT_SET_RESET 1000 /* us */ |
| 56 |
|
| 57 |
/* |
| 58 |
* Memsic OTP process code piece is put here for reference: |
| 59 |
* |
| 60 |
* #define OTP_CONVERT(REG) ((float)((REG) >=32 ? (32 - (REG)) : (REG)) * 0.006 |
| 61 |
* 1) For X axis, the COEFFICIENT is always 1. |
| 62 |
* 2) For Y axis, the COEFFICIENT is as below: |
| 63 |
* f_OTP_matrix[4] = OTP_CONVERT(((reg_data[1] & 0x03) << 4) | |
| 64 |
* (reg_data[2] >> 4)) + 1.0; |
| 65 |
* 3) For Z axis, the COEFFICIENT is as below: |
| 66 |
* f_OTP_matrix[8] = (OTP_CONVERT(reg_data[3] & 0x3f) + 1) * 1.35; |
| 67 |
* We implemented the OTP logic into driver. |
| 68 |
*/ |
| 69 |
|
| 70 |
/* scale = 1000 here for Y otp */ |
| 71 |
#define MMC35240_OTP_CONVERT_Y(REG) (((REG) >= 32 ? (32 - (REG)) : (REG)) * 6) |
| 72 |
|
| 73 |
/* 0.6 * 1.35 = 0.81, scale 10000 for Z otp */ |
| 74 |
#define MMC35240_OTP_CONVERT_Z(REG) (((REG) >= 32 ? (32 - (REG)) : (REG)) * 81) |
| 75 |
|
| 76 |
#define MMC35240_X_COEFF(x) (x) |
| 77 |
#define MMC35240_Y_COEFF(y) (y + 1000) |
| 78 |
#define MMC35240_Z_COEFF(z) (z + 13500) |
| 79 |
|
| 80 |
#define MMC35240_OTP_START_ADDR 0x1B |
| 81 |
|
| 82 |
enum mmc35240_resolution { |
| 83 |
MMC35240_16_BITS_SLOW = 0, /* 7.92 ms */ |
| 84 |
MMC35240_16_BITS_FAST, /* 4.08 ms */ |
| 85 |
MMC35240_14_BITS, /* 2.16 ms */ |
| 86 |
MMC35240_12_BITS, /* 1.20 ms */ |
| 87 |
}; |
| 88 |
|
| 89 |
enum mmc35240_axis { |
| 90 |
AXIS_X = 0, |
| 91 |
AXIS_Y, |
| 92 |
AXIS_Z, |
| 93 |
}; |
| 94 |
|
| 95 |
static const struct { |
| 96 |
int sens[3]; /* sensitivity per X, Y, Z axis */ |
| 97 |
int nfo; /* null field output */ |
| 98 |
} mmc35240_props_table[] = { |
| 99 |
/* 16 bits, 125Hz ODR */ |
| 100 |
{ |
| 101 |
{1024, 1024, 1024}, |
| 102 |
32768, |
| 103 |
}, |
| 104 |
/* 16 bits, 250Hz ODR */ |
| 105 |
{ |
| 106 |
{1024, 1024, 770}, |
| 107 |
32768, |
| 108 |
}, |
| 109 |
/* 14 bits, 450Hz ODR */ |
| 110 |
{ |
| 111 |
{256, 256, 193}, |
| 112 |
8192, |
| 113 |
}, |
| 114 |
/* 12 bits, 800Hz ODR */ |
| 115 |
{ |
| 116 |
{64, 64, 48}, |
| 117 |
2048, |
| 118 |
}, |
| 119 |
}; |
| 120 |
|
| 121 |
struct mmc35240_data { |
| 122 |
struct i2c_client *client; |
| 123 |
struct mutex mutex; |
| 124 |
struct regmap *regmap; |
| 125 |
enum mmc35240_resolution res; |
| 126 |
|
| 127 |
/* OTP compensation */ |
| 128 |
int axis_coef[3]; |
| 129 |
int axis_scale[3]; |
| 130 |
}; |
| 131 |
|
| 132 |
static const struct { |
| 133 |
int val; |
| 134 |
int val2; |
| 135 |
} mmc35240_samp_freq[] = { {1, 500000}, |
| 136 |
{13, 0}, |
| 137 |
{25, 0}, |
| 138 |
{50, 0} }; |
| 139 |
|
| 140 |
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1.5 13 25 50"); |
| 141 |
|
| 142 |
#define MMC35240_CHANNEL(_axis) { \ |
| 143 |
.type = IIO_MAGN, \ |
| 144 |
.modified = 1, \ |
| 145 |
.channel2 = IIO_MOD_ ## _axis, \ |
| 146 |
.address = AXIS_ ## _axis, \ |
| 147 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 148 |
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ |
| 149 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 150 |
} |
| 151 |
|
| 152 |
static const struct iio_chan_spec mmc35240_channels[] = { |
| 153 |
MMC35240_CHANNEL(X), |
| 154 |
MMC35240_CHANNEL(Y), |
| 155 |
MMC35240_CHANNEL(Z), |
| 156 |
}; |
| 157 |
|
| 158 |
static struct attribute *mmc35240_attributes[] = { |
| 159 |
&iio_const_attr_sampling_frequency_available.dev_attr.attr, |
| 160 |
NULL |
| 161 |
}; |
| 162 |
|
| 163 |
static const struct attribute_group mmc35240_attribute_group = { |
| 164 |
.attrs = mmc35240_attributes, |
| 165 |
}; |
| 166 |
|
| 167 |
static int mmc35240_get_samp_freq_index(struct mmc35240_data *data, |
| 168 |
int val, int val2) |
| 169 |
{ |
| 170 |
int i; |
| 171 |
|
| 172 |
for (i = 0; i < ARRAY_SIZE(mmc35240_samp_freq); i++) |
| 173 |
if (mmc35240_samp_freq[i].val == val && |
| 174 |
mmc35240_samp_freq[i].val2 == val2) |
| 175 |
return i; |
| 176 |
return -EINVAL; |
| 177 |
} |
| 178 |
|
| 179 |
static int mmc35240_hw_set(struct mmc35240_data *data, bool set) |
| 180 |
{ |
| 181 |
int ret; |
| 182 |
u8 coil_bit; |
| 183 |
|
| 184 |
/* |
| 185 |
* Recharge the capacitor at VCAP pin, requested to be issued |
| 186 |
* before a SET/RESET command. |
| 187 |
*/ |
| 188 |
ret = regmap_set_bits(data->regmap, MMC35240_REG_CTRL0, |
| 189 |
MMC35240_CTRL0_REFILL_BIT); |
| 190 |
if (ret < 0) |
| 191 |
return ret; |
| 192 |
usleep_range(MMC35240_WAIT_CHARGE_PUMP, MMC35240_WAIT_CHARGE_PUMP + 1); |
| 193 |
|
| 194 |
if (set) |
| 195 |
coil_bit = MMC35240_CTRL0_SET_BIT; |
| 196 |
else |
| 197 |
coil_bit = MMC35240_CTRL0_RESET_BIT; |
| 198 |
|
| 199 |
return regmap_set_bits(data->regmap, MMC35240_REG_CTRL0, coil_bit); |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
static int mmc35240_init(struct mmc35240_data *data) |
| 204 |
{ |
| 205 |
int ret, y_convert, z_convert; |
| 206 |
unsigned int reg_id; |
| 207 |
u8 otp_data[6]; |
| 208 |
|
| 209 |
ret = regmap_read(data->regmap, MMC35240_REG_ID, ®_id); |
| 210 |
if (ret < 0) { |
| 211 |
dev_err(&data->client->dev, "Error reading product id\n"); |
| 212 |
return ret; |
| 213 |
} |
| 214 |
|
| 215 |
dev_dbg(&data->client->dev, "MMC35240 chip id %x\n", reg_id); |
| 216 |
|
| 217 |
/* |
| 218 |
* make sure we restore sensor characteristics, by doing |
| 219 |
* a SET/RESET sequence, the axis polarity being naturally |
| 220 |
* aligned after RESET |
| 221 |
*/ |
| 222 |
ret = mmc35240_hw_set(data, true); |
| 223 |
if (ret < 0) |
| 224 |
return ret; |
| 225 |
usleep_range(MMC35240_WAIT_SET_RESET, MMC35240_WAIT_SET_RESET + 1); |
| 226 |
|
| 227 |
ret = mmc35240_hw_set(data, false); |
| 228 |
if (ret < 0) |
| 229 |
return ret; |
| 230 |
|
| 231 |
/* set default sampling frequency */ |
| 232 |
ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, |
| 233 |
MMC35240_CTRL1_BW_MASK, |
| 234 |
data->res << MMC35240_CTRL1_BW_SHIFT); |
| 235 |
if (ret < 0) |
| 236 |
return ret; |
| 237 |
|
| 238 |
ret = regmap_bulk_read(data->regmap, MMC35240_OTP_START_ADDR, |
| 239 |
otp_data, sizeof(otp_data)); |
| 240 |
if (ret < 0) |
| 241 |
return ret; |
| 242 |
|
| 243 |
y_convert = MMC35240_OTP_CONVERT_Y(((otp_data[1] & 0x03) << 4) | |
| 244 |
(otp_data[2] >> 4)); |
| 245 |
z_convert = MMC35240_OTP_CONVERT_Z(otp_data[3] & 0x3f); |
| 246 |
|
| 247 |
data->axis_coef[0] = MMC35240_X_COEFF(1); |
| 248 |
data->axis_coef[1] = MMC35240_Y_COEFF(y_convert); |
| 249 |
data->axis_coef[2] = MMC35240_Z_COEFF(z_convert); |
| 250 |
|
| 251 |
data->axis_scale[0] = 1; |
| 252 |
data->axis_scale[1] = 1000; |
| 253 |
data->axis_scale[2] = 10000; |
| 254 |
|
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
static int mmc35240_take_measurement(struct mmc35240_data *data) |
| 259 |
{ |
| 260 |
int ret, tries = 100; |
| 261 |
unsigned int reg_status; |
| 262 |
|
| 263 |
ret = regmap_write(data->regmap, MMC35240_REG_CTRL0, |
| 264 |
MMC35240_CTRL0_TM_BIT); |
| 265 |
if (ret < 0) |
| 266 |
return ret; |
| 267 |
|
| 268 |
while (tries-- > 0) { |
| 269 |
ret = regmap_read(data->regmap, MMC35240_REG_STATUS, |
| 270 |
®_status); |
| 271 |
if (ret < 0) |
| 272 |
return ret; |
| 273 |
if (reg_status & MMC35240_STATUS_MEAS_DONE_BIT) |
| 274 |
break; |
| 275 |
/* minimum wait time to complete measurement is 10 ms */ |
| 276 |
usleep_range(10000, 11000); |
| 277 |
} |
| 278 |
|
| 279 |
if (tries < 0) { |
| 280 |
dev_err(&data->client->dev, "data not ready\n"); |
| 281 |
return -EIO; |
| 282 |
} |
| 283 |
|
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
static int mmc35240_read_measurement(struct mmc35240_data *data, __le16 buf[3]) |
| 288 |
{ |
| 289 |
int ret; |
| 290 |
|
| 291 |
ret = mmc35240_take_measurement(data); |
| 292 |
if (ret < 0) |
| 293 |
return ret; |
| 294 |
|
| 295 |
return regmap_bulk_read(data->regmap, MMC35240_REG_XOUT_L, buf, |
| 296 |
3 * sizeof(__le16)); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* mmc35240_raw_to_mgauss - convert raw readings to milli gauss. Also apply |
| 301 |
* compensation for output value. |
| 302 |
* |
| 303 |
* @data: device private data |
| 304 |
* @index: axis index for which we want the conversion |
| 305 |
* @buf: raw data to be converted, 2 bytes in little endian format |
| 306 |
* @val: compensated output reading (unit is milli gauss) |
| 307 |
* |
| 308 |
* Returns: 0 in case of success, -EINVAL when @index is not valid |
| 309 |
*/ |
| 310 |
static int mmc35240_raw_to_mgauss(struct mmc35240_data *data, int index, |
| 311 |
__le16 buf[], int *val) |
| 312 |
{ |
| 313 |
int raw[3]; |
| 314 |
int sens[3]; |
| 315 |
int nfo; |
| 316 |
|
| 317 |
raw[AXIS_X] = le16_to_cpu(buf[AXIS_X]); |
| 318 |
raw[AXIS_Y] = le16_to_cpu(buf[AXIS_Y]); |
| 319 |
raw[AXIS_Z] = le16_to_cpu(buf[AXIS_Z]); |
| 320 |
|
| 321 |
sens[AXIS_X] = mmc35240_props_table[data->res].sens[AXIS_X]; |
| 322 |
sens[AXIS_Y] = mmc35240_props_table[data->res].sens[AXIS_Y]; |
| 323 |
sens[AXIS_Z] = mmc35240_props_table[data->res].sens[AXIS_Z]; |
| 324 |
|
| 325 |
nfo = mmc35240_props_table[data->res].nfo; |
| 326 |
|
| 327 |
switch (index) { |
| 328 |
case AXIS_X: |
| 329 |
*val = (raw[AXIS_X] - nfo) * 1000 / sens[AXIS_X]; |
| 330 |
break; |
| 331 |
case AXIS_Y: |
| 332 |
*val = (raw[AXIS_Y] - nfo) * 1000 / sens[AXIS_Y] - |
| 333 |
(raw[AXIS_Z] - nfo) * 1000 / sens[AXIS_Z]; |
| 334 |
break; |
| 335 |
case AXIS_Z: |
| 336 |
*val = (raw[AXIS_Y] - nfo) * 1000 / sens[AXIS_Y] + |
| 337 |
(raw[AXIS_Z] - nfo) * 1000 / sens[AXIS_Z]; |
| 338 |
break; |
| 339 |
default: |
| 340 |
return -EINVAL; |
| 341 |
} |
| 342 |
/* apply OTP compensation */ |
| 343 |
*val = (*val) * data->axis_coef[index] / data->axis_scale[index]; |
| 344 |
|
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
static int mmc35240_read_raw(struct iio_dev *indio_dev, |
| 349 |
struct iio_chan_spec const *chan, int *val, |
| 350 |
int *val2, long mask) |
| 351 |
{ |
| 352 |
struct mmc35240_data *data = iio_priv(indio_dev); |
| 353 |
int ret, i; |
| 354 |
unsigned int reg; |
| 355 |
__le16 buf[3]; |
| 356 |
|
| 357 |
switch (mask) { |
| 358 |
case IIO_CHAN_INFO_RAW: |
| 359 |
mutex_lock(&data->mutex); |
| 360 |
ret = mmc35240_read_measurement(data, buf); |
| 361 |
mutex_unlock(&data->mutex); |
| 362 |
if (ret < 0) |
| 363 |
return ret; |
| 364 |
ret = mmc35240_raw_to_mgauss(data, chan->address, buf, val); |
| 365 |
if (ret < 0) |
| 366 |
return ret; |
| 367 |
return IIO_VAL_INT; |
| 368 |
case IIO_CHAN_INFO_SCALE: |
| 369 |
*val = 0; |
| 370 |
*val2 = 1000; |
| 371 |
return IIO_VAL_INT_PLUS_MICRO; |
| 372 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 373 |
mutex_lock(&data->mutex); |
| 374 |
ret = regmap_read(data->regmap, MMC35240_REG_CTRL1, ®); |
| 375 |
mutex_unlock(&data->mutex); |
| 376 |
if (ret < 0) |
| 377 |
return ret; |
| 378 |
|
| 379 |
i = (reg & MMC35240_CTRL1_BW_MASK) >> MMC35240_CTRL1_BW_SHIFT; |
| 380 |
if (i < 0 || i >= ARRAY_SIZE(mmc35240_samp_freq)) |
| 381 |
return -EINVAL; |
| 382 |
|
| 383 |
*val = mmc35240_samp_freq[i].val; |
| 384 |
*val2 = mmc35240_samp_freq[i].val2; |
| 385 |
return IIO_VAL_INT_PLUS_MICRO; |
| 386 |
default: |
| 387 |
return -EINVAL; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
static int mmc35240_write_raw(struct iio_dev *indio_dev, |
| 392 |
struct iio_chan_spec const *chan, int val, |
| 393 |
int val2, long mask) |
| 394 |
{ |
| 395 |
struct mmc35240_data *data = iio_priv(indio_dev); |
| 396 |
int i, ret; |
| 397 |
|
| 398 |
switch (mask) { |
| 399 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 400 |
i = mmc35240_get_samp_freq_index(data, val, val2); |
| 401 |
if (i < 0) |
| 402 |
return -EINVAL; |
| 403 |
mutex_lock(&data->mutex); |
| 404 |
ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, |
| 405 |
MMC35240_CTRL1_BW_MASK, |
| 406 |
i << MMC35240_CTRL1_BW_SHIFT); |
| 407 |
mutex_unlock(&data->mutex); |
| 408 |
return ret; |
| 409 |
default: |
| 410 |
return -EINVAL; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
static const struct iio_info mmc35240_info = { |
| 415 |
.read_raw = mmc35240_read_raw, |
| 416 |
.write_raw = mmc35240_write_raw, |
| 417 |
.attrs = &mmc35240_attribute_group, |
| 418 |
}; |
| 419 |
|
| 420 |
static bool mmc35240_is_writeable_reg(struct device *dev, unsigned int reg) |
| 421 |
{ |
| 422 |
switch (reg) { |
| 423 |
case MMC35240_REG_CTRL0: |
| 424 |
case MMC35240_REG_CTRL1: |
| 425 |
return true; |
| 426 |
default: |
| 427 |
return false; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
static bool mmc35240_is_readable_reg(struct device *dev, unsigned int reg) |
| 432 |
{ |
| 433 |
switch (reg) { |
| 434 |
case MMC35240_REG_XOUT_L: |
| 435 |
case MMC35240_REG_XOUT_H: |
| 436 |
case MMC35240_REG_YOUT_L: |
| 437 |
case MMC35240_REG_YOUT_H: |
| 438 |
case MMC35240_REG_ZOUT_L: |
| 439 |
case MMC35240_REG_ZOUT_H: |
| 440 |
case MMC35240_REG_STATUS: |
| 441 |
case MMC35240_REG_ID: |
| 442 |
return true; |
| 443 |
default: |
| 444 |
return false; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
static bool mmc35240_is_volatile_reg(struct device *dev, unsigned int reg) |
| 449 |
{ |
| 450 |
switch (reg) { |
| 451 |
case MMC35240_REG_CTRL0: |
| 452 |
case MMC35240_REG_CTRL1: |
| 453 |
return false; |
| 454 |
default: |
| 455 |
return true; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
static const struct reg_default mmc35240_reg_defaults[] = { |
| 460 |
{ MMC35240_REG_CTRL0, 0x00 }, |
| 461 |
{ MMC35240_REG_CTRL1, 0x00 }, |
| 462 |
}; |
| 463 |
|
| 464 |
static const struct regmap_config mmc35240_regmap_config = { |
| 465 |
.name = "mmc35240_regmap", |
| 466 |
|
| 467 |
.reg_bits = 8, |
| 468 |
.val_bits = 8, |
| 469 |
|
| 470 |
.max_register = MMC35240_REG_ID, |
| 471 |
.cache_type = REGCACHE_FLAT, |
| 472 |
|
| 473 |
.writeable_reg = mmc35240_is_writeable_reg, |
| 474 |
.readable_reg = mmc35240_is_readable_reg, |
| 475 |
.volatile_reg = mmc35240_is_volatile_reg, |
| 476 |
|
| 477 |
.reg_defaults = mmc35240_reg_defaults, |
| 478 |
.num_reg_defaults = ARRAY_SIZE(mmc35240_reg_defaults), |
| 479 |
}; |
| 480 |
|
| 481 |
static int mmc35240_probe(struct i2c_client *client) |
| 482 |
{ |
| 483 |
struct mmc35240_data *data; |
| 484 |
struct iio_dev *indio_dev; |
| 485 |
struct regmap *regmap; |
| 486 |
int ret; |
| 487 |
|
| 488 |
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 489 |
if (!indio_dev) |
| 490 |
return -ENOMEM; |
| 491 |
|
| 492 |
regmap = devm_regmap_init_i2c(client, &mmc35240_regmap_config); |
| 493 |
if (IS_ERR(regmap)) { |
| 494 |
dev_err(&client->dev, "regmap initialization failed\n"); |
| 495 |
return PTR_ERR(regmap); |
| 496 |
} |
| 497 |
|
| 498 |
data = iio_priv(indio_dev); |
| 499 |
i2c_set_clientdata(client, indio_dev); |
| 500 |
data->client = client; |
| 501 |
data->regmap = regmap; |
| 502 |
data->res = MMC35240_16_BITS_SLOW; |
| 503 |
|
| 504 |
mutex_init(&data->mutex); |
| 505 |
|
| 506 |
indio_dev->info = &mmc35240_info; |
| 507 |
indio_dev->name = MMC35240_DRV_NAME; |
| 508 |
indio_dev->channels = mmc35240_channels; |
| 509 |
indio_dev->num_channels = ARRAY_SIZE(mmc35240_channels); |
| 510 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 511 |
|
| 512 |
ret = mmc35240_init(data); |
| 513 |
if (ret < 0) { |
| 514 |
dev_err(&client->dev, "mmc35240 chip init failed\n"); |
| 515 |
return ret; |
| 516 |
} |
| 517 |
return devm_iio_device_register(&client->dev, indio_dev); |
| 518 |
} |
| 519 |
|
| 520 |
static int mmc35240_suspend(struct device *dev) |
| 521 |
{ |
| 522 |
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 523 |
struct mmc35240_data *data = iio_priv(indio_dev); |
| 524 |
|
| 525 |
regcache_cache_only(data->regmap, true); |
| 526 |
|
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
static int mmc35240_resume(struct device *dev) |
| 531 |
{ |
| 532 |
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 533 |
struct mmc35240_data *data = iio_priv(indio_dev); |
| 534 |
int ret; |
| 535 |
|
| 536 |
regcache_mark_dirty(data->regmap); |
| 537 |
ret = regcache_sync_region(data->regmap, MMC35240_REG_CTRL0, |
| 538 |
MMC35240_REG_CTRL1); |
| 539 |
if (ret < 0) |
| 540 |
dev_err(dev, "Failed to restore control registers\n"); |
| 541 |
|
| 542 |
regcache_cache_only(data->regmap, false); |
| 543 |
|
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
static DEFINE_SIMPLE_DEV_PM_OPS(mmc35240_pm_ops, mmc35240_suspend, |
| 548 |
mmc35240_resume); |
| 549 |
|
| 550 |
static const struct of_device_id mmc35240_of_match[] = { |
| 551 |
{ .compatible = "memsic,mmc35240", }, |
| 552 |
{ } |
| 553 |
}; |
| 554 |
MODULE_DEVICE_TABLE(of, mmc35240_of_match); |
| 555 |
|
| 556 |
static const struct acpi_device_id mmc35240_acpi_match[] = { |
| 557 |
{"MMC35240", 0}, |
| 558 |
{ } |
| 559 |
}; |
| 560 |
MODULE_DEVICE_TABLE(acpi, mmc35240_acpi_match); |
| 561 |
|
| 562 |
static const struct i2c_device_id mmc35240_id[] = { |
| 563 |
{ "mmc35240" }, |
| 564 |
{ } |
| 565 |
}; |
| 566 |
MODULE_DEVICE_TABLE(i2c, mmc35240_id); |
| 567 |
|
| 568 |
static struct i2c_driver mmc35240_driver = { |
| 569 |
.driver = { |
| 570 |
.name = MMC35240_DRV_NAME, |
| 571 |
.of_match_table = mmc35240_of_match, |
| 572 |
.pm = pm_sleep_ptr(&mmc35240_pm_ops), |
| 573 |
.acpi_match_table = mmc35240_acpi_match, |
| 574 |
}, |
| 575 |
.probe = mmc35240_probe, |
| 576 |
.id_table = mmc35240_id, |
| 577 |
}; |
| 578 |
|
| 579 |
module_i2c_driver(mmc35240_driver); |
| 580 |
|
| 581 |
MODULE_AUTHOR("Daniel Baluta <[email protected]>"); |
| 582 |
MODULE_DESCRIPTION("MEMSIC MMC35240 magnetic sensor driver"); |
| 583 |
MODULE_LICENSE("GPL v2"); |