mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / iio / adc / da9150-gpadc.c
blob3445107e10b722126ad2806fcc16654ef35cc7bb
1 /*
2 * DA9150 GPADC Driver
4 * Copyright (c) 2014 Dialog Semiconductor
6 * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/interrupt.h>
19 #include <linux/mutex.h>
20 #include <linux/completion.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/machine.h>
23 #include <linux/iio/driver.h>
24 #include <linux/mfd/da9150/core.h>
25 #include <linux/mfd/da9150/registers.h>
27 /* Channels */
28 enum da9150_gpadc_hw_channel {
29 DA9150_GPADC_HW_CHAN_GPIOA_2V = 0,
30 DA9150_GPADC_HW_CHAN_GPIOA_2V_,
31 DA9150_GPADC_HW_CHAN_GPIOB_2V,
32 DA9150_GPADC_HW_CHAN_GPIOB_2V_,
33 DA9150_GPADC_HW_CHAN_GPIOC_2V,
34 DA9150_GPADC_HW_CHAN_GPIOC_2V_,
35 DA9150_GPADC_HW_CHAN_GPIOD_2V,
36 DA9150_GPADC_HW_CHAN_GPIOD_2V_,
37 DA9150_GPADC_HW_CHAN_IBUS_SENSE,
38 DA9150_GPADC_HW_CHAN_IBUS_SENSE_,
39 DA9150_GPADC_HW_CHAN_VBUS_DIV,
40 DA9150_GPADC_HW_CHAN_VBUS_DIV_,
41 DA9150_GPADC_HW_CHAN_ID,
42 DA9150_GPADC_HW_CHAN_ID_,
43 DA9150_GPADC_HW_CHAN_VSYS,
44 DA9150_GPADC_HW_CHAN_VSYS_,
45 DA9150_GPADC_HW_CHAN_GPIOA_6V,
46 DA9150_GPADC_HW_CHAN_GPIOA_6V_,
47 DA9150_GPADC_HW_CHAN_GPIOB_6V,
48 DA9150_GPADC_HW_CHAN_GPIOB_6V_,
49 DA9150_GPADC_HW_CHAN_GPIOC_6V,
50 DA9150_GPADC_HW_CHAN_GPIOC_6V_,
51 DA9150_GPADC_HW_CHAN_GPIOD_6V,
52 DA9150_GPADC_HW_CHAN_GPIOD_6V_,
53 DA9150_GPADC_HW_CHAN_VBAT,
54 DA9150_GPADC_HW_CHAN_VBAT_,
55 DA9150_GPADC_HW_CHAN_TBAT,
56 DA9150_GPADC_HW_CHAN_TBAT_,
57 DA9150_GPADC_HW_CHAN_TJUNC_CORE,
58 DA9150_GPADC_HW_CHAN_TJUNC_CORE_,
59 DA9150_GPADC_HW_CHAN_TJUNC_OVP,
60 DA9150_GPADC_HW_CHAN_TJUNC_OVP_,
63 enum da9150_gpadc_channel {
64 DA9150_GPADC_CHAN_GPIOA = 0,
65 DA9150_GPADC_CHAN_GPIOB,
66 DA9150_GPADC_CHAN_GPIOC,
67 DA9150_GPADC_CHAN_GPIOD,
68 DA9150_GPADC_CHAN_IBUS,
69 DA9150_GPADC_CHAN_VBUS,
70 DA9150_GPADC_CHAN_VSYS,
71 DA9150_GPADC_CHAN_VBAT,
72 DA9150_GPADC_CHAN_TBAT,
73 DA9150_GPADC_CHAN_TJUNC_CORE,
74 DA9150_GPADC_CHAN_TJUNC_OVP,
77 /* Private data */
78 struct da9150_gpadc {
79 struct da9150 *da9150;
80 struct device *dev;
82 struct mutex lock;
83 struct completion complete;
87 static irqreturn_t da9150_gpadc_irq(int irq, void *data)
90 struct da9150_gpadc *gpadc = data;
92 complete(&gpadc->complete);
94 return IRQ_HANDLED;
97 static int da9150_gpadc_read_adc(struct da9150_gpadc *gpadc, int hw_chan)
99 u8 result_regs[2];
100 int result;
102 mutex_lock(&gpadc->lock);
104 /* Set channel & enable measurement */
105 da9150_reg_write(gpadc->da9150, DA9150_GPADC_MAN,
106 (DA9150_GPADC_EN_MASK |
107 hw_chan << DA9150_GPADC_MUX_SHIFT));
109 /* Consume left-over completion from a previous timeout */
110 try_wait_for_completion(&gpadc->complete);
112 /* Check for actual completion */
113 wait_for_completion_timeout(&gpadc->complete, msecs_to_jiffies(5));
115 /* Read result and status from device */
116 da9150_bulk_read(gpadc->da9150, DA9150_GPADC_RES_A, 2, result_regs);
118 mutex_unlock(&gpadc->lock);
120 /* Check to make sure device really has completed reading */
121 if (result_regs[1] & DA9150_GPADC_RUN_MASK) {
122 dev_err(gpadc->dev, "Timeout on channel %d of GPADC\n",
123 hw_chan);
124 return -ETIMEDOUT;
127 /* LSBs - 2 bits */
128 result = (result_regs[1] & DA9150_GPADC_RES_L_MASK) >>
129 DA9150_GPADC_RES_L_SHIFT;
130 /* MSBs - 8 bits */
131 result |= result_regs[0] << DA9150_GPADC_RES_L_BITS;
133 return result;
136 static inline int da9150_gpadc_gpio_6v_voltage_now(int raw_val)
138 /* Convert to mV */
139 return (6 * ((raw_val * 1000) + 500)) / 1024;
142 static inline int da9150_gpadc_ibus_current_avg(int raw_val)
144 /* Convert to mA */
145 return (4 * ((raw_val * 1000) + 500)) / 2048;
148 static inline int da9150_gpadc_vbus_21v_voltage_now(int raw_val)
150 /* Convert to mV */
151 return (21 * ((raw_val * 1000) + 500)) / 1024;
154 static inline int da9150_gpadc_vsys_6v_voltage_now(int raw_val)
156 /* Convert to mV */
157 return (3 * ((raw_val * 1000) + 500)) / 512;
160 static int da9150_gpadc_read_processed(struct da9150_gpadc *gpadc, int channel,
161 int hw_chan, int *val)
163 int raw_val;
165 raw_val = da9150_gpadc_read_adc(gpadc, hw_chan);
166 if (raw_val < 0)
167 return raw_val;
169 switch (channel) {
170 case DA9150_GPADC_CHAN_GPIOA:
171 case DA9150_GPADC_CHAN_GPIOB:
172 case DA9150_GPADC_CHAN_GPIOC:
173 case DA9150_GPADC_CHAN_GPIOD:
174 *val = da9150_gpadc_gpio_6v_voltage_now(raw_val);
175 break;
176 case DA9150_GPADC_CHAN_IBUS:
177 *val = da9150_gpadc_ibus_current_avg(raw_val);
178 break;
179 case DA9150_GPADC_CHAN_VBUS:
180 *val = da9150_gpadc_vbus_21v_voltage_now(raw_val);
181 break;
182 case DA9150_GPADC_CHAN_VSYS:
183 *val = da9150_gpadc_vsys_6v_voltage_now(raw_val);
184 break;
185 default:
186 /* No processing for other channels so return raw value */
187 *val = raw_val;
188 break;
191 return IIO_VAL_INT;
194 static int da9150_gpadc_read_scale(int channel, int *val, int *val2)
196 switch (channel) {
197 case DA9150_GPADC_CHAN_VBAT:
198 *val = 2932;
199 *val2 = 1000;
200 return IIO_VAL_FRACTIONAL;
201 case DA9150_GPADC_CHAN_TJUNC_CORE:
202 case DA9150_GPADC_CHAN_TJUNC_OVP:
203 *val = 1000000;
204 *val2 = 4420;
205 return IIO_VAL_FRACTIONAL;
206 default:
207 return -EINVAL;
211 static int da9150_gpadc_read_offset(int channel, int *val)
213 switch (channel) {
214 case DA9150_GPADC_CHAN_VBAT:
215 *val = 1500000 / 2932;
216 return IIO_VAL_INT;
217 case DA9150_GPADC_CHAN_TJUNC_CORE:
218 case DA9150_GPADC_CHAN_TJUNC_OVP:
219 *val = -144;
220 return IIO_VAL_INT;
221 default:
222 return -EINVAL;
226 static int da9150_gpadc_read_raw(struct iio_dev *indio_dev,
227 struct iio_chan_spec const *chan,
228 int *val, int *val2, long mask)
230 struct da9150_gpadc *gpadc = iio_priv(indio_dev);
232 if ((chan->channel < DA9150_GPADC_CHAN_GPIOA) ||
233 (chan->channel > DA9150_GPADC_CHAN_TJUNC_OVP))
234 return -EINVAL;
236 switch (mask) {
237 case IIO_CHAN_INFO_RAW:
238 case IIO_CHAN_INFO_PROCESSED:
239 return da9150_gpadc_read_processed(gpadc, chan->channel,
240 chan->address, val);
241 case IIO_CHAN_INFO_SCALE:
242 return da9150_gpadc_read_scale(chan->channel, val, val2);
243 case IIO_CHAN_INFO_OFFSET:
244 return da9150_gpadc_read_offset(chan->channel, val);
245 default:
246 return -EINVAL;
250 static const struct iio_info da9150_gpadc_info = {
251 .read_raw = &da9150_gpadc_read_raw,
252 .driver_module = THIS_MODULE,
255 #define DA9150_GPADC_CHANNEL(_id, _hw_id, _type, chan_info, \
256 _ext_name) { \
257 .type = _type, \
258 .indexed = 1, \
259 .channel = DA9150_GPADC_CHAN_##_id, \
260 .address = DA9150_GPADC_HW_CHAN_##_hw_id, \
261 .info_mask_separate = chan_info, \
262 .extend_name = _ext_name, \
263 .datasheet_name = #_id, \
266 #define DA9150_GPADC_CHANNEL_RAW(_id, _hw_id, _type, _ext_name) \
267 DA9150_GPADC_CHANNEL(_id, _hw_id, _type, \
268 BIT(IIO_CHAN_INFO_RAW), _ext_name)
270 #define DA9150_GPADC_CHANNEL_SCALED(_id, _hw_id, _type, _ext_name) \
271 DA9150_GPADC_CHANNEL(_id, _hw_id, _type, \
272 BIT(IIO_CHAN_INFO_RAW) | \
273 BIT(IIO_CHAN_INFO_SCALE) | \
274 BIT(IIO_CHAN_INFO_OFFSET), \
275 _ext_name)
277 #define DA9150_GPADC_CHANNEL_PROCESSED(_id, _hw_id, _type, _ext_name) \
278 DA9150_GPADC_CHANNEL(_id, _hw_id, _type, \
279 BIT(IIO_CHAN_INFO_PROCESSED), _ext_name)
281 /* Supported channels */
282 static const struct iio_chan_spec da9150_gpadc_channels[] = {
283 DA9150_GPADC_CHANNEL_PROCESSED(GPIOA, GPIOA_6V, IIO_VOLTAGE, NULL),
284 DA9150_GPADC_CHANNEL_PROCESSED(GPIOB, GPIOB_6V, IIO_VOLTAGE, NULL),
285 DA9150_GPADC_CHANNEL_PROCESSED(GPIOC, GPIOC_6V, IIO_VOLTAGE, NULL),
286 DA9150_GPADC_CHANNEL_PROCESSED(GPIOD, GPIOD_6V, IIO_VOLTAGE, NULL),
287 DA9150_GPADC_CHANNEL_PROCESSED(IBUS, IBUS_SENSE, IIO_CURRENT, "ibus"),
288 DA9150_GPADC_CHANNEL_PROCESSED(VBUS, VBUS_DIV_, IIO_VOLTAGE, "vbus"),
289 DA9150_GPADC_CHANNEL_PROCESSED(VSYS, VSYS, IIO_VOLTAGE, "vsys"),
290 DA9150_GPADC_CHANNEL_SCALED(VBAT, VBAT, IIO_VOLTAGE, "vbat"),
291 DA9150_GPADC_CHANNEL_RAW(TBAT, TBAT, IIO_VOLTAGE, "tbat"),
292 DA9150_GPADC_CHANNEL_SCALED(TJUNC_CORE, TJUNC_CORE, IIO_TEMP,
293 "tjunc_core"),
294 DA9150_GPADC_CHANNEL_SCALED(TJUNC_OVP, TJUNC_OVP, IIO_TEMP,
295 "tjunc_ovp"),
298 /* Default maps used by da9150-charger */
299 static struct iio_map da9150_gpadc_default_maps[] = {
301 .consumer_dev_name = "da9150-charger",
302 .consumer_channel = "CHAN_IBUS",
303 .adc_channel_label = "IBUS",
306 .consumer_dev_name = "da9150-charger",
307 .consumer_channel = "CHAN_VBUS",
308 .adc_channel_label = "VBUS",
311 .consumer_dev_name = "da9150-charger",
312 .consumer_channel = "CHAN_TJUNC",
313 .adc_channel_label = "TJUNC_CORE",
316 .consumer_dev_name = "da9150-charger",
317 .consumer_channel = "CHAN_VBAT",
318 .adc_channel_label = "VBAT",
323 static int da9150_gpadc_probe(struct platform_device *pdev)
325 struct device *dev = &pdev->dev;
326 struct da9150 *da9150 = dev_get_drvdata(dev->parent);
327 struct da9150_gpadc *gpadc;
328 struct iio_dev *indio_dev;
329 int irq, ret;
331 indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
332 if (!indio_dev) {
333 dev_err(&pdev->dev, "Failed to allocate IIO device\n");
334 return -ENOMEM;
336 gpadc = iio_priv(indio_dev);
338 platform_set_drvdata(pdev, indio_dev);
339 gpadc->da9150 = da9150;
340 gpadc->dev = dev;
341 mutex_init(&gpadc->lock);
342 init_completion(&gpadc->complete);
344 irq = platform_get_irq_byname(pdev, "GPADC");
345 if (irq < 0) {
346 dev_err(dev, "Failed to get IRQ: %d\n", irq);
347 return irq;
350 ret = devm_request_threaded_irq(dev, irq, NULL, da9150_gpadc_irq,
351 IRQF_ONESHOT, "GPADC", gpadc);
352 if (ret) {
353 dev_err(dev, "Failed to request IRQ %d: %d\n", irq, ret);
354 return ret;
357 ret = iio_map_array_register(indio_dev, da9150_gpadc_default_maps);
358 if (ret) {
359 dev_err(dev, "Failed to register IIO maps: %d\n", ret);
360 return ret;
363 indio_dev->name = dev_name(dev);
364 indio_dev->dev.parent = dev;
365 indio_dev->dev.of_node = pdev->dev.of_node;
366 indio_dev->info = &da9150_gpadc_info;
367 indio_dev->modes = INDIO_DIRECT_MODE;
368 indio_dev->channels = da9150_gpadc_channels;
369 indio_dev->num_channels = ARRAY_SIZE(da9150_gpadc_channels);
371 ret = iio_device_register(indio_dev);
372 if (ret) {
373 dev_err(dev, "Failed to register IIO device: %d\n", ret);
374 goto iio_map_unreg;
377 return 0;
379 iio_map_unreg:
380 iio_map_array_unregister(indio_dev);
382 return ret;
385 static int da9150_gpadc_remove(struct platform_device *pdev)
387 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
389 iio_device_unregister(indio_dev);
390 iio_map_array_unregister(indio_dev);
392 return 0;
395 static struct platform_driver da9150_gpadc_driver = {
396 .driver = {
397 .name = "da9150-gpadc",
399 .probe = da9150_gpadc_probe,
400 .remove = da9150_gpadc_remove,
403 module_platform_driver(da9150_gpadc_driver);
405 MODULE_DESCRIPTION("GPADC Driver for DA9150");
406 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
407 MODULE_LICENSE("GPL");