drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / iio / magnetometer / af8133j.c
blobacd291f3e7924c8cd8447bf884453256d74362c2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * af8133j.c - Voltafield AF8133J magnetometer driver
5 * Copyright 2021 Icenowy Zheng <icenowy@aosc.io>
6 * Copyright 2024 Ondřej Jirman <megi@xff.cz>
7 */
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[] = {
36 "avdd",
37 "dvdd",
40 struct af8133j_data {
41 struct i2c_client *client;
42 struct regmap *regmap;
44 * Protect device internal state between starting a measurement
45 * and reading the result.
47 struct mutex mutex;
48 struct iio_mount_matrix orientation;
50 struct gpio_desc *reset_gpiod;
51 struct regulator_bulk_data supplies[ARRAY_SIZE(af8133j_supply_names)];
53 u8 range;
56 enum af8133j_axis {
57 AXIS_X = 0,
58 AXIS_Y,
59 AXIS_Z,
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),
73 { }
76 #define AF8133J_CHANNEL(_si, _axis) { \
77 .type = IIO_MAGN, \
78 .modified = 1, \
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, \
85 .scan_index = _si, \
86 .scan_type = { \
87 .sign = 's', \
88 .realbits = 16, \
89 .storagebits = 16, \
90 .endianness = IIO_LE, \
91 }, \
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;
104 unsigned int val;
105 int ret;
107 ret = regmap_read(data->regmap, AF8133J_REG_PCODE, &val);
108 if (ret) {
109 dev_err(dev, "Error reading product code (%d)\n", ret);
110 return 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 */
118 return 0;
121 static int af8133j_reset(struct af8133j_data *data)
123 struct device *dev = &data->client->dev;
124 int ret;
126 if (data->reset_gpiod) {
127 /* If we have GPIO reset line, use it */
128 gpiod_set_value_cansleep(data->reset_gpiod, 1);
129 udelay(10);
130 gpiod_set_value_cansleep(data->reset_gpiod, 0);
131 } else {
132 /* Otherwise use software reset */
133 ret = regmap_write(data->regmap, AF8133J_REG_SWR,
134 AF8133J_REG_SWR_PERFORM);
135 if (ret) {
136 dev_err(dev, "Failed to reset the chip\n");
137 return ret;
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);
147 if (ret)
148 return ret;
151 return 0;
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;
163 int ret;
165 ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies);
166 if (ret) {
167 dev_err(dev, "Could not enable regulators\n");
168 return ret;
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);
177 if (ret) {
178 af8133j_power_down(data);
179 return ret;
182 return 0;
185 static int af8133j_take_measurement(struct af8133j_data *data)
187 unsigned int val;
188 int ret;
190 ret = regmap_write(data->regmap,
191 AF8133J_REG_STATE, AF8133J_REG_STATE_WORK);
192 if (ret)
193 return ret;
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,
198 500, 1500);
199 if (ret)
200 return ret;
202 ret = regmap_write(data->regmap,
203 AF8133J_REG_STATE, AF8133J_REG_STATE_STBY);
204 if (ret)
205 return ret;
207 return 0;
210 static int af8133j_read_measurement(struct af8133j_data *data, __le16 buf[3])
212 struct device *dev = &data->client->dev;
213 int ret;
215 ret = pm_runtime_resume_and_get(dev);
216 if (ret) {
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.
222 if (ret != -EACCES)
223 dev_err(dev, "Failed to power on (%d)\n", ret);
224 return ret;
227 scoped_guard(mutex, &data->mutex) {
228 ret = af8133j_take_measurement(data);
229 if (ret)
230 goto out_rpm_put;
232 ret = regmap_bulk_read(data->regmap, AF8133J_REG_OUT,
233 buf, sizeof(__le16) * 3);
236 out_rpm_put:
237 pm_runtime_mark_last_busy(dev);
238 pm_runtime_put_autosuspend(dev);
240 return ret;
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);
253 __le16 buf[3];
254 int ret;
256 switch (mask) {
257 case IIO_CHAN_INFO_RAW:
258 ret = af8133j_read_measurement(data, buf);
259 if (ret)
260 return ret;
262 *val = sign_extend32(le16_to_cpu(buf[chan->address]),
263 chan->scan_type.realbits - 1);
264 return IIO_VAL_INT;
265 case IIO_CHAN_INFO_SCALE:
266 *val = 0;
268 if (data->range == AF8133J_REG_RANGE_12G)
269 *val2 = af8133j_scales[0][1];
270 else
271 *val2 = af8133j_scales[1][1];
273 return IIO_VAL_INT_PLUS_NANO;
274 default:
275 return -EINVAL;
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,
282 long mask)
284 switch (mask) {
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;
290 default:
291 return -EINVAL;
295 static int af8133j_set_scale(struct af8133j_data *data,
296 unsigned int val, unsigned int val2)
298 struct device *dev = &data->client->dev;
299 u8 range;
300 int ret = 0;
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;
306 else
307 return -EINVAL;
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);
323 data->range = range;
324 return ret;
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);
333 switch (mask) {
334 case IIO_CHAN_INFO_SCALE:
335 return af8133j_set_scale(data, val, val2);
336 default:
337 return -EINVAL;
341 static int af8133j_write_raw_get_fmt(struct iio_dev *indio_dev,
342 struct iio_chan_spec const *chan,
343 long mask)
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);
361 struct {
362 __le16 values[3];
363 s64 timestamp __aligned(8);
364 } sample;
365 int ret;
367 memset(&sample, 0, sizeof(sample));
369 ret = af8133j_read_measurement(data, sample.values);
370 if (ret)
371 goto out_done;
373 iio_push_to_buffers_with_timestamp(indio_dev, &sample, timestamp);
375 out_done:
376 iio_trigger_notify_done(indio_dev->trig);
378 return IRQ_HANDLED;
381 static const struct regmap_config af8133j_regmap_config = {
382 .name = "af8133j_regmap",
383 .reg_bits = 8,
384 .val_bits = 8,
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;
403 int ret, i;
405 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
406 if (!indio_dev)
407 return -ENOMEM;
409 regmap = devm_regmap_init_i2c(client, &af8133j_regmap_config);
410 if (IS_ERR(regmap))
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),
429 data->supplies);
430 if (ret)
431 return ret;
433 ret = iio_read_mount_matrix(dev, &data->orientation);
434 if (ret)
435 return dev_err_probe(dev, ret, "Failed to read mount matrix\n");
437 ret = af8133j_power_up(data);
438 if (ret)
439 return ret;
441 pm_runtime_set_active(dev);
443 ret = devm_add_action_or_reset(dev, af8133j_power_down_action, data);
444 if (ret)
445 return ret;
447 ret = af8133j_product_check(data);
448 if (ret)
449 return ret;
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);
455 if (ret)
456 return ret;
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);
468 if (ret)
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);
473 if (ret)
474 return dev_err_probe(dev, ret, "Failed to register iio device");
476 return 0;
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);
486 return 0;
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[] = {
509 { "af8133j" },
512 MODULE_DEVICE_TABLE(i2c, af8133j_id);
514 static struct i2c_driver af8133j_driver = {
515 .driver = {
516 .name = "af8133j",
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");