1a7118662SVincent Pelletier // SPDX-License-Identifier: GPL-2.0
2a7118662SVincent Pelletier /*
3a7118662SVincent Pelletier * ADC driver for Basin Cove PMIC
4a7118662SVincent Pelletier *
5a7118662SVincent Pelletier * Copyright (C) 2012 Intel Corporation
6a7118662SVincent Pelletier * Author: Bin Yang <bin.yang@intel.com>
7a7118662SVincent Pelletier *
8a7118662SVincent Pelletier * Rewritten for upstream by:
9a7118662SVincent Pelletier * Vincent Pelletier <plr.vincent@gmail.com>
10a7118662SVincent Pelletier * Andy Shevchenko <andriy.shevchenko@linux.intel.com>
11a7118662SVincent Pelletier */
12a7118662SVincent Pelletier
13a7118662SVincent Pelletier #include <linux/bitops.h>
14a7118662SVincent Pelletier #include <linux/completion.h>
15a7118662SVincent Pelletier #include <linux/interrupt.h>
16a7118662SVincent Pelletier #include <linux/mfd/intel_soc_pmic.h>
17a7118662SVincent Pelletier #include <linux/mfd/intel_soc_pmic_mrfld.h>
18*bf49a46bSAndy Shevchenko #include <linux/mod_devicetable.h>
19a7118662SVincent Pelletier #include <linux/module.h>
20a7118662SVincent Pelletier #include <linux/mutex.h>
21a7118662SVincent Pelletier #include <linux/platform_device.h>
22a7118662SVincent Pelletier #include <linux/regmap.h>
23a7118662SVincent Pelletier
24a7118662SVincent Pelletier #include <linux/iio/driver.h>
25a7118662SVincent Pelletier #include <linux/iio/iio.h>
26a7118662SVincent Pelletier #include <linux/iio/machine.h>
27a7118662SVincent Pelletier
28a7118662SVincent Pelletier #include <asm/unaligned.h>
29a7118662SVincent Pelletier
30a7118662SVincent Pelletier #define BCOVE_GPADCREQ 0xDC
31a7118662SVincent Pelletier #define BCOVE_GPADCREQ_BUSY BIT(0)
32a7118662SVincent Pelletier #define BCOVE_GPADCREQ_IRQEN BIT(1)
33a7118662SVincent Pelletier
34a7118662SVincent Pelletier #define BCOVE_ADCIRQ_ALL ( \
35a7118662SVincent Pelletier BCOVE_ADCIRQ_BATTEMP | \
36a7118662SVincent Pelletier BCOVE_ADCIRQ_SYSTEMP | \
37a7118662SVincent Pelletier BCOVE_ADCIRQ_BATTID | \
38a7118662SVincent Pelletier BCOVE_ADCIRQ_VIBATT | \
39a7118662SVincent Pelletier BCOVE_ADCIRQ_CCTICK)
40a7118662SVincent Pelletier
41a7118662SVincent Pelletier #define BCOVE_ADC_TIMEOUT msecs_to_jiffies(1000)
42a7118662SVincent Pelletier
43a7118662SVincent Pelletier static const u8 mrfld_adc_requests[] = {
44a7118662SVincent Pelletier BCOVE_ADCIRQ_VIBATT,
45a7118662SVincent Pelletier BCOVE_ADCIRQ_BATTID,
46a7118662SVincent Pelletier BCOVE_ADCIRQ_VIBATT,
47a7118662SVincent Pelletier BCOVE_ADCIRQ_SYSTEMP,
48a7118662SVincent Pelletier BCOVE_ADCIRQ_BATTEMP,
49a7118662SVincent Pelletier BCOVE_ADCIRQ_BATTEMP,
50a7118662SVincent Pelletier BCOVE_ADCIRQ_SYSTEMP,
51a7118662SVincent Pelletier BCOVE_ADCIRQ_SYSTEMP,
52a7118662SVincent Pelletier BCOVE_ADCIRQ_SYSTEMP,
53a7118662SVincent Pelletier };
54a7118662SVincent Pelletier
55a7118662SVincent Pelletier struct mrfld_adc {
56a7118662SVincent Pelletier struct regmap *regmap;
57a7118662SVincent Pelletier struct completion completion;
58a7118662SVincent Pelletier /* Lock to protect the IPC transfers */
59a7118662SVincent Pelletier struct mutex lock;
60a7118662SVincent Pelletier };
61a7118662SVincent Pelletier
mrfld_adc_thread_isr(int irq,void * data)62a7118662SVincent Pelletier static irqreturn_t mrfld_adc_thread_isr(int irq, void *data)
63a7118662SVincent Pelletier {
64a7118662SVincent Pelletier struct iio_dev *indio_dev = data;
65a7118662SVincent Pelletier struct mrfld_adc *adc = iio_priv(indio_dev);
66a7118662SVincent Pelletier
67a7118662SVincent Pelletier complete(&adc->completion);
68a7118662SVincent Pelletier return IRQ_HANDLED;
69a7118662SVincent Pelletier }
70a7118662SVincent Pelletier
mrfld_adc_single_conv(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * result)71a7118662SVincent Pelletier static int mrfld_adc_single_conv(struct iio_dev *indio_dev,
72a7118662SVincent Pelletier struct iio_chan_spec const *chan,
73a7118662SVincent Pelletier int *result)
74a7118662SVincent Pelletier {
75a7118662SVincent Pelletier struct mrfld_adc *adc = iio_priv(indio_dev);
76a7118662SVincent Pelletier struct regmap *regmap = adc->regmap;
77a7118662SVincent Pelletier unsigned int req;
78a7118662SVincent Pelletier long timeout;
794be590e3SAndy Shevchenko __be16 value;
80a7118662SVincent Pelletier int ret;
81a7118662SVincent Pelletier
82a7118662SVincent Pelletier reinit_completion(&adc->completion);
83a7118662SVincent Pelletier
84a7118662SVincent Pelletier regmap_update_bits(regmap, BCOVE_MADCIRQ, BCOVE_ADCIRQ_ALL, 0);
85a7118662SVincent Pelletier regmap_update_bits(regmap, BCOVE_MIRQLVL1, BCOVE_LVL1_ADC, 0);
86a7118662SVincent Pelletier
87a7118662SVincent Pelletier ret = regmap_read_poll_timeout(regmap, BCOVE_GPADCREQ, req,
88a7118662SVincent Pelletier !(req & BCOVE_GPADCREQ_BUSY),
89a7118662SVincent Pelletier 2000, 1000000);
90a7118662SVincent Pelletier if (ret)
91a7118662SVincent Pelletier goto done;
92a7118662SVincent Pelletier
93a7118662SVincent Pelletier req = mrfld_adc_requests[chan->channel];
94a7118662SVincent Pelletier ret = regmap_write(regmap, BCOVE_GPADCREQ, BCOVE_GPADCREQ_IRQEN | req);
95a7118662SVincent Pelletier if (ret)
96a7118662SVincent Pelletier goto done;
97a7118662SVincent Pelletier
98a7118662SVincent Pelletier timeout = wait_for_completion_interruptible_timeout(&adc->completion,
99a7118662SVincent Pelletier BCOVE_ADC_TIMEOUT);
100a7118662SVincent Pelletier if (timeout < 0) {
101a7118662SVincent Pelletier ret = timeout;
102a7118662SVincent Pelletier goto done;
103a7118662SVincent Pelletier }
104a7118662SVincent Pelletier if (timeout == 0) {
105a7118662SVincent Pelletier ret = -ETIMEDOUT;
106a7118662SVincent Pelletier goto done;
107a7118662SVincent Pelletier }
108a7118662SVincent Pelletier
1094be590e3SAndy Shevchenko ret = regmap_bulk_read(regmap, chan->address, &value, sizeof(value));
110a7118662SVincent Pelletier if (ret)
111a7118662SVincent Pelletier goto done;
112a7118662SVincent Pelletier
1134be590e3SAndy Shevchenko *result = be16_to_cpu(value);
114a7118662SVincent Pelletier ret = IIO_VAL_INT;
115a7118662SVincent Pelletier
116a7118662SVincent Pelletier done:
117a7118662SVincent Pelletier regmap_update_bits(regmap, BCOVE_MIRQLVL1, BCOVE_LVL1_ADC, 0xff);
118a7118662SVincent Pelletier regmap_update_bits(regmap, BCOVE_MADCIRQ, BCOVE_ADCIRQ_ALL, 0xff);
119a7118662SVincent Pelletier
120a7118662SVincent Pelletier return ret;
121a7118662SVincent Pelletier }
122a7118662SVincent Pelletier
mrfld_adc_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)123a7118662SVincent Pelletier static int mrfld_adc_read_raw(struct iio_dev *indio_dev,
124a7118662SVincent Pelletier struct iio_chan_spec const *chan,
125a7118662SVincent Pelletier int *val, int *val2, long mask)
126a7118662SVincent Pelletier {
127a7118662SVincent Pelletier struct mrfld_adc *adc = iio_priv(indio_dev);
128a7118662SVincent Pelletier int ret;
129a7118662SVincent Pelletier
130a7118662SVincent Pelletier switch (mask) {
131a7118662SVincent Pelletier case IIO_CHAN_INFO_RAW:
132a7118662SVincent Pelletier mutex_lock(&adc->lock);
133a7118662SVincent Pelletier ret = mrfld_adc_single_conv(indio_dev, chan, val);
134a7118662SVincent Pelletier mutex_unlock(&adc->lock);
135a7118662SVincent Pelletier return ret;
136a7118662SVincent Pelletier default:
137a7118662SVincent Pelletier return -EINVAL;
138a7118662SVincent Pelletier }
139a7118662SVincent Pelletier }
140a7118662SVincent Pelletier
141a7118662SVincent Pelletier static const struct iio_info mrfld_adc_iio_info = {
142a7118662SVincent Pelletier .read_raw = &mrfld_adc_read_raw,
143a7118662SVincent Pelletier };
144a7118662SVincent Pelletier
145a7118662SVincent Pelletier #define BCOVE_ADC_CHANNEL(_type, _channel, _datasheet_name, _address) \
146a7118662SVincent Pelletier { \
147a7118662SVincent Pelletier .indexed = 1, \
148a7118662SVincent Pelletier .type = _type, \
149a7118662SVincent Pelletier .channel = _channel, \
150a7118662SVincent Pelletier .address = _address, \
151a7118662SVincent Pelletier .datasheet_name = _datasheet_name, \
152a7118662SVincent Pelletier .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
153a7118662SVincent Pelletier }
154a7118662SVincent Pelletier
155a7118662SVincent Pelletier static const struct iio_chan_spec mrfld_adc_channels[] = {
156a7118662SVincent Pelletier BCOVE_ADC_CHANNEL(IIO_VOLTAGE, 0, "CH0", 0xE9),
157a7118662SVincent Pelletier BCOVE_ADC_CHANNEL(IIO_RESISTANCE, 1, "CH1", 0xEB),
158a7118662SVincent Pelletier BCOVE_ADC_CHANNEL(IIO_CURRENT, 2, "CH2", 0xED),
159a7118662SVincent Pelletier BCOVE_ADC_CHANNEL(IIO_TEMP, 3, "CH3", 0xCC),
160a7118662SVincent Pelletier BCOVE_ADC_CHANNEL(IIO_TEMP, 4, "CH4", 0xC8),
161a7118662SVincent Pelletier BCOVE_ADC_CHANNEL(IIO_TEMP, 5, "CH5", 0xCA),
162a7118662SVincent Pelletier BCOVE_ADC_CHANNEL(IIO_TEMP, 6, "CH6", 0xC2),
163a7118662SVincent Pelletier BCOVE_ADC_CHANNEL(IIO_TEMP, 7, "CH7", 0xC4),
164a7118662SVincent Pelletier BCOVE_ADC_CHANNEL(IIO_TEMP, 8, "CH8", 0xC6),
165a7118662SVincent Pelletier };
166a7118662SVincent Pelletier
167a7118662SVincent Pelletier static struct iio_map iio_maps[] = {
168a7118662SVincent Pelletier IIO_MAP("CH0", "bcove-battery", "VBATRSLT"),
169a7118662SVincent Pelletier IIO_MAP("CH1", "bcove-battery", "BATTID"),
170a7118662SVincent Pelletier IIO_MAP("CH2", "bcove-battery", "IBATRSLT"),
171a7118662SVincent Pelletier IIO_MAP("CH3", "bcove-temp", "PMICTEMP"),
172a7118662SVincent Pelletier IIO_MAP("CH4", "bcove-temp", "BATTEMP0"),
173a7118662SVincent Pelletier IIO_MAP("CH5", "bcove-temp", "BATTEMP1"),
174a7118662SVincent Pelletier IIO_MAP("CH6", "bcove-temp", "SYSTEMP0"),
175a7118662SVincent Pelletier IIO_MAP("CH7", "bcove-temp", "SYSTEMP1"),
176a7118662SVincent Pelletier IIO_MAP("CH8", "bcove-temp", "SYSTEMP2"),
177a7118662SVincent Pelletier {}
178a7118662SVincent Pelletier };
179a7118662SVincent Pelletier
mrfld_adc_probe(struct platform_device * pdev)180a7118662SVincent Pelletier static int mrfld_adc_probe(struct platform_device *pdev)
181a7118662SVincent Pelletier {
182a7118662SVincent Pelletier struct device *dev = &pdev->dev;
183a7118662SVincent Pelletier struct intel_soc_pmic *pmic = dev_get_drvdata(dev->parent);
184a7118662SVincent Pelletier struct iio_dev *indio_dev;
185a7118662SVincent Pelletier struct mrfld_adc *adc;
186a7118662SVincent Pelletier int irq;
187a7118662SVincent Pelletier int ret;
188a7118662SVincent Pelletier
1891f310f77SDan Carpenter indio_dev = devm_iio_device_alloc(dev, sizeof(struct mrfld_adc));
190a7118662SVincent Pelletier if (!indio_dev)
191a7118662SVincent Pelletier return -ENOMEM;
192a7118662SVincent Pelletier
193a7118662SVincent Pelletier adc = iio_priv(indio_dev);
194a7118662SVincent Pelletier
195a7118662SVincent Pelletier mutex_init(&adc->lock);
196a7118662SVincent Pelletier init_completion(&adc->completion);
197a7118662SVincent Pelletier adc->regmap = pmic->regmap;
198a7118662SVincent Pelletier
199a7118662SVincent Pelletier irq = platform_get_irq(pdev, 0);
200a7118662SVincent Pelletier if (irq < 0)
201a7118662SVincent Pelletier return irq;
202a7118662SVincent Pelletier
203a7118662SVincent Pelletier ret = devm_request_threaded_irq(dev, irq, NULL, mrfld_adc_thread_isr,
204a7118662SVincent Pelletier IRQF_ONESHOT | IRQF_SHARED, pdev->name,
205a7118662SVincent Pelletier indio_dev);
206a7118662SVincent Pelletier if (ret)
207a7118662SVincent Pelletier return ret;
208a7118662SVincent Pelletier
209a7118662SVincent Pelletier indio_dev->name = pdev->name;
210a7118662SVincent Pelletier
211a7118662SVincent Pelletier indio_dev->channels = mrfld_adc_channels;
212a7118662SVincent Pelletier indio_dev->num_channels = ARRAY_SIZE(mrfld_adc_channels);
213a7118662SVincent Pelletier indio_dev->info = &mrfld_adc_iio_info;
214a7118662SVincent Pelletier indio_dev->modes = INDIO_DIRECT_MODE;
215a7118662SVincent Pelletier
2167a29120cSAlexandru Ardelean ret = devm_iio_map_array_register(dev, indio_dev, iio_maps);
217a7118662SVincent Pelletier if (ret)
218a7118662SVincent Pelletier return ret;
219a7118662SVincent Pelletier
2207a29120cSAlexandru Ardelean return devm_iio_device_register(dev, indio_dev);
221a7118662SVincent Pelletier }
222a7118662SVincent Pelletier
223a7118662SVincent Pelletier static const struct platform_device_id mrfld_adc_id_table[] = {
224a7118662SVincent Pelletier { .name = "mrfld_bcove_adc" },
225a7118662SVincent Pelletier {}
226a7118662SVincent Pelletier };
227a7118662SVincent Pelletier MODULE_DEVICE_TABLE(platform, mrfld_adc_id_table);
228a7118662SVincent Pelletier
229a7118662SVincent Pelletier static struct platform_driver mrfld_adc_driver = {
230a7118662SVincent Pelletier .driver = {
231a7118662SVincent Pelletier .name = "mrfld_bcove_adc",
232a7118662SVincent Pelletier },
233a7118662SVincent Pelletier .probe = mrfld_adc_probe,
234a7118662SVincent Pelletier .id_table = mrfld_adc_id_table,
235a7118662SVincent Pelletier };
236a7118662SVincent Pelletier module_platform_driver(mrfld_adc_driver);
237a7118662SVincent Pelletier
238a7118662SVincent Pelletier MODULE_AUTHOR("Bin Yang <bin.yang@intel.com>");
239a7118662SVincent Pelletier MODULE_AUTHOR("Vincent Pelletier <plr.vincent@gmail.com>");
240a7118662SVincent Pelletier MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
241a7118662SVincent Pelletier MODULE_DESCRIPTION("ADC driver for Basin Cove PMIC");
242a7118662SVincent Pelletier MODULE_LICENSE("GPL v2");
243