drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / iio / magnetometer / als31300.c
blob87b60c4e81fa1c9d8d6e8b083c5bcb605baf8ee9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver for the Allegro MicroSystems ALS31300 3-D Linear Hall Effect Sensor
5 * Copyright (c) 2024 Linaro Limited
6 */
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>
14 #include <linux/pm.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 {
86 TEMPERATURE = 0,
87 AXIS_X,
88 AXIS_Y,
89 AXIS_Z,
92 struct als31300_variant_info {
93 u8 sensitivity;
96 struct als31300_data {
97 struct device *dev;
98 /* protects power on/off the device and access HW */
99 struct mutex mutex;
100 const struct als31300_variant_info *variant_info;
101 struct regmap *map;
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)
108 u32 buf[2];
109 int ret, err;
111 guard(mutex)(&data->mutex);
113 ret = pm_runtime_resume_and_get(data->dev);
114 if (ret)
115 return ret;
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 */
127 if (ret)
128 goto out;
130 /* Bail out on regmap_bulk_read() error */
131 if (err) {
132 dev_err(data->dev, "read data failed, error %d\n", ret);
133 ret = err;
134 goto out;
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);
142 out:
143 pm_runtime_mark_last_busy(data->dev);
144 pm_runtime_put_autosuspend(data->dev);
146 return ret;
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);
154 s16 x, y, z;
155 u16 t;
156 int ret;
158 switch (mask) {
159 case IIO_CHAN_INFO_PROCESSED:
160 case IIO_CHAN_INFO_RAW:
161 ret = als31300_get_measure(data, &t, &x, &y, &z);
162 if (ret)
163 return ret;
165 switch (chan->address) {
166 case TEMPERATURE:
167 *val = t;
168 return IIO_VAL_INT;
169 case AXIS_X:
170 *val = x;
171 return IIO_VAL_INT;
172 case AXIS_Y:
173 *val = y;
174 return IIO_VAL_INT;
175 case AXIS_Z:
176 *val = z;
177 return IIO_VAL_INT;
178 default:
179 return -EINVAL;
181 case IIO_CHAN_INFO_SCALE:
182 switch (chan->type) {
183 case IIO_TEMP:
185 * Fractional part of:
186 * 1000 * 302 * (value - 1708)
187 * temp = ----------------------------
188 * 4096
189 * to convert temperature in millicelcius.
191 *val = MILLI * 302;
192 *val2 = 4096;
193 return IIO_VAL_FRACTIONAL;
194 case IIO_MAGN:
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
205 * or device id.
207 *val = 1;
208 *val2 = data->variant_info->sensitivity;
209 return IIO_VAL_FRACTIONAL;
210 default:
211 return -EINVAL;
213 case IIO_CHAN_INFO_OFFSET:
214 switch (chan->type) {
215 case IIO_TEMP:
216 *val = -1708;
217 return IIO_VAL_INT;
218 default:
219 return -EINVAL;
221 default:
222 return -EINVAL;
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);
231 struct {
232 u16 temperature;
233 s16 channels[3];
234 aligned_s64 timestamp;
235 } scan;
236 s16 x, y, z;
237 int ret;
238 u16 t;
240 ret = als31300_get_measure(data, &t, &x, &y, &z);
241 if (ret)
242 goto trigger_out;
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,
249 pf->timestamp);
251 trigger_out:
252 iio_trigger_notify_done(indio_dev->trig);
254 return IRQ_HANDLED;
257 #define ALS31300_AXIS_CHANNEL(axis, index) \
259 .type = IIO_MAGN, \
260 .modified = 1, \
261 .channel2 = IIO_MOD_##axis, \
262 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
263 BIT(IIO_CHAN_INFO_SCALE), \
264 .address = index, \
265 .scan_index = index, \
266 .scan_type = { \
267 .sign = 's', \
268 .realbits = 12, \
269 .storagebits = 16, \
270 .endianness = IIO_CPU, \
271 }, \
274 static const struct iio_chan_spec als31300_channels[] = {
276 .type = IIO_TEMP,
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,
282 .scan_type = {
283 .sign = 'u',
284 .realbits = 16,
285 .storagebits = 16,
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,
300 unsigned int val)
302 int ret;
304 ret = regmap_update_bits(data->map, ALS31300_VOL_MODE,
305 ALS31300_VOL_MODE_SLEEP, val);
306 if (ret) {
307 dev_err(data->dev, "failed to set operating mode (%pe)\n", ERR_PTR(ret));
308 return 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)
313 fsleep(600);
315 return 0;
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 = {
333 .reg_bits = 8,
334 .val_bits = 32,
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;
344 int ret;
346 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
347 if (!indio_dev)
348 return -ENOMEM;
350 data = iio_priv(indio_dev);
351 data->dev = dev;
352 i2c_set_clientdata(i2c, indio_dev);
354 ret = devm_mutex_init(dev, &data->mutex);
355 if (ret)
356 return ret;
358 data->variant_info = i2c_get_match_data(i2c);
359 if (!data->variant_info)
360 return -EINVAL;
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");
368 if (ret)
369 return dev_err_probe(dev, ret, "failed to enable regulator\n");
371 ret = als31300_set_operating_mode(data, ALS31300_VOL_MODE_ACTIVE_MODE);
372 if (ret)
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);
376 if (ret)
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);
390 if (ret < 0)
391 return dev_err_probe(dev, ret, "iio triggered buffer setup failed\n");
393 ret = pm_runtime_set_active(dev);
394 if (ret < 0)
395 return ret;
397 ret = devm_pm_runtime_enable(dev);
398 if (ret)
399 return ret;
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);
409 if (ret)
410 return dev_err_probe(dev, ret, "device register failed\n");
412 return 0;
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,
433 NULL);
435 static const struct als31300_variant_info al31300_variant_500 = {
436 .sensitivity = 4,
439 static const struct als31300_variant_info al31300_variant_1000 = {
440 .sensitivity = 2,
443 static const struct als31300_variant_info al31300_variant_2000 = {
444 .sensitivity = 1,
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,
460 { /* sentinel */ }
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,
477 { /* sentinel */ }
479 MODULE_DEVICE_TABLE(of, als31300_of_match);
481 static struct i2c_driver als31300_driver = {
482 .driver = {
483 .name = "als31300",
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>");