1 // SPDX-License-Identifier: GPL-2.0
4 * x9250.c -- Renesas X9250 potentiometers IIO driver
6 * Copyright 2023 CS GROUP France
8 * Author: Herve Codina <herve.codina@bootlin.com>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/iio/iio.h>
14 #include <linux/limits.h>
15 #include <linux/module.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/slab.h>
18 #include <linux/spi/spi.h>
26 struct spi_device *spi;
27 const struct x9250_cfg *cfg;
28 struct gpio_desc *wp_gpio;
32 #define X9250_CMD_RD_WCR(_p) (0x90 | (_p))
33 #define X9250_CMD_WR_WCR(_p) (0xa0 | (_p))
35 static int x9250_write8(struct x9250 *x9250, u8 cmd, u8 val)
43 return spi_write_then_read(x9250->spi, txbuf, ARRAY_SIZE(txbuf), NULL, 0);
46 static int x9250_read8(struct x9250 *x9250, u8 cmd, u8 *val)
53 return spi_write_then_read(x9250->spi, txbuf, ARRAY_SIZE(txbuf), val, 1);
56 #define X9250_CHANNEL(ch) { \
57 .type = IIO_RESISTANCE, \
61 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
62 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
63 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_RAW), \
66 static const struct iio_chan_spec x9250_channels[] = {
73 static int x9250_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
74 int *val, int *val2, long mask)
76 struct x9250 *x9250 = iio_priv(indio_dev);
77 int ch = chan->channel;
82 case IIO_CHAN_INFO_RAW:
83 ret = x9250_read8(x9250, X9250_CMD_RD_WCR(ch), &v);
89 case IIO_CHAN_INFO_SCALE:
90 *val = 1000 * x9250->cfg->kohms;
92 return IIO_VAL_FRACTIONAL;
98 static int x9250_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
99 const int **vals, int *type, int *length, long mask)
101 static const int range[] = {0, 1, 255}; /* min, step, max */
104 case IIO_CHAN_INFO_RAW:
105 *length = ARRAY_SIZE(range);
108 return IIO_AVAIL_RANGE;
114 static int x9250_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
115 int val, int val2, long mask)
117 struct x9250 *x9250 = iio_priv(indio_dev);
118 int ch = chan->channel;
121 if (mask != IIO_CHAN_INFO_RAW)
124 if (val > U8_MAX || val < 0)
127 gpiod_set_value_cansleep(x9250->wp_gpio, 0);
128 ret = x9250_write8(x9250, X9250_CMD_WR_WCR(ch), val);
129 gpiod_set_value_cansleep(x9250->wp_gpio, 1);
134 static const struct iio_info x9250_info = {
135 .read_raw = x9250_read_raw,
136 .read_avail = x9250_read_avail,
137 .write_raw = x9250_write_raw,
145 static const struct x9250_cfg x9250_cfg[] = {
146 [X9250T] = { .name = "x9250t", .kohms = 100, },
147 [X9250U] = { .name = "x9250u", .kohms = 50, },
150 static const char *const x9250_regulator_names[] = {
156 static int x9250_probe(struct spi_device *spi)
158 struct iio_dev *indio_dev;
162 ret = devm_regulator_bulk_get_enable(&spi->dev, ARRAY_SIZE(x9250_regulator_names),
163 x9250_regulator_names);
165 return dev_err_probe(&spi->dev, ret, "Failed to get regulators\n");
168 * The x9250 needs a 5ms maximum delay after the power-supplies are set
169 * before performing the first write (1ms for the first read).
171 usleep_range(5000, 6000);
173 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*x9250));
177 x9250 = iio_priv(indio_dev);
179 x9250->cfg = spi_get_device_match_data(spi);
180 x9250->wp_gpio = devm_gpiod_get_optional(&spi->dev, "wp", GPIOD_OUT_LOW);
181 if (IS_ERR(x9250->wp_gpio))
182 return dev_err_probe(&spi->dev, PTR_ERR(x9250->wp_gpio),
183 "failed to get wp gpio\n");
185 indio_dev->info = &x9250_info;
186 indio_dev->channels = x9250_channels;
187 indio_dev->num_channels = ARRAY_SIZE(x9250_channels);
188 indio_dev->name = x9250->cfg->name;
190 return devm_iio_device_register(&spi->dev, indio_dev);
193 static const struct of_device_id x9250_of_match[] = {
194 { .compatible = "renesas,x9250t", .data = &x9250_cfg[X9250T]},
195 { .compatible = "renesas,x9250u", .data = &x9250_cfg[X9250U]},
198 MODULE_DEVICE_TABLE(of, x9250_of_match);
200 static const struct spi_device_id x9250_id_table[] = {
201 { "x9250t", (kernel_ulong_t)&x9250_cfg[X9250T] },
202 { "x9250u", (kernel_ulong_t)&x9250_cfg[X9250U] },
205 MODULE_DEVICE_TABLE(spi, x9250_id_table);
207 static struct spi_driver x9250_spi_driver = {
210 .of_match_table = x9250_of_match,
212 .id_table = x9250_id_table,
213 .probe = x9250_probe,
216 module_spi_driver(x9250_spi_driver);
218 MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
219 MODULE_DESCRIPTION("X9250 ALSA SoC driver");
220 MODULE_LICENSE("GPL");