1 // SPDX-License-Identifier: GPL-2.0-only
3 * af8133j.c - Voltafield AF8133J magnetometer driver
5 * Copyright 2021 Icenowy Zheng <icenowy@aosc.io>
6 * Copyright 2024 Ondřej Jirman <megi@xff.cz>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
21 #define AF8133J_REG_OUT 0x03
22 #define AF8133J_REG_PCODE 0x00
23 #define AF8133J_REG_PCODE_VAL 0x5e
24 #define AF8133J_REG_STATUS 0x02
25 #define AF8133J_REG_STATUS_ACQ BIT(0)
26 #define AF8133J_REG_STATE 0x0a
27 #define AF8133J_REG_STATE_STBY 0x00
28 #define AF8133J_REG_STATE_WORK 0x01
29 #define AF8133J_REG_RANGE 0x0b
30 #define AF8133J_REG_RANGE_22G 0x12
31 #define AF8133J_REG_RANGE_12G 0x34
32 #define AF8133J_REG_SWR 0x11
33 #define AF8133J_REG_SWR_PERFORM 0x81
35 static const char * const af8133j_supply_names
[] = {
41 struct i2c_client
*client
;
42 struct regmap
*regmap
;
44 * Protect device internal state between starting a measurement
45 * and reading the result.
48 struct iio_mount_matrix orientation
;
50 struct gpio_desc
*reset_gpiod
;
51 struct regulator_bulk_data supplies
[ARRAY_SIZE(af8133j_supply_names
)];
62 static struct iio_mount_matrix
*
63 af8133j_get_mount_matrix(struct iio_dev
*indio_dev
,
64 const struct iio_chan_spec
*chan
)
66 struct af8133j_data
*data
= iio_priv(indio_dev
);
68 return &data
->orientation
;
71 static const struct iio_chan_spec_ext_info af8133j_ext_info
[] = {
72 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, af8133j_get_mount_matrix
),
76 #define AF8133J_CHANNEL(_si, _axis) { \
79 .channel2 = IIO_MOD_ ## _axis, \
80 .address = AXIS_ ## _axis, \
81 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
82 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
83 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE), \
84 .ext_info = af8133j_ext_info, \
90 .endianness = IIO_LE, \
94 static const struct iio_chan_spec af8133j_channels
[] = {
95 AF8133J_CHANNEL(0, X
),
96 AF8133J_CHANNEL(1, Y
),
97 AF8133J_CHANNEL(2, Z
),
98 IIO_CHAN_SOFT_TIMESTAMP(3),
101 static int af8133j_product_check(struct af8133j_data
*data
)
103 struct device
*dev
= &data
->client
->dev
;
107 ret
= regmap_read(data
->regmap
, AF8133J_REG_PCODE
, &val
);
109 dev_err(dev
, "Error reading product code (%d)\n", ret
);
113 if (val
!= AF8133J_REG_PCODE_VAL
) {
114 dev_warn(dev
, "Invalid product code (0x%02x)\n", val
);
115 return 0; /* Allow unknown ID so fallback compatibles work */
121 static int af8133j_reset(struct af8133j_data
*data
)
123 struct device
*dev
= &data
->client
->dev
;
126 if (data
->reset_gpiod
) {
127 /* If we have GPIO reset line, use it */
128 gpiod_set_value_cansleep(data
->reset_gpiod
, 1);
130 gpiod_set_value_cansleep(data
->reset_gpiod
, 0);
132 /* Otherwise use software reset */
133 ret
= regmap_write(data
->regmap
, AF8133J_REG_SWR
,
134 AF8133J_REG_SWR_PERFORM
);
136 dev_err(dev
, "Failed to reset the chip\n");
141 /* Wait for reset to finish */
142 usleep_range(1000, 1100);
144 /* Restore range setting */
145 if (data
->range
== AF8133J_REG_RANGE_22G
) {
146 ret
= regmap_write(data
->regmap
, AF8133J_REG_RANGE
, data
->range
);
154 static void af8133j_power_down(struct af8133j_data
*data
)
156 gpiod_set_value_cansleep(data
->reset_gpiod
, 1);
157 regulator_bulk_disable(ARRAY_SIZE(data
->supplies
), data
->supplies
);
160 static int af8133j_power_up(struct af8133j_data
*data
)
162 struct device
*dev
= &data
->client
->dev
;
165 ret
= regulator_bulk_enable(ARRAY_SIZE(data
->supplies
), data
->supplies
);
167 dev_err(dev
, "Could not enable regulators\n");
171 gpiod_set_value_cansleep(data
->reset_gpiod
, 0);
173 /* Wait for power on reset */
174 usleep_range(15000, 16000);
176 ret
= af8133j_reset(data
);
178 af8133j_power_down(data
);
185 static int af8133j_take_measurement(struct af8133j_data
*data
)
190 ret
= regmap_write(data
->regmap
,
191 AF8133J_REG_STATE
, AF8133J_REG_STATE_WORK
);
195 /* The datasheet says "Mesaure Time <1.5ms" */
196 ret
= regmap_read_poll_timeout(data
->regmap
, AF8133J_REG_STATUS
, val
,
197 val
& AF8133J_REG_STATUS_ACQ
,
202 ret
= regmap_write(data
->regmap
,
203 AF8133J_REG_STATE
, AF8133J_REG_STATE_STBY
);
210 static int af8133j_read_measurement(struct af8133j_data
*data
, __le16 buf
[3])
212 struct device
*dev
= &data
->client
->dev
;
215 ret
= pm_runtime_resume_and_get(dev
);
218 * Ignore EACCES because that happens when RPM is disabled
219 * during system sleep, while userspace leave eg. hrtimer
220 * trigger attached and IIO core keeps trying to do measurements.
223 dev_err(dev
, "Failed to power on (%d)\n", ret
);
227 scoped_guard(mutex
, &data
->mutex
) {
228 ret
= af8133j_take_measurement(data
);
232 ret
= regmap_bulk_read(data
->regmap
, AF8133J_REG_OUT
,
233 buf
, sizeof(__le16
) * 3);
237 pm_runtime_mark_last_busy(dev
);
238 pm_runtime_put_autosuspend(dev
);
243 static const int af8133j_scales
[][2] = {
244 [0] = { 0, 366210 }, /* 12 gauss */
245 [1] = { 0, 671386 }, /* 22 gauss */
248 static int af8133j_read_raw(struct iio_dev
*indio_dev
,
249 struct iio_chan_spec
const *chan
, int *val
,
250 int *val2
, long mask
)
252 struct af8133j_data
*data
= iio_priv(indio_dev
);
257 case IIO_CHAN_INFO_RAW
:
258 ret
= af8133j_read_measurement(data
, buf
);
262 *val
= sign_extend32(le16_to_cpu(buf
[chan
->address
]),
263 chan
->scan_type
.realbits
- 1);
265 case IIO_CHAN_INFO_SCALE
:
268 if (data
->range
== AF8133J_REG_RANGE_12G
)
269 *val2
= af8133j_scales
[0][1];
271 *val2
= af8133j_scales
[1][1];
273 return IIO_VAL_INT_PLUS_NANO
;
279 static int af8133j_read_avail(struct iio_dev
*indio_dev
,
280 struct iio_chan_spec
const *chan
,
281 const int **vals
, int *type
, int *length
,
285 case IIO_CHAN_INFO_SCALE
:
286 *vals
= (const int *)af8133j_scales
;
287 *length
= ARRAY_SIZE(af8133j_scales
) * 2;
288 *type
= IIO_VAL_INT_PLUS_NANO
;
289 return IIO_AVAIL_LIST
;
295 static int af8133j_set_scale(struct af8133j_data
*data
,
296 unsigned int val
, unsigned int val2
)
298 struct device
*dev
= &data
->client
->dev
;
302 if (af8133j_scales
[0][0] == val
&& af8133j_scales
[0][1] == val2
)
303 range
= AF8133J_REG_RANGE_12G
;
304 else if (af8133j_scales
[1][0] == val
&& af8133j_scales
[1][1] == val2
)
305 range
= AF8133J_REG_RANGE_22G
;
309 pm_runtime_disable(dev
);
312 * When suspended, just store the new range to data->range to be
313 * applied later during power up.
315 if (!pm_runtime_status_suspended(dev
)) {
316 scoped_guard(mutex
, &data
->mutex
)
317 ret
= regmap_write(data
->regmap
,
318 AF8133J_REG_RANGE
, range
);
321 pm_runtime_enable(dev
);
327 static int af8133j_write_raw(struct iio_dev
*indio_dev
,
328 struct iio_chan_spec
const *chan
,
329 int val
, int val2
, long mask
)
331 struct af8133j_data
*data
= iio_priv(indio_dev
);
334 case IIO_CHAN_INFO_SCALE
:
335 return af8133j_set_scale(data
, val
, val2
);
341 static int af8133j_write_raw_get_fmt(struct iio_dev
*indio_dev
,
342 struct iio_chan_spec
const *chan
,
345 return IIO_VAL_INT_PLUS_NANO
;
348 static const struct iio_info af8133j_info
= {
349 .read_raw
= af8133j_read_raw
,
350 .read_avail
= af8133j_read_avail
,
351 .write_raw
= af8133j_write_raw
,
352 .write_raw_get_fmt
= af8133j_write_raw_get_fmt
,
355 static irqreturn_t
af8133j_trigger_handler(int irq
, void *p
)
357 struct iio_poll_func
*pf
= p
;
358 struct iio_dev
*indio_dev
= pf
->indio_dev
;
359 struct af8133j_data
*data
= iio_priv(indio_dev
);
360 s64 timestamp
= iio_get_time_ns(indio_dev
);
363 s64 timestamp
__aligned(8);
367 memset(&sample
, 0, sizeof(sample
));
369 ret
= af8133j_read_measurement(data
, sample
.values
);
373 iio_push_to_buffers_with_timestamp(indio_dev
, &sample
, timestamp
);
376 iio_trigger_notify_done(indio_dev
->trig
);
381 static const struct regmap_config af8133j_regmap_config
= {
382 .name
= "af8133j_regmap",
385 .max_register
= AF8133J_REG_SWR
,
386 .cache_type
= REGCACHE_NONE
,
389 static void af8133j_power_down_action(void *ptr
)
391 struct af8133j_data
*data
= ptr
;
393 if (!pm_runtime_status_suspended(&data
->client
->dev
))
394 af8133j_power_down(data
);
397 static int af8133j_probe(struct i2c_client
*client
)
399 struct device
*dev
= &client
->dev
;
400 struct af8133j_data
*data
;
401 struct iio_dev
*indio_dev
;
402 struct regmap
*regmap
;
405 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
409 regmap
= devm_regmap_init_i2c(client
, &af8133j_regmap_config
);
411 return dev_err_probe(dev
, PTR_ERR(regmap
),
412 "regmap initialization failed\n");
414 data
= iio_priv(indio_dev
);
415 i2c_set_clientdata(client
, indio_dev
);
416 data
->client
= client
;
417 data
->regmap
= regmap
;
418 data
->range
= AF8133J_REG_RANGE_12G
;
419 mutex_init(&data
->mutex
);
421 data
->reset_gpiod
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
422 if (IS_ERR(data
->reset_gpiod
))
423 return dev_err_probe(dev
, PTR_ERR(data
->reset_gpiod
),
424 "Failed to get reset gpio\n");
426 for (i
= 0; i
< ARRAY_SIZE(af8133j_supply_names
); i
++)
427 data
->supplies
[i
].supply
= af8133j_supply_names
[i
];
428 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(data
->supplies
),
433 ret
= iio_read_mount_matrix(dev
, &data
->orientation
);
435 return dev_err_probe(dev
, ret
, "Failed to read mount matrix\n");
437 ret
= af8133j_power_up(data
);
441 pm_runtime_set_active(dev
);
443 ret
= devm_add_action_or_reset(dev
, af8133j_power_down_action
, data
);
447 ret
= af8133j_product_check(data
);
451 pm_runtime_get_noresume(dev
);
452 pm_runtime_use_autosuspend(dev
);
453 pm_runtime_set_autosuspend_delay(dev
, 500);
454 ret
= devm_pm_runtime_enable(dev
);
458 pm_runtime_put_autosuspend(dev
);
460 indio_dev
->info
= &af8133j_info
;
461 indio_dev
->name
= "af8133j";
462 indio_dev
->channels
= af8133j_channels
;
463 indio_dev
->num_channels
= ARRAY_SIZE(af8133j_channels
);
464 indio_dev
->modes
= INDIO_DIRECT_MODE
;
466 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
,
467 &af8133j_trigger_handler
, NULL
);
469 return dev_err_probe(&client
->dev
, ret
,
470 "Failed to setup iio triggered buffer\n");
472 ret
= devm_iio_device_register(dev
, indio_dev
);
474 return dev_err_probe(dev
, ret
, "Failed to register iio device");
479 static int af8133j_runtime_suspend(struct device
*dev
)
481 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
482 struct af8133j_data
*data
= iio_priv(indio_dev
);
484 af8133j_power_down(data
);
489 static int af8133j_runtime_resume(struct device
*dev
)
491 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
492 struct af8133j_data
*data
= iio_priv(indio_dev
);
494 return af8133j_power_up(data
);
497 static const struct dev_pm_ops af8133j_pm_ops
= {
498 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
, pm_runtime_force_resume
)
499 RUNTIME_PM_OPS(af8133j_runtime_suspend
, af8133j_runtime_resume
, NULL
)
502 static const struct of_device_id af8133j_of_match
[] = {
503 { .compatible
= "voltafield,af8133j", },
506 MODULE_DEVICE_TABLE(of
, af8133j_of_match
);
508 static const struct i2c_device_id af8133j_id
[] = {
512 MODULE_DEVICE_TABLE(i2c
, af8133j_id
);
514 static struct i2c_driver af8133j_driver
= {
517 .of_match_table
= af8133j_of_match
,
518 .pm
= pm_ptr(&af8133j_pm_ops
),
520 .probe
= af8133j_probe
,
521 .id_table
= af8133j_id
,
524 module_i2c_driver(af8133j_driver
);
526 MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
527 MODULE_AUTHOR("Ondřej Jirman <megi@xff.cz>");
528 MODULE_DESCRIPTION("Voltafield AF8133J magnetic sensor driver");
529 MODULE_LICENSE("GPL");