1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the Allegro MicroSystems ALS31300 3-D Linear Hall Effect Sensor
5 * Copyright (c) 2024 Linaro Limited
8 #include <linux/bits.h>
9 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/regmap.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/types.h>
18 #include <linux/units.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
26 * The Allegro MicroSystems ALS31300 has an EEPROM space to configure how
27 * the device works and how the interrupt line behaves.
28 * Only the default setup with external trigger is supported.
30 * While the bindings supports declaring an interrupt line, those
31 * events are not supported.
33 * It should be possible to adapt the driver to the current
34 * device EEPROM setup at runtime.
37 #define ALS31300_EEPROM_CONFIG 0x02
38 #define ALS31300_EEPROM_INTERRUPT 0x03
39 #define ALS31300_EEPROM_CUSTOMER_1 0x0d
40 #define ALS31300_EEPROM_CUSTOMER_2 0x0e
41 #define ALS31300_EEPROM_CUSTOMER_3 0x0f
42 #define ALS31300_VOL_MODE 0x27
43 #define ALS31300_VOL_MODE_LPDCM GENMASK(6, 4)
44 #define ALS31300_LPDCM_INACTIVE_0_5_MS 0
45 #define ALS31300_LPDCM_INACTIVE_1_0_MS 1
46 #define ALS31300_LPDCM_INACTIVE_5_0_MS 2
47 #define ALS31300_LPDCM_INACTIVE_10_0_MS 3
48 #define ALS31300_LPDCM_INACTIVE_50_0_MS 4
49 #define ALS31300_LPDCM_INACTIVE_100_0_MS 5
50 #define ALS31300_LPDCM_INACTIVE_500_0_MS 6
51 #define ALS31300_LPDCM_INACTIVE_1000_0_MS 7
52 #define ALS31300_VOL_MODE_SLEEP GENMASK(1, 0)
53 #define ALS31300_VOL_MODE_ACTIVE_MODE 0
54 #define ALS31300_VOL_MODE_SLEEP_MODE 1
55 #define ALS31300_VOL_MODE_LPDCM_MODE 2
56 #define ALS31300_VOL_MSB 0x28
57 #define ALS31300_VOL_MSB_TEMPERATURE GENMASK(5, 0)
58 #define ALS31300_VOL_MSB_INTERRUPT BIT(6)
59 #define ALS31300_VOL_MSB_NEW_DATA BIT(7)
60 #define ALS31300_VOL_MSB_Z_AXIS GENMASK(15, 8)
61 #define ALS31300_VOL_MSB_Y_AXIS GENMASK(23, 16)
62 #define ALS31300_VOL_MSB_X_AXIS GENMASK(31, 24)
63 #define ALS31300_VOL_LSB 0x29
64 #define ALS31300_VOL_LSB_TEMPERATURE GENMASK(5, 0)
65 #define ALS31300_VOL_LSB_HALL_STATUS GENMASK(7, 7)
66 #define ALS31300_VOL_LSB_Z_AXIS GENMASK(11, 8)
67 #define ALS31300_VOL_LSB_Y_AXIS GENMASK(15, 12)
68 #define ALS31300_VOL_LSB_X_AXIS GENMASK(19, 16)
69 #define ALS31300_VOL_LSB_INTERRUPT_WRITE BIT(20)
70 #define ALS31300_CUSTOMER_ACCESS 0x35
72 #define ALS31300_DATA_X_GET(b) \
73 sign_extend32(FIELD_GET(ALS31300_VOL_MSB_X_AXIS, b[0]) << 4 | \
74 FIELD_GET(ALS31300_VOL_LSB_X_AXIS, b[1]), 11)
75 #define ALS31300_DATA_Y_GET(b) \
76 sign_extend32(FIELD_GET(ALS31300_VOL_MSB_Y_AXIS, b[0]) << 4 | \
77 FIELD_GET(ALS31300_VOL_LSB_Y_AXIS, b[1]), 11)
78 #define ALS31300_DATA_Z_GET(b) \
79 sign_extend32(FIELD_GET(ALS31300_VOL_MSB_Z_AXIS, b[0]) << 4 | \
80 FIELD_GET(ALS31300_VOL_LSB_Z_AXIS, b[1]), 11)
81 #define ALS31300_TEMPERATURE_GET(b) \
82 (FIELD_GET(ALS31300_VOL_MSB_TEMPERATURE, b[0]) << 6 | \
83 FIELD_GET(ALS31300_VOL_LSB_TEMPERATURE, b[1]))
85 enum als31300_channels
{
92 struct als31300_variant_info
{
96 struct als31300_data
{
98 /* protects power on/off the device and access HW */
100 const struct als31300_variant_info
*variant_info
;
104 /* The whole measure is split into 2x32-bit registers, we need to read them both at once */
105 static int als31300_get_measure(struct als31300_data
*data
,
106 u16
*t
, s16
*x
, s16
*y
, s16
*z
)
111 guard(mutex
)(&data
->mutex
);
113 ret
= pm_runtime_resume_and_get(data
->dev
);
118 * Loop until data is valid, new data should have the
119 * ALS31300_VOL_MSB_NEW_DATA bit set to 1.
120 * Max update rate is 2KHz, wait up to 1ms.
122 ret
= read_poll_timeout(regmap_bulk_read
, err
,
123 err
|| FIELD_GET(ALS31300_VOL_MSB_NEW_DATA
, buf
[0]),
124 20, USEC_PER_MSEC
, false,
125 data
->map
, ALS31300_VOL_MSB
, buf
, ARRAY_SIZE(buf
));
126 /* Bail out on read_poll_timeout() error */
130 /* Bail out on regmap_bulk_read() error */
132 dev_err(data
->dev
, "read data failed, error %d\n", ret
);
137 *t
= ALS31300_TEMPERATURE_GET(buf
);
138 *x
= ALS31300_DATA_X_GET(buf
);
139 *y
= ALS31300_DATA_Y_GET(buf
);
140 *z
= ALS31300_DATA_Z_GET(buf
);
143 pm_runtime_mark_last_busy(data
->dev
);
144 pm_runtime_put_autosuspend(data
->dev
);
149 static int als31300_read_raw(struct iio_dev
*indio_dev
,
150 const struct iio_chan_spec
*chan
, int *val
,
151 int *val2
, long mask
)
153 struct als31300_data
*data
= iio_priv(indio_dev
);
159 case IIO_CHAN_INFO_PROCESSED
:
160 case IIO_CHAN_INFO_RAW
:
161 ret
= als31300_get_measure(data
, &t
, &x
, &y
, &z
);
165 switch (chan
->address
) {
181 case IIO_CHAN_INFO_SCALE
:
182 switch (chan
->type
) {
185 * Fractional part of:
186 * 1000 * 302 * (value - 1708)
187 * temp = ----------------------------
189 * to convert temperature in millicelcius.
193 return IIO_VAL_FRACTIONAL
;
196 * Devices are configured in factory
197 * with different sensitivities:
198 * - 500 GAUSS <-> 4 LSB/Gauss
199 * - 1000 GAUSS <-> 2 LSB/Gauss
200 * - 2000 GAUSS <-> 1 LSB/Gauss
201 * with translates by a division of the returned
202 * value to get Gauss value.
203 * The sensitivity cannot be read at runtime
204 * so the value depends on the model compatible
208 *val2
= data
->variant_info
->sensitivity
;
209 return IIO_VAL_FRACTIONAL
;
213 case IIO_CHAN_INFO_OFFSET
:
214 switch (chan
->type
) {
226 static irqreturn_t
als31300_trigger_handler(int irq
, void *p
)
228 struct iio_poll_func
*pf
= p
;
229 struct iio_dev
*indio_dev
= pf
->indio_dev
;
230 struct als31300_data
*data
= iio_priv(indio_dev
);
234 aligned_s64 timestamp
;
240 ret
= als31300_get_measure(data
, &t
, &x
, &y
, &z
);
244 scan
.temperature
= t
;
245 scan
.channels
[0] = x
;
246 scan
.channels
[1] = y
;
247 scan
.channels
[2] = z
;
248 iio_push_to_buffers_with_timestamp(indio_dev
, &scan
,
252 iio_trigger_notify_done(indio_dev
->trig
);
257 #define ALS31300_AXIS_CHANNEL(axis, index) \
261 .channel2 = IIO_MOD_##axis, \
262 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
263 BIT(IIO_CHAN_INFO_SCALE), \
265 .scan_index = index, \
270 .endianness = IIO_CPU, \
274 static const struct iio_chan_spec als31300_channels
[] = {
277 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
278 BIT(IIO_CHAN_INFO_SCALE
) |
279 BIT(IIO_CHAN_INFO_OFFSET
),
280 .address
= TEMPERATURE
,
281 .scan_index
= TEMPERATURE
,
286 .endianness
= IIO_CPU
,
289 ALS31300_AXIS_CHANNEL(X
, AXIS_X
),
290 ALS31300_AXIS_CHANNEL(Y
, AXIS_Y
),
291 ALS31300_AXIS_CHANNEL(Z
, AXIS_Z
),
292 IIO_CHAN_SOFT_TIMESTAMP(4),
295 static const struct iio_info als31300_info
= {
296 .read_raw
= als31300_read_raw
,
299 static int als31300_set_operating_mode(struct als31300_data
*data
,
304 ret
= regmap_update_bits(data
->map
, ALS31300_VOL_MODE
,
305 ALS31300_VOL_MODE_SLEEP
, val
);
307 dev_err(data
->dev
, "failed to set operating mode (%pe)\n", ERR_PTR(ret
));
311 /* The time it takes to exit sleep mode is equivalent to Power-On Delay Time */
312 if (val
== ALS31300_VOL_MODE_ACTIVE_MODE
)
318 static void als31300_power_down(void *data
)
320 als31300_set_operating_mode(data
, ALS31300_VOL_MODE_SLEEP_MODE
);
323 static const struct iio_buffer_setup_ops als31300_setup_ops
= {};
325 static const unsigned long als31300_scan_masks
[] = { GENMASK(3, 0), 0 };
327 static bool als31300_volatile_reg(struct device
*dev
, unsigned int reg
)
329 return reg
== ALS31300_VOL_MSB
|| reg
== ALS31300_VOL_LSB
;
332 static const struct regmap_config als31300_regmap_config
= {
335 .max_register
= ALS31300_CUSTOMER_ACCESS
,
336 .volatile_reg
= als31300_volatile_reg
,
339 static int als31300_probe(struct i2c_client
*i2c
)
341 struct device
*dev
= &i2c
->dev
;
342 struct als31300_data
*data
;
343 struct iio_dev
*indio_dev
;
346 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
350 data
= iio_priv(indio_dev
);
352 i2c_set_clientdata(i2c
, indio_dev
);
354 ret
= devm_mutex_init(dev
, &data
->mutex
);
358 data
->variant_info
= i2c_get_match_data(i2c
);
359 if (!data
->variant_info
)
362 data
->map
= devm_regmap_init_i2c(i2c
, &als31300_regmap_config
);
363 if (IS_ERR(data
->map
))
364 return dev_err_probe(dev
, PTR_ERR(data
->map
),
365 "failed to allocate register map\n");
367 ret
= devm_regulator_get_enable(dev
, "vcc");
369 return dev_err_probe(dev
, ret
, "failed to enable regulator\n");
371 ret
= als31300_set_operating_mode(data
, ALS31300_VOL_MODE_ACTIVE_MODE
);
373 return dev_err_probe(dev
, ret
, "failed to power on device\n");
375 ret
= devm_add_action_or_reset(dev
, als31300_power_down
, data
);
377 return dev_err_probe(dev
, ret
, "failed to add powerdown action\n");
379 indio_dev
->info
= &als31300_info
;
380 indio_dev
->modes
= INDIO_DIRECT_MODE
;
381 indio_dev
->name
= i2c
->name
;
382 indio_dev
->channels
= als31300_channels
;
383 indio_dev
->num_channels
= ARRAY_SIZE(als31300_channels
);
384 indio_dev
->available_scan_masks
= als31300_scan_masks
;
386 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
387 iio_pollfunc_store_time
,
388 als31300_trigger_handler
,
389 &als31300_setup_ops
);
391 return dev_err_probe(dev
, ret
, "iio triggered buffer setup failed\n");
393 ret
= pm_runtime_set_active(dev
);
397 ret
= devm_pm_runtime_enable(dev
);
401 pm_runtime_get_noresume(dev
);
402 pm_runtime_set_autosuspend_delay(dev
, 200);
403 pm_runtime_use_autosuspend(dev
);
405 pm_runtime_mark_last_busy(dev
);
406 pm_runtime_put_autosuspend(dev
);
408 ret
= devm_iio_device_register(dev
, indio_dev
);
410 return dev_err_probe(dev
, ret
, "device register failed\n");
415 static int als31300_runtime_suspend(struct device
*dev
)
417 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
418 struct als31300_data
*data
= iio_priv(indio_dev
);
420 return als31300_set_operating_mode(data
, ALS31300_VOL_MODE_SLEEP_MODE
);
423 static int als31300_runtime_resume(struct device
*dev
)
425 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
426 struct als31300_data
*data
= iio_priv(indio_dev
);
428 return als31300_set_operating_mode(data
, ALS31300_VOL_MODE_ACTIVE_MODE
);
431 static DEFINE_RUNTIME_DEV_PM_OPS(als31300_pm_ops
,
432 als31300_runtime_suspend
, als31300_runtime_resume
,
435 static const struct als31300_variant_info al31300_variant_500
= {
439 static const struct als31300_variant_info al31300_variant_1000
= {
443 static const struct als31300_variant_info al31300_variant_2000
= {
447 static const struct i2c_device_id als31300_id
[] = {
449 .name
= "als31300-500",
450 .driver_data
= (kernel_ulong_t
)&al31300_variant_500
,
453 .name
= "als31300-1000",
454 .driver_data
= (kernel_ulong_t
)&al31300_variant_1000
,
457 .name
= "als31300-2000",
458 .driver_data
= (kernel_ulong_t
)&al31300_variant_2000
,
462 MODULE_DEVICE_TABLE(i2c
, als31300_id
);
464 static const struct of_device_id als31300_of_match
[] = {
466 .compatible
= "allegromicro,als31300-500",
467 .data
= &al31300_variant_500
,
470 .compatible
= "allegromicro,als31300-1000",
471 .data
= &al31300_variant_1000
,
474 .compatible
= "allegromicro,als31300-2000",
475 .data
= &al31300_variant_2000
,
479 MODULE_DEVICE_TABLE(of
, als31300_of_match
);
481 static struct i2c_driver als31300_driver
= {
484 .of_match_table
= als31300_of_match
,
485 .pm
= pm_ptr(&als31300_pm_ops
),
487 .probe
= als31300_probe
,
488 .id_table
= als31300_id
,
490 module_i2c_driver(als31300_driver
);
492 MODULE_LICENSE("GPL");
493 MODULE_DESCRIPTION("ALS31300 3-D Linear Hall Effect Driver");
494 MODULE_AUTHOR("Neil Armstrong <neil.armstrong@linaro.org>");