| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Driver for the TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor |
| 4 |
* |
| 5 |
* Copyright (C) 2022 WolfVision GmbH |
| 6 |
* |
| 7 |
* Author: Gerald Loacker <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/bitfield.h> |
| 11 |
#include <linux/bits.h> |
| 12 |
#include <linux/delay.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/i2c.h> |
| 15 |
#include <linux/regmap.h> |
| 16 |
#include <linux/pm_runtime.h> |
| 17 |
|
| 18 |
#include <linux/iio/iio.h> |
| 19 |
#include <linux/iio/sysfs.h> |
| 20 |
|
| 21 |
#define TMAG5273_DEVICE_CONFIG_1 0x00 |
| 22 |
#define TMAG5273_DEVICE_CONFIG_2 0x01 |
| 23 |
#define TMAG5273_SENSOR_CONFIG_1 0x02 |
| 24 |
#define TMAG5273_SENSOR_CONFIG_2 0x03 |
| 25 |
#define TMAG5273_X_THR_CONFIG 0x04 |
| 26 |
#define TMAG5273_Y_THR_CONFIG 0x05 |
| 27 |
#define TMAG5273_Z_THR_CONFIG 0x06 |
| 28 |
#define TMAG5273_T_CONFIG 0x07 |
| 29 |
#define TMAG5273_INT_CONFIG_1 0x08 |
| 30 |
#define TMAG5273_MAG_GAIN_CONFIG 0x09 |
| 31 |
#define TMAG5273_MAG_OFFSET_CONFIG_1 0x0A |
| 32 |
#define TMAG5273_MAG_OFFSET_CONFIG_2 0x0B |
| 33 |
#define TMAG5273_I2C_ADDRESS 0x0C |
| 34 |
#define TMAG5273_DEVICE_ID 0x0D |
| 35 |
#define TMAG5273_MANUFACTURER_ID_LSB 0x0E |
| 36 |
#define TMAG5273_MANUFACTURER_ID_MSB 0x0F |
| 37 |
#define TMAG5273_T_MSB_RESULT 0x10 |
| 38 |
#define TMAG5273_T_LSB_RESULT 0x11 |
| 39 |
#define TMAG5273_X_MSB_RESULT 0x12 |
| 40 |
#define TMAG5273_X_LSB_RESULT 0x13 |
| 41 |
#define TMAG5273_Y_MSB_RESULT 0x14 |
| 42 |
#define TMAG5273_Y_LSB_RESULT 0x15 |
| 43 |
#define TMAG5273_Z_MSB_RESULT 0x16 |
| 44 |
#define TMAG5273_Z_LSB_RESULT 0x17 |
| 45 |
#define TMAG5273_CONV_STATUS 0x18 |
| 46 |
#define TMAG5273_ANGLE_RESULT_MSB 0x19 |
| 47 |
#define TMAG5273_ANGLE_RESULT_LSB 0x1A |
| 48 |
#define TMAG5273_MAGNITUDE_RESULT 0x1B |
| 49 |
#define TMAG5273_DEVICE_STATUS 0x1C |
| 50 |
#define TMAG5273_MAX_REG TMAG5273_DEVICE_STATUS |
| 51 |
|
| 52 |
#define TMAG5273_AUTOSLEEP_DELAY_MS 5000 |
| 53 |
#define TMAG5273_MAX_AVERAGE 32 |
| 54 |
|
| 55 |
/* |
| 56 |
* bits in the TMAG5273_MANUFACTURER_ID_LSB / MSB register |
| 57 |
* 16-bit unique manufacturer ID 0x49 / 0x54 = "TI" |
| 58 |
*/ |
| 59 |
#define TMAG5273_MANUFACTURER_ID 0x5449 |
| 60 |
|
| 61 |
/* bits in the TMAG5273_DEVICE_CONFIG_1 register */ |
| 62 |
#define TMAG5273_AVG_MODE_MASK GENMASK(4, 2) |
| 63 |
#define TMAG5273_AVG_1_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 0) |
| 64 |
#define TMAG5273_AVG_2_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 1) |
| 65 |
#define TMAG5273_AVG_4_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 2) |
| 66 |
#define TMAG5273_AVG_8_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 3) |
| 67 |
#define TMAG5273_AVG_16_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 4) |
| 68 |
#define TMAG5273_AVG_32_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 5) |
| 69 |
|
| 70 |
/* bits in the TMAG5273_DEVICE_CONFIG_2 register */ |
| 71 |
#define TMAG5273_OP_MODE_MASK GENMASK(1, 0) |
| 72 |
#define TMAG5273_OP_MODE_STANDBY FIELD_PREP(TMAG5273_OP_MODE_MASK, 0) |
| 73 |
#define TMAG5273_OP_MODE_SLEEP FIELD_PREP(TMAG5273_OP_MODE_MASK, 1) |
| 74 |
#define TMAG5273_OP_MODE_CONT FIELD_PREP(TMAG5273_OP_MODE_MASK, 2) |
| 75 |
#define TMAG5273_OP_MODE_WAKEUP FIELD_PREP(TMAG5273_OP_MODE_MASK, 3) |
| 76 |
|
| 77 |
/* bits in the TMAG5273_SENSOR_CONFIG_1 register */ |
| 78 |
#define TMAG5273_MAG_CH_EN_MASK GENMASK(7, 4) |
| 79 |
#define TMAG5273_MAG_CH_EN_X_Y_Z 7 |
| 80 |
|
| 81 |
/* bits in the TMAG5273_SENSOR_CONFIG_2 register */ |
| 82 |
#define TMAG5273_Z_RANGE_MASK BIT(0) |
| 83 |
#define TMAG5273_X_Y_RANGE_MASK BIT(1) |
| 84 |
#define TMAG5273_ANGLE_EN_MASK GENMASK(3, 2) |
| 85 |
#define TMAG5273_ANGLE_EN_OFF 0 |
| 86 |
#define TMAG5273_ANGLE_EN_X_Y 1 |
| 87 |
#define TMAG5273_ANGLE_EN_Y_Z 2 |
| 88 |
#define TMAG5273_ANGLE_EN_X_Z 3 |
| 89 |
|
| 90 |
/* bits in the TMAG5273_T_CONFIG register */ |
| 91 |
#define TMAG5273_T_CH_EN BIT(0) |
| 92 |
|
| 93 |
/* bits in the TMAG5273_DEVICE_ID register */ |
| 94 |
#define TMAG5273_VERSION_MASK GENMASK(1, 0) |
| 95 |
|
| 96 |
/* bits in the TMAG5273_CONV_STATUS register */ |
| 97 |
#define TMAG5273_CONV_STATUS_COMPLETE BIT(0) |
| 98 |
|
| 99 |
enum tmag5273_channels { |
| 100 |
TEMPERATURE = 0, |
| 101 |
AXIS_X, |
| 102 |
AXIS_Y, |
| 103 |
AXIS_Z, |
| 104 |
ANGLE, |
| 105 |
MAGNITUDE, |
| 106 |
}; |
| 107 |
|
| 108 |
enum tmag5273_scale_index { |
| 109 |
MAGN_RANGE_LOW = 0, |
| 110 |
MAGN_RANGE_HIGH, |
| 111 |
MAGN_RANGE_NUM |
| 112 |
}; |
| 113 |
|
| 114 |
/* state container for the TMAG5273 driver */ |
| 115 |
struct tmag5273_data { |
| 116 |
struct device *dev; |
| 117 |
unsigned int devid; |
| 118 |
unsigned int version; |
| 119 |
char name[16]; |
| 120 |
unsigned int conv_avg; |
| 121 |
enum tmag5273_scale_index scale_index; |
| 122 |
unsigned int angle_measurement; |
| 123 |
struct regmap *map; |
| 124 |
|
| 125 |
/* |
| 126 |
* Locks the sensor for exclusive use during a measurement (which |
| 127 |
* involves several register transactions so the regmap lock is not |
| 128 |
* enough) so that measurements get serialized in a |
| 129 |
* first-come-first-serve manner. |
| 130 |
*/ |
| 131 |
struct mutex lock; |
| 132 |
}; |
| 133 |
|
| 134 |
static const char *const tmag5273_angle_names[] = { "off", "x-y", "y-z", "x-z" }; |
| 135 |
|
| 136 |
/* |
| 137 |
* Averaging enables additional sampling of the sensor data to reduce the noise |
| 138 |
* effect, but also increases conversion time. |
| 139 |
*/ |
| 140 |
static const unsigned int tmag5273_avg_table[] = { |
| 141 |
1, 2, 4, 8, 16, 32, |
| 142 |
}; |
| 143 |
|
| 144 |
/* |
| 145 |
* Magnetic resolution in Gauss for different TMAG5273 versions. |
| 146 |
* Scale[Gauss] = Range[mT] * 1000 / 2^15 * 10, (1 mT = 10 Gauss) |
| 147 |
* Only version 1 and 2 are valid, version 0 and 3 are reserved. |
| 148 |
*/ |
| 149 |
static const struct iio_val_int_plus_micro tmag5273_scale[][MAGN_RANGE_NUM] = { |
| 150 |
{ { 0, 0 }, { 0, 0 } }, |
| 151 |
{ { 0, 12200 }, { 0, 24400 } }, |
| 152 |
{ { 0, 40600 }, { 0, 81200 } }, |
| 153 |
{ { 0, 0 }, { 0, 0 } }, |
| 154 |
}; |
| 155 |
|
| 156 |
static int tmag5273_get_measure(struct tmag5273_data *data, s16 *t, s16 *x, |
| 157 |
s16 *y, s16 *z, u16 *angle, u16 *magnitude) |
| 158 |
{ |
| 159 |
unsigned int status, val; |
| 160 |
__be16 reg_data[4]; |
| 161 |
int ret; |
| 162 |
|
| 163 |
mutex_lock(&data->lock); |
| 164 |
|
| 165 |
/* |
| 166 |
* Max. conversion time is 2425 us in 32x averaging mode for all three |
| 167 |
* channels. Since we are in continuous measurement mode, a measurement |
| 168 |
* may already be there, so poll for completed measurement with |
| 169 |
* timeout. |
| 170 |
*/ |
| 171 |
ret = regmap_read_poll_timeout(data->map, TMAG5273_CONV_STATUS, status, |
| 172 |
status & TMAG5273_CONV_STATUS_COMPLETE, |
| 173 |
100, 10000); |
| 174 |
if (ret) { |
| 175 |
dev_err(data->dev, "timeout waiting for measurement\n"); |
| 176 |
goto out_unlock; |
| 177 |
} |
| 178 |
|
| 179 |
ret = regmap_bulk_read(data->map, TMAG5273_T_MSB_RESULT, reg_data, |
| 180 |
sizeof(reg_data)); |
| 181 |
if (ret) |
| 182 |
goto out_unlock; |
| 183 |
*t = be16_to_cpu(reg_data[0]); |
| 184 |
*x = be16_to_cpu(reg_data[1]); |
| 185 |
*y = be16_to_cpu(reg_data[2]); |
| 186 |
*z = be16_to_cpu(reg_data[3]); |
| 187 |
|
| 188 |
ret = regmap_bulk_read(data->map, TMAG5273_ANGLE_RESULT_MSB, |
| 189 |
®_data[0], sizeof(reg_data[0])); |
| 190 |
if (ret) |
| 191 |
goto out_unlock; |
| 192 |
/* |
| 193 |
* angle has 9 bits integer value and 4 bits fractional part |
| 194 |
* 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 195 |
* 0 0 0 a a a a a a a a a f f f f |
| 196 |
*/ |
| 197 |
*angle = be16_to_cpu(reg_data[0]); |
| 198 |
|
| 199 |
ret = regmap_read(data->map, TMAG5273_MAGNITUDE_RESULT, &val); |
| 200 |
if (ret < 0) |
| 201 |
goto out_unlock; |
| 202 |
*magnitude = val; |
| 203 |
|
| 204 |
out_unlock: |
| 205 |
mutex_unlock(&data->lock); |
| 206 |
return ret; |
| 207 |
} |
| 208 |
|
| 209 |
static int tmag5273_write_osr(struct tmag5273_data *data, int val) |
| 210 |
{ |
| 211 |
int i; |
| 212 |
|
| 213 |
if (val == data->conv_avg) |
| 214 |
return 0; |
| 215 |
|
| 216 |
for (i = 0; i < ARRAY_SIZE(tmag5273_avg_table); i++) { |
| 217 |
if (tmag5273_avg_table[i] == val) |
| 218 |
break; |
| 219 |
} |
| 220 |
if (i == ARRAY_SIZE(tmag5273_avg_table)) |
| 221 |
return -EINVAL; |
| 222 |
data->conv_avg = val; |
| 223 |
|
| 224 |
return regmap_update_bits(data->map, TMAG5273_DEVICE_CONFIG_1, |
| 225 |
TMAG5273_AVG_MODE_MASK, |
| 226 |
FIELD_PREP(TMAG5273_AVG_MODE_MASK, i)); |
| 227 |
} |
| 228 |
|
| 229 |
static int tmag5273_write_scale(struct tmag5273_data *data, int scale_micro) |
| 230 |
{ |
| 231 |
u32 value; |
| 232 |
int i; |
| 233 |
|
| 234 |
for (i = 0; i < ARRAY_SIZE(tmag5273_scale[0]); i++) { |
| 235 |
if (tmag5273_scale[data->version][i].micro == scale_micro) |
| 236 |
break; |
| 237 |
} |
| 238 |
if (i == ARRAY_SIZE(tmag5273_scale[0])) |
| 239 |
return -EINVAL; |
| 240 |
data->scale_index = i; |
| 241 |
|
| 242 |
if (data->scale_index == MAGN_RANGE_LOW) |
| 243 |
value = 0; |
| 244 |
else |
| 245 |
value = TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK; |
| 246 |
|
| 247 |
return regmap_update_bits(data->map, TMAG5273_SENSOR_CONFIG_2, |
| 248 |
TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK, value); |
| 249 |
} |
| 250 |
|
| 251 |
static int tmag5273_read_avail(struct iio_dev *indio_dev, |
| 252 |
struct iio_chan_spec const *chan, |
| 253 |
const int **vals, int *type, int *length, |
| 254 |
long mask) |
| 255 |
{ |
| 256 |
struct tmag5273_data *data = iio_priv(indio_dev); |
| 257 |
|
| 258 |
switch (mask) { |
| 259 |
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 260 |
*vals = tmag5273_avg_table; |
| 261 |
*type = IIO_VAL_INT; |
| 262 |
*length = ARRAY_SIZE(tmag5273_avg_table); |
| 263 |
return IIO_AVAIL_LIST; |
| 264 |
case IIO_CHAN_INFO_SCALE: |
| 265 |
switch (chan->type) { |
| 266 |
case IIO_MAGN: |
| 267 |
*type = IIO_VAL_INT_PLUS_MICRO; |
| 268 |
*vals = (int *)tmag5273_scale[data->version]; |
| 269 |
*length = ARRAY_SIZE(tmag5273_scale[data->version]) * |
| 270 |
MAGN_RANGE_NUM; |
| 271 |
return IIO_AVAIL_LIST; |
| 272 |
default: |
| 273 |
return -EINVAL; |
| 274 |
} |
| 275 |
default: |
| 276 |
return -EINVAL; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
static int tmag5273_read_raw(struct iio_dev *indio_dev, |
| 281 |
const struct iio_chan_spec *chan, int *val, |
| 282 |
int *val2, long mask) |
| 283 |
{ |
| 284 |
struct tmag5273_data *data = iio_priv(indio_dev); |
| 285 |
s16 t, x, y, z; |
| 286 |
u16 angle, magnitude; |
| 287 |
int ret; |
| 288 |
|
| 289 |
switch (mask) { |
| 290 |
case IIO_CHAN_INFO_RAW: |
| 291 |
ret = pm_runtime_resume_and_get(data->dev); |
| 292 |
if (ret < 0) |
| 293 |
return ret; |
| 294 |
|
| 295 |
ret = tmag5273_get_measure(data, &t, &x, &y, &z, &angle, &magnitude); |
| 296 |
|
| 297 |
pm_runtime_put_autosuspend(data->dev); |
| 298 |
|
| 299 |
if (ret) |
| 300 |
return ret; |
| 301 |
|
| 302 |
switch (chan->address) { |
| 303 |
case TEMPERATURE: |
| 304 |
*val = t; |
| 305 |
return IIO_VAL_INT; |
| 306 |
case AXIS_X: |
| 307 |
*val = x; |
| 308 |
return IIO_VAL_INT; |
| 309 |
case AXIS_Y: |
| 310 |
*val = y; |
| 311 |
return IIO_VAL_INT; |
| 312 |
case AXIS_Z: |
| 313 |
*val = z; |
| 314 |
return IIO_VAL_INT; |
| 315 |
case ANGLE: |
| 316 |
*val = angle; |
| 317 |
return IIO_VAL_INT; |
| 318 |
case MAGNITUDE: |
| 319 |
*val = magnitude; |
| 320 |
return IIO_VAL_INT; |
| 321 |
default: |
| 322 |
return -EINVAL; |
| 323 |
} |
| 324 |
case IIO_CHAN_INFO_SCALE: |
| 325 |
switch (chan->type) { |
| 326 |
case IIO_TEMP: |
| 327 |
/* |
| 328 |
* Convert device specific value to millicelsius. |
| 329 |
* Resolution from the sensor is 60.1 LSB/celsius and |
| 330 |
* the reference value at 25 celsius is 17508 LSBs. |
| 331 |
*/ |
| 332 |
*val = 10000; |
| 333 |
*val2 = 601; |
| 334 |
return IIO_VAL_FRACTIONAL; |
| 335 |
case IIO_MAGN: |
| 336 |
/* Magnetic resolution in uT */ |
| 337 |
*val = 0; |
| 338 |
*val2 = tmag5273_scale[data->version] |
| 339 |
[data->scale_index].micro; |
| 340 |
return IIO_VAL_INT_PLUS_MICRO; |
| 341 |
case IIO_ANGL: |
| 342 |
/* |
| 343 |
* Angle is in degrees and has four fractional bits, |
| 344 |
* therefore use 1/16 * pi/180 to convert to radians. |
| 345 |
*/ |
| 346 |
*val = 1000; |
| 347 |
*val2 = 916732; |
| 348 |
return IIO_VAL_FRACTIONAL; |
| 349 |
default: |
| 350 |
return -EINVAL; |
| 351 |
} |
| 352 |
case IIO_CHAN_INFO_OFFSET: |
| 353 |
switch (chan->type) { |
| 354 |
case IIO_TEMP: |
| 355 |
*val = -16005; |
| 356 |
return IIO_VAL_INT; |
| 357 |
default: |
| 358 |
return -EINVAL; |
| 359 |
} |
| 360 |
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 361 |
*val = data->conv_avg; |
| 362 |
return IIO_VAL_INT; |
| 363 |
|
| 364 |
default: |
| 365 |
return -EINVAL; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
static int tmag5273_write_raw(struct iio_dev *indio_dev, |
| 370 |
struct iio_chan_spec const *chan, int val, |
| 371 |
int val2, long mask) |
| 372 |
{ |
| 373 |
struct tmag5273_data *data = iio_priv(indio_dev); |
| 374 |
|
| 375 |
switch (mask) { |
| 376 |
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 377 |
return tmag5273_write_osr(data, val); |
| 378 |
case IIO_CHAN_INFO_SCALE: |
| 379 |
switch (chan->type) { |
| 380 |
case IIO_MAGN: |
| 381 |
if (val) |
| 382 |
return -EINVAL; |
| 383 |
return tmag5273_write_scale(data, val2); |
| 384 |
default: |
| 385 |
return -EINVAL; |
| 386 |
} |
| 387 |
default: |
| 388 |
return -EINVAL; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
#define TMAG5273_AXIS_CHANNEL(axis, index) \ |
| 393 |
{ \ |
| 394 |
.type = IIO_MAGN, \ |
| 395 |
.modified = 1, \ |
| 396 |
.channel2 = IIO_MOD_##axis, \ |
| 397 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 398 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 399 |
.info_mask_shared_by_type_available = \ |
| 400 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 401 |
.info_mask_shared_by_all = \ |
| 402 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ |
| 403 |
.info_mask_shared_by_all_available = \ |
| 404 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ |
| 405 |
.address = index, \ |
| 406 |
.scan_index = index, \ |
| 407 |
.scan_type = { \ |
| 408 |
.sign = 's', \ |
| 409 |
.realbits = 16, \ |
| 410 |
.storagebits = 16, \ |
| 411 |
.endianness = IIO_CPU, \ |
| 412 |
}, \ |
| 413 |
} |
| 414 |
|
| 415 |
static const struct iio_chan_spec tmag5273_channels[] = { |
| 416 |
{ |
| 417 |
.type = IIO_TEMP, |
| 418 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 419 |
BIT(IIO_CHAN_INFO_SCALE) | |
| 420 |
BIT(IIO_CHAN_INFO_OFFSET), |
| 421 |
.address = TEMPERATURE, |
| 422 |
.scan_index = TEMPERATURE, |
| 423 |
.scan_type = { |
| 424 |
.sign = 'u', |
| 425 |
.realbits = 16, |
| 426 |
.storagebits = 16, |
| 427 |
.endianness = IIO_CPU, |
| 428 |
}, |
| 429 |
}, |
| 430 |
TMAG5273_AXIS_CHANNEL(X, AXIS_X), |
| 431 |
TMAG5273_AXIS_CHANNEL(Y, AXIS_Y), |
| 432 |
TMAG5273_AXIS_CHANNEL(Z, AXIS_Z), |
| 433 |
{ |
| 434 |
.type = IIO_ANGL, |
| 435 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 436 |
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
| 437 |
.info_mask_shared_by_all = |
| 438 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), |
| 439 |
.info_mask_shared_by_all_available = |
| 440 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), |
| 441 |
.address = ANGLE, |
| 442 |
.scan_index = ANGLE, |
| 443 |
.scan_type = { |
| 444 |
.sign = 'u', |
| 445 |
.realbits = 16, |
| 446 |
.storagebits = 16, |
| 447 |
.endianness = IIO_CPU, |
| 448 |
}, |
| 449 |
}, |
| 450 |
{ |
| 451 |
.type = IIO_DISTANCE, |
| 452 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 453 |
.info_mask_shared_by_all = |
| 454 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), |
| 455 |
.info_mask_shared_by_all_available = |
| 456 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), |
| 457 |
.address = MAGNITUDE, |
| 458 |
.scan_index = MAGNITUDE, |
| 459 |
.scan_type = { |
| 460 |
.sign = 'u', |
| 461 |
.realbits = 16, |
| 462 |
.storagebits = 16, |
| 463 |
.endianness = IIO_CPU, |
| 464 |
}, |
| 465 |
}, |
| 466 |
IIO_CHAN_SOFT_TIMESTAMP(6), |
| 467 |
}; |
| 468 |
|
| 469 |
static const struct iio_info tmag5273_info = { |
| 470 |
.read_avail = tmag5273_read_avail, |
| 471 |
.read_raw = tmag5273_read_raw, |
| 472 |
.write_raw = tmag5273_write_raw, |
| 473 |
}; |
| 474 |
|
| 475 |
static bool tmag5273_volatile_reg(struct device *dev, unsigned int reg) |
| 476 |
{ |
| 477 |
return reg >= TMAG5273_T_MSB_RESULT && reg <= TMAG5273_MAGNITUDE_RESULT; |
| 478 |
} |
| 479 |
|
| 480 |
static const struct regmap_config tmag5273_regmap_config = { |
| 481 |
.reg_bits = 8, |
| 482 |
.val_bits = 8, |
| 483 |
.max_register = TMAG5273_MAX_REG, |
| 484 |
.volatile_reg = tmag5273_volatile_reg, |
| 485 |
}; |
| 486 |
|
| 487 |
static int tmag5273_set_operating_mode(struct tmag5273_data *data, |
| 488 |
unsigned int val) |
| 489 |
{ |
| 490 |
return regmap_write(data->map, TMAG5273_DEVICE_CONFIG_2, val); |
| 491 |
} |
| 492 |
|
| 493 |
static void tmag5273_read_device_property(struct tmag5273_data *data) |
| 494 |
{ |
| 495 |
struct device *dev = data->dev; |
| 496 |
int ret; |
| 497 |
|
| 498 |
data->angle_measurement = TMAG5273_ANGLE_EN_X_Y; |
| 499 |
|
| 500 |
ret = device_property_match_property_string(dev, "ti,angle-measurement", |
| 501 |
tmag5273_angle_names, |
| 502 |
ARRAY_SIZE(tmag5273_angle_names)); |
| 503 |
if (ret >= 0) |
| 504 |
data->angle_measurement = ret; |
| 505 |
} |
| 506 |
|
| 507 |
static void tmag5273_wake_up(struct tmag5273_data *data) |
| 508 |
{ |
| 509 |
int val; |
| 510 |
|
| 511 |
/* Wake up the chip by sending a dummy I2C command */ |
| 512 |
regmap_read(data->map, TMAG5273_DEVICE_ID, &val); |
| 513 |
/* |
| 514 |
* Time to go to stand-by mode from sleep mode is 50us |
| 515 |
* typically, during this time no I2C access is possible. |
| 516 |
*/ |
| 517 |
usleep_range(80, 200); |
| 518 |
} |
| 519 |
|
| 520 |
static int tmag5273_chip_init(struct tmag5273_data *data) |
| 521 |
{ |
| 522 |
int ret; |
| 523 |
|
| 524 |
ret = regmap_write(data->map, TMAG5273_DEVICE_CONFIG_1, |
| 525 |
TMAG5273_AVG_32_MODE); |
| 526 |
if (ret) |
| 527 |
return ret; |
| 528 |
data->conv_avg = 32; |
| 529 |
|
| 530 |
ret = regmap_write(data->map, TMAG5273_DEVICE_CONFIG_2, |
| 531 |
TMAG5273_OP_MODE_CONT); |
| 532 |
if (ret) |
| 533 |
return ret; |
| 534 |
|
| 535 |
ret = regmap_write(data->map, TMAG5273_SENSOR_CONFIG_1, |
| 536 |
FIELD_PREP(TMAG5273_MAG_CH_EN_MASK, |
| 537 |
TMAG5273_MAG_CH_EN_X_Y_Z)); |
| 538 |
if (ret) |
| 539 |
return ret; |
| 540 |
|
| 541 |
ret = regmap_write(data->map, TMAG5273_SENSOR_CONFIG_2, |
| 542 |
FIELD_PREP(TMAG5273_ANGLE_EN_MASK, |
| 543 |
data->angle_measurement)); |
| 544 |
if (ret) |
| 545 |
return ret; |
| 546 |
data->scale_index = MAGN_RANGE_LOW; |
| 547 |
|
| 548 |
return regmap_write(data->map, TMAG5273_T_CONFIG, TMAG5273_T_CH_EN); |
| 549 |
} |
| 550 |
|
| 551 |
static int tmag5273_check_device_id(struct tmag5273_data *data) |
| 552 |
{ |
| 553 |
__le16 devid; |
| 554 |
int val, ret; |
| 555 |
|
| 556 |
ret = regmap_read(data->map, TMAG5273_DEVICE_ID, &val); |
| 557 |
if (ret) |
| 558 |
return dev_err_probe(data->dev, ret, "failed to power on device\n"); |
| 559 |
data->version = FIELD_PREP(TMAG5273_VERSION_MASK, val); |
| 560 |
|
| 561 |
ret = regmap_bulk_read(data->map, TMAG5273_MANUFACTURER_ID_LSB, &devid, |
| 562 |
sizeof(devid)); |
| 563 |
if (ret) |
| 564 |
return dev_err_probe(data->dev, ret, "failed to read device ID\n"); |
| 565 |
data->devid = le16_to_cpu(devid); |
| 566 |
|
| 567 |
switch (data->devid) { |
| 568 |
case TMAG5273_MANUFACTURER_ID: |
| 569 |
/* |
| 570 |
* The device name matches the orderable part number. 'x' stands |
| 571 |
* for A, B, C or D devices, which have different I2C addresses. |
| 572 |
* Versions 1 or 2 (0 and 3 is reserved) stands for different |
| 573 |
* magnetic strengths. |
| 574 |
*/ |
| 575 |
snprintf(data->name, sizeof(data->name), "tmag5273x%1u", data->version); |
| 576 |
if (data->version < 1 || data->version > 2) |
| 577 |
dev_warn(data->dev, "Unsupported device %s\n", data->name); |
| 578 |
return 0; |
| 579 |
default: |
| 580 |
/* |
| 581 |
* Only print warning in case of unknown device ID to allow |
| 582 |
* fallback compatible in device tree. |
| 583 |
*/ |
| 584 |
dev_warn(data->dev, "Unknown device ID 0x%x\n", data->devid); |
| 585 |
return 0; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
static void tmag5273_power_down(void *data) |
| 590 |
{ |
| 591 |
tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_SLEEP); |
| 592 |
} |
| 593 |
|
| 594 |
static int tmag5273_probe(struct i2c_client *i2c) |
| 595 |
{ |
| 596 |
struct device *dev = &i2c->dev; |
| 597 |
struct tmag5273_data *data; |
| 598 |
struct iio_dev *indio_dev; |
| 599 |
int ret; |
| 600 |
|
| 601 |
indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); |
| 602 |
if (!indio_dev) |
| 603 |
return -ENOMEM; |
| 604 |
|
| 605 |
data = iio_priv(indio_dev); |
| 606 |
data->dev = dev; |
| 607 |
i2c_set_clientdata(i2c, indio_dev); |
| 608 |
|
| 609 |
data->map = devm_regmap_init_i2c(i2c, &tmag5273_regmap_config); |
| 610 |
if (IS_ERR(data->map)) |
| 611 |
return dev_err_probe(dev, PTR_ERR(data->map), |
| 612 |
"failed to allocate register map\n"); |
| 613 |
|
| 614 |
mutex_init(&data->lock); |
| 615 |
|
| 616 |
ret = devm_regulator_get_enable(dev, "vcc"); |
| 617 |
if (ret) |
| 618 |
return dev_err_probe(dev, ret, "failed to enable regulator\n"); |
| 619 |
|
| 620 |
tmag5273_wake_up(data); |
| 621 |
|
| 622 |
ret = tmag5273_check_device_id(data); |
| 623 |
if (ret) |
| 624 |
return ret; |
| 625 |
|
| 626 |
ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT); |
| 627 |
if (ret) |
| 628 |
return dev_err_probe(dev, ret, "failed to power on device\n"); |
| 629 |
|
| 630 |
/* |
| 631 |
* Register powerdown deferred callback which suspends the chip |
| 632 |
* after module unloaded. |
| 633 |
* |
| 634 |
* TMAG5273 should be in SUSPEND mode in the two cases: |
| 635 |
* 1) When driver is loaded, but we do not have any data or |
| 636 |
* configuration requests to it (we are solving it using |
| 637 |
* autosuspend feature). |
| 638 |
* 2) When driver is unloaded and device is not used (devm action is |
| 639 |
* used in this case). |
| 640 |
*/ |
| 641 |
ret = devm_add_action_or_reset(dev, tmag5273_power_down, data); |
| 642 |
if (ret) |
| 643 |
return ret; |
| 644 |
|
| 645 |
ret = pm_runtime_set_active(dev); |
| 646 |
if (ret < 0) |
| 647 |
return ret; |
| 648 |
|
| 649 |
ret = devm_pm_runtime_enable(dev); |
| 650 |
if (ret) |
| 651 |
return ret; |
| 652 |
|
| 653 |
pm_runtime_get_noresume(dev); |
| 654 |
pm_runtime_set_autosuspend_delay(dev, TMAG5273_AUTOSLEEP_DELAY_MS); |
| 655 |
pm_runtime_use_autosuspend(dev); |
| 656 |
|
| 657 |
tmag5273_read_device_property(data); |
| 658 |
|
| 659 |
ret = tmag5273_chip_init(data); |
| 660 |
if (ret) |
| 661 |
return dev_err_probe(dev, ret, "failed to init device\n"); |
| 662 |
|
| 663 |
indio_dev->info = &tmag5273_info; |
| 664 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 665 |
indio_dev->name = data->name; |
| 666 |
indio_dev->channels = tmag5273_channels; |
| 667 |
indio_dev->num_channels = ARRAY_SIZE(tmag5273_channels); |
| 668 |
|
| 669 |
pm_runtime_put_autosuspend(dev); |
| 670 |
|
| 671 |
ret = devm_iio_device_register(dev, indio_dev); |
| 672 |
if (ret) |
| 673 |
return dev_err_probe(dev, ret, "device register failed\n"); |
| 674 |
|
| 675 |
return 0; |
| 676 |
} |
| 677 |
|
| 678 |
static int tmag5273_runtime_suspend(struct device *dev) |
| 679 |
{ |
| 680 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 681 |
struct tmag5273_data *data = iio_priv(indio_dev); |
| 682 |
int ret; |
| 683 |
|
| 684 |
ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_SLEEP); |
| 685 |
if (ret) |
| 686 |
dev_err(dev, "failed to power off device (%pe)\n", ERR_PTR(ret)); |
| 687 |
|
| 688 |
return ret; |
| 689 |
} |
| 690 |
|
| 691 |
static int tmag5273_runtime_resume(struct device *dev) |
| 692 |
{ |
| 693 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 694 |
struct tmag5273_data *data = iio_priv(indio_dev); |
| 695 |
int ret; |
| 696 |
|
| 697 |
tmag5273_wake_up(data); |
| 698 |
|
| 699 |
ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT); |
| 700 |
if (ret) |
| 701 |
dev_err(dev, "failed to power on device (%pe)\n", ERR_PTR(ret)); |
| 702 |
|
| 703 |
return ret; |
| 704 |
} |
| 705 |
|
| 706 |
static DEFINE_RUNTIME_DEV_PM_OPS(tmag5273_pm_ops, |
| 707 |
tmag5273_runtime_suspend, tmag5273_runtime_resume, |
| 708 |
NULL); |
| 709 |
|
| 710 |
static const struct i2c_device_id tmag5273_id[] = { |
| 711 |
{ "tmag5273" }, |
| 712 |
{ } |
| 713 |
}; |
| 714 |
MODULE_DEVICE_TABLE(i2c, tmag5273_id); |
| 715 |
|
| 716 |
static const struct of_device_id tmag5273_of_match[] = { |
| 717 |
{ .compatible = "ti,tmag5273" }, |
| 718 |
{ } |
| 719 |
}; |
| 720 |
MODULE_DEVICE_TABLE(of, tmag5273_of_match); |
| 721 |
|
| 722 |
static struct i2c_driver tmag5273_driver = { |
| 723 |
.driver = { |
| 724 |
.name = "tmag5273", |
| 725 |
.of_match_table = tmag5273_of_match, |
| 726 |
.pm = pm_ptr(&tmag5273_pm_ops), |
| 727 |
}, |
| 728 |
.probe = tmag5273_probe, |
| 729 |
.id_table = tmag5273_id, |
| 730 |
}; |
| 731 |
module_i2c_driver(tmag5273_driver); |
| 732 |
|
| 733 |
MODULE_DESCRIPTION("TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor driver"); |
| 734 |
MODULE_AUTHOR("Gerald Loacker <[email protected]>"); |
| 735 |
MODULE_LICENSE("GPL"); |