xref: /linux/drivers/iio/magnetometer/als31300.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the Allegro MicroSystems ALS31300 3-D Linear Hall Effect Sensor
4  *
5  * Copyright (c) 2024 Linaro Limited
6  */
7 
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>
19 
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>
24 
25 /*
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.
29  *
30  * While the bindings supports declaring an interrupt line, those
31  * events are not supported.
32  *
33  * It should be possible to adapt the driver to the current
34  * device EEPROM setup at runtime.
35  */
36 
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
71 
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]))
84 
85 enum als31300_channels {
86 	TEMPERATURE = 0,
87 	AXIS_X,
88 	AXIS_Y,
89 	AXIS_Z,
90 };
91 
92 struct als31300_variant_info {
93 	u8 sensitivity;
94 };
95 
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;
102 };
103 
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)
107 {
108 	u32 buf[2];
109 	int ret, err;
110 
111 	guard(mutex)(&data->mutex);
112 
113 	ret = pm_runtime_resume_and_get(data->dev);
114 	if (ret)
115 		return ret;
116 
117 	/*
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.
121 	 */
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;
129 
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;
135 	}
136 
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);
141 
142 out:
143 	pm_runtime_mark_last_busy(data->dev);
144 	pm_runtime_put_autosuspend(data->dev);
145 
146 	return ret;
147 }
148 
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)
152 {
153 	struct als31300_data *data = iio_priv(indio_dev);
154 	s16 x, y, z;
155 	u16 t;
156 	int ret;
157 
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;
164 
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;
180 		}
181 	case IIO_CHAN_INFO_SCALE:
182 		switch (chan->type) {
183 		case IIO_TEMP:
184 			/*
185 			 * Fractional part of:
186 			 *         1000 * 302 * (value - 1708)
187 			 * temp = ----------------------------
188 			 *             4096
189 			 * to convert temperature in millicelcius.
190 			 */
191 			*val = MILLI * 302;
192 			*val2 = 4096;
193 			return IIO_VAL_FRACTIONAL;
194 		case IIO_MAGN:
195 			/*
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.
206 			 */
207 			*val = 1;
208 			*val2 = data->variant_info->sensitivity;
209 			return IIO_VAL_FRACTIONAL;
210 		default:
211 			return -EINVAL;
212 		}
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;
220 		}
221 	default:
222 		return -EINVAL;
223 	}
224 }
225 
226 static irqreturn_t als31300_trigger_handler(int irq, void *p)
227 {
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;
239 
240 	ret = als31300_get_measure(data, &t, &x, &y, &z);
241 	if (ret)
242 		goto trigger_out;
243 
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);
250 
251 trigger_out:
252 	iio_trigger_notify_done(indio_dev->trig);
253 
254 	return IRQ_HANDLED;
255 }
256 
257 #define ALS31300_AXIS_CHANNEL(axis, index)				     \
258 	{								     \
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 		},							     \
272 	}
273 
274 static const struct iio_chan_spec als31300_channels[] = {
275 	{
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,
287 		},
288 	},
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),
293 };
294 
295 static const struct iio_info als31300_info = {
296 	.read_raw = als31300_read_raw,
297 };
298 
299 static int als31300_set_operating_mode(struct als31300_data *data,
300 				       unsigned int val)
301 {
302 	int ret;
303 
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;
309 	}
310 
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);
314 
315 	return 0;
316 }
317 
318 static void als31300_power_down(void *data)
319 {
320 	als31300_set_operating_mode(data, ALS31300_VOL_MODE_SLEEP_MODE);
321 }
322 
323 static const struct iio_buffer_setup_ops als31300_setup_ops = {};
324 
325 static const unsigned long als31300_scan_masks[] = { GENMASK(3, 0), 0 };
326 
327 static bool als31300_volatile_reg(struct device *dev, unsigned int reg)
328 {
329 	return reg == ALS31300_VOL_MSB || reg == ALS31300_VOL_LSB;
330 }
331 
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,
337 };
338 
339 static int als31300_probe(struct i2c_client *i2c)
340 {
341 	struct device *dev = &i2c->dev;
342 	struct als31300_data *data;
343 	struct iio_dev *indio_dev;
344 	int ret;
345 
346 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
347 	if (!indio_dev)
348 		return -ENOMEM;
349 
350 	data = iio_priv(indio_dev);
351 	data->dev = dev;
352 	i2c_set_clientdata(i2c, indio_dev);
353 
354 	ret = devm_mutex_init(dev, &data->mutex);
355 	if (ret)
356 		return ret;
357 
358 	data->variant_info = i2c_get_match_data(i2c);
359 	if (!data->variant_info)
360 		return -EINVAL;
361 
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");
366 
367 	ret = devm_regulator_get_enable(dev, "vcc");
368 	if (ret)
369 		return dev_err_probe(dev, ret, "failed to enable regulator\n");
370 
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");
374 
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");
378 
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;
385 
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");
392 
393 	ret = pm_runtime_set_active(dev);
394 	if (ret < 0)
395 		return ret;
396 
397 	ret = devm_pm_runtime_enable(dev);
398 	if (ret)
399 		return ret;
400 
401 	pm_runtime_get_noresume(dev);
402 	pm_runtime_set_autosuspend_delay(dev, 200);
403 	pm_runtime_use_autosuspend(dev);
404 
405 	pm_runtime_mark_last_busy(dev);
406 	pm_runtime_put_autosuspend(dev);
407 
408 	ret = devm_iio_device_register(dev, indio_dev);
409 	if (ret)
410 		return dev_err_probe(dev, ret, "device register failed\n");
411 
412 	return 0;
413 }
414 
415 static int als31300_runtime_suspend(struct device *dev)
416 {
417 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
418 	struct als31300_data *data = iio_priv(indio_dev);
419 
420 	return als31300_set_operating_mode(data, ALS31300_VOL_MODE_SLEEP_MODE);
421 }
422 
423 static int als31300_runtime_resume(struct device *dev)
424 {
425 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
426 	struct als31300_data *data = iio_priv(indio_dev);
427 
428 	return als31300_set_operating_mode(data, ALS31300_VOL_MODE_ACTIVE_MODE);
429 }
430 
431 static DEFINE_RUNTIME_DEV_PM_OPS(als31300_pm_ops,
432 				 als31300_runtime_suspend, als31300_runtime_resume,
433 				 NULL);
434 
435 static const struct als31300_variant_info al31300_variant_500 = {
436 	.sensitivity = 4,
437 };
438 
439 static const struct als31300_variant_info al31300_variant_1000 = {
440 	.sensitivity = 2,
441 };
442 
443 static const struct als31300_variant_info al31300_variant_2000 = {
444 	.sensitivity = 1,
445 };
446 
447 static const struct i2c_device_id als31300_id[] = {
448 	{
449 		.name = "als31300-500",
450 		.driver_data = (kernel_ulong_t)&al31300_variant_500,
451 	},
452 	{
453 		.name = "als31300-1000",
454 		.driver_data = (kernel_ulong_t)&al31300_variant_1000,
455 	},
456 	{
457 		.name = "als31300-2000",
458 		.driver_data = (kernel_ulong_t)&al31300_variant_2000,
459 	},
460 	{ /* sentinel */ }
461 };
462 MODULE_DEVICE_TABLE(i2c, als31300_id);
463 
464 static const struct of_device_id als31300_of_match[] = {
465 	{
466 		.compatible = "allegromicro,als31300-500",
467 		.data = &al31300_variant_500,
468 	},
469 	{
470 		.compatible = "allegromicro,als31300-1000",
471 		.data = &al31300_variant_1000,
472 	},
473 	{
474 		.compatible = "allegromicro,als31300-2000",
475 		.data = &al31300_variant_2000,
476 	},
477 	{ /* sentinel */ }
478 };
479 MODULE_DEVICE_TABLE(of, als31300_of_match);
480 
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),
486 	},
487 	.probe = als31300_probe,
488 	.id_table = als31300_id,
489 };
490 module_i2c_driver(als31300_driver);
491 
492 MODULE_LICENSE("GPL");
493 MODULE_DESCRIPTION("ALS31300 3-D Linear Hall Effect Driver");
494 MODULE_AUTHOR("Neil Armstrong <neil.armstrong@linaro.org>");
495