Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 1 | /* |
| 2 | * TI ADC MFD driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation version 2. |
| 9 | * |
| 10 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 11 | * kind, whether express or implied; without even the implied warranty |
| 12 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/iio/iio.h> |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_device.h> |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 27 | #include <linux/iio/machine.h> |
| 28 | #include <linux/iio/driver.h> |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 29 | |
| 30 | #include <linux/mfd/ti_am335x_tscadc.h> |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 31 | |
| 32 | struct tiadc_device { |
| 33 | struct ti_tscadc_dev *mfd_tscadc; |
| 34 | int channels; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 35 | u8 channel_line[8]; |
| 36 | u8 channel_step[8]; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg) |
| 40 | { |
| 41 | return readl(adc->mfd_tscadc->tscadc_base + reg); |
| 42 | } |
| 43 | |
| 44 | static void tiadc_writel(struct tiadc_device *adc, unsigned int reg, |
| 45 | unsigned int val) |
| 46 | { |
| 47 | writel(val, adc->mfd_tscadc->tscadc_base + reg); |
| 48 | } |
| 49 | |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 50 | static u32 get_adc_step_mask(struct tiadc_device *adc_dev) |
| 51 | { |
| 52 | u32 step_en; |
| 53 | |
| 54 | step_en = ((1 << adc_dev->channels) - 1); |
| 55 | step_en <<= TOTAL_STEPS - adc_dev->channels + 1; |
| 56 | return step_en; |
| 57 | } |
| 58 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 59 | static void tiadc_step_config(struct tiadc_device *adc_dev) |
| 60 | { |
| 61 | unsigned int stepconfig; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 62 | int i, steps; |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 63 | u32 step_en; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * There are 16 configurable steps and 8 analog input |
| 67 | * lines available which are shared between Touchscreen and ADC. |
| 68 | * |
| 69 | * Steps backwards i.e. from 16 towards 0 are used by ADC |
| 70 | * depending on number of input lines needed. |
| 71 | * Channel would represent which analog input |
| 72 | * needs to be given to ADC to digitalize data. |
| 73 | */ |
| 74 | |
| 75 | steps = TOTAL_STEPS - adc_dev->channels; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 76 | stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1; |
| 77 | |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 78 | for (i = 0; i < adc_dev->channels; i++) { |
| 79 | int chan; |
| 80 | |
| 81 | chan = adc_dev->channel_line[i]; |
| 82 | tiadc_writel(adc_dev, REG_STEPCONFIG(steps), |
| 83 | stepconfig | STEPCONFIG_INP(chan)); |
| 84 | tiadc_writel(adc_dev, REG_STEPDELAY(steps), |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 85 | STEPCONFIG_OPENDLY); |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 86 | adc_dev->channel_step[i] = steps; |
| 87 | steps++; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 88 | } |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 89 | step_en = get_adc_step_mask(adc_dev); |
| 90 | am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 91 | } |
| 92 | |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 93 | static const char * const chan_name_ain[] = { |
| 94 | "AIN0", |
| 95 | "AIN1", |
| 96 | "AIN2", |
| 97 | "AIN3", |
| 98 | "AIN4", |
| 99 | "AIN5", |
| 100 | "AIN6", |
| 101 | "AIN7", |
| 102 | }; |
| 103 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 104 | static int tiadc_channel_init(struct iio_dev *indio_dev, int channels) |
| 105 | { |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 106 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 107 | struct iio_chan_spec *chan_array; |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 108 | struct iio_chan_spec *chan; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 109 | int i; |
| 110 | |
| 111 | indio_dev->num_channels = channels; |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 112 | chan_array = kcalloc(channels, |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 113 | sizeof(struct iio_chan_spec), GFP_KERNEL); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 114 | if (chan_array == NULL) |
| 115 | return -ENOMEM; |
| 116 | |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 117 | chan = chan_array; |
| 118 | for (i = 0; i < channels; i++, chan++) { |
| 119 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 120 | chan->type = IIO_VOLTAGE; |
| 121 | chan->indexed = 1; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 122 | chan->channel = adc_dev->channel_line[i]; |
Jonathan Cameron | 6c57252 | 2013-02-27 19:07:18 +0000 | [diff] [blame] | 123 | chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 124 | chan->datasheet_name = chan_name_ain[chan->channel]; |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 125 | chan->scan_type.sign = 'u'; |
| 126 | chan->scan_type.realbits = 12; |
| 127 | chan->scan_type.storagebits = 32; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | indio_dev->channels = chan_array; |
| 131 | |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 132 | return 0; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void tiadc_channels_remove(struct iio_dev *indio_dev) |
| 136 | { |
| 137 | kfree(indio_dev->channels); |
| 138 | } |
| 139 | |
| 140 | static int tiadc_read_raw(struct iio_dev *indio_dev, |
| 141 | struct iio_chan_spec const *chan, |
| 142 | int *val, int *val2, long mask) |
| 143 | { |
| 144 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 145 | int i; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 146 | unsigned int fifo1count, read; |
| 147 | u32 step = UINT_MAX; |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame^] | 148 | bool found = false; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * When the sub-system is first enabled, |
| 152 | * the sequencer will always start with the |
| 153 | * lowest step (1) and continue until step (16). |
| 154 | * For ex: If we have enabled 4 ADC channels and |
| 155 | * currently use only 1 out of them, the |
| 156 | * sequencer still configures all the 4 steps, |
| 157 | * leading to 3 unwanted data. |
| 158 | * Hence we need to flush out this data. |
| 159 | */ |
| 160 | |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 161 | for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) { |
| 162 | if (chan->channel == adc_dev->channel_line[i]) { |
| 163 | step = adc_dev->channel_step[i]; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | if (WARN_ON_ONCE(step == UINT_MAX)) |
| 168 | return -EINVAL; |
| 169 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 170 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 171 | for (i = 0; i < fifo1count; i++) { |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 172 | read = tiadc_readl(adc_dev, REG_FIFO1); |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame^] | 173 | if (read >> 16 == step) { |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 174 | *val = read & 0xfff; |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame^] | 175 | found = true; |
| 176 | } |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 177 | } |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 178 | am335x_tsc_se_update(adc_dev->mfd_tscadc); |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame^] | 179 | if (found == false) |
| 180 | return -EBUSY; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 181 | return IIO_VAL_INT; |
| 182 | } |
| 183 | |
| 184 | static const struct iio_info tiadc_info = { |
| 185 | .read_raw = &tiadc_read_raw, |
| 186 | }; |
| 187 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 188 | static int tiadc_probe(struct platform_device *pdev) |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 189 | { |
| 190 | struct iio_dev *indio_dev; |
| 191 | struct tiadc_device *adc_dev; |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 192 | struct device_node *node = pdev->dev.of_node; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 193 | struct property *prop; |
| 194 | const __be32 *cur; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 195 | int err; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 196 | u32 val; |
| 197 | int channels = 0; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 198 | |
Sebastian Andrzej Siewior | 0ead4fb | 2013-05-21 17:49:22 +0200 | [diff] [blame] | 199 | if (!node) { |
| 200 | dev_err(&pdev->dev, "Could not find valid DT data.\n"); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 201 | return -EINVAL; |
| 202 | } |
| 203 | |
| 204 | indio_dev = iio_device_alloc(sizeof(struct tiadc_device)); |
| 205 | if (indio_dev == NULL) { |
| 206 | dev_err(&pdev->dev, "failed to allocate iio device\n"); |
| 207 | err = -ENOMEM; |
| 208 | goto err_ret; |
| 209 | } |
| 210 | adc_dev = iio_priv(indio_dev); |
| 211 | |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 212 | adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev); |
| 213 | |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 214 | of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) { |
| 215 | adc_dev->channel_line[channels] = val; |
| 216 | channels++; |
| 217 | } |
| 218 | adc_dev->channels = channels; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 219 | |
| 220 | indio_dev->dev.parent = &pdev->dev; |
| 221 | indio_dev->name = dev_name(&pdev->dev); |
| 222 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 223 | indio_dev->info = &tiadc_info; |
| 224 | |
| 225 | tiadc_step_config(adc_dev); |
| 226 | |
| 227 | err = tiadc_channel_init(indio_dev, adc_dev->channels); |
| 228 | if (err < 0) |
| 229 | goto err_free_device; |
| 230 | |
| 231 | err = iio_device_register(indio_dev); |
| 232 | if (err) |
| 233 | goto err_free_channels; |
| 234 | |
| 235 | platform_set_drvdata(pdev, indio_dev); |
| 236 | |
| 237 | return 0; |
| 238 | |
| 239 | err_free_channels: |
| 240 | tiadc_channels_remove(indio_dev); |
| 241 | err_free_device: |
| 242 | iio_device_free(indio_dev); |
| 243 | err_ret: |
| 244 | return err; |
| 245 | } |
| 246 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 247 | static int tiadc_remove(struct platform_device *pdev) |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 248 | { |
| 249 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 250 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 251 | u32 step_en; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 252 | |
| 253 | iio_device_unregister(indio_dev); |
| 254 | tiadc_channels_remove(indio_dev); |
| 255 | |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 256 | step_en = get_adc_step_mask(adc_dev); |
| 257 | am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en); |
| 258 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 259 | iio_device_free(indio_dev); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | #ifdef CONFIG_PM |
| 265 | static int tiadc_suspend(struct device *dev) |
| 266 | { |
| 267 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 268 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Sebastian Andrzej Siewior | a9bce1b | 2013-06-05 16:13:47 +0200 | [diff] [blame] | 269 | struct ti_tscadc_dev *tscadc_dev; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 270 | unsigned int idle; |
| 271 | |
Sebastian Andrzej Siewior | a9bce1b | 2013-06-05 16:13:47 +0200 | [diff] [blame] | 272 | tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev)); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 273 | if (!device_may_wakeup(tscadc_dev->dev)) { |
| 274 | idle = tiadc_readl(adc_dev, REG_CTRL); |
| 275 | idle &= ~(CNTRLREG_TSCSSENB); |
| 276 | tiadc_writel(adc_dev, REG_CTRL, (idle | |
| 277 | CNTRLREG_POWERDOWN)); |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int tiadc_resume(struct device *dev) |
| 284 | { |
| 285 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 286 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 287 | unsigned int restore; |
| 288 | |
| 289 | /* Make sure ADC is powered up */ |
| 290 | restore = tiadc_readl(adc_dev, REG_CTRL); |
| 291 | restore &= ~(CNTRLREG_POWERDOWN); |
| 292 | tiadc_writel(adc_dev, REG_CTRL, restore); |
| 293 | |
| 294 | tiadc_step_config(adc_dev); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static const struct dev_pm_ops tiadc_pm_ops = { |
| 300 | .suspend = tiadc_suspend, |
| 301 | .resume = tiadc_resume, |
| 302 | }; |
| 303 | #define TIADC_PM_OPS (&tiadc_pm_ops) |
| 304 | #else |
| 305 | #define TIADC_PM_OPS NULL |
| 306 | #endif |
| 307 | |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 308 | static const struct of_device_id ti_adc_dt_ids[] = { |
| 309 | { .compatible = "ti,am3359-adc", }, |
| 310 | { } |
| 311 | }; |
| 312 | MODULE_DEVICE_TABLE(of, ti_adc_dt_ids); |
| 313 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 314 | static struct platform_driver tiadc_driver = { |
| 315 | .driver = { |
Sebastian Andrzej Siewior | 9f99928 | 2013-05-27 17:12:52 +0200 | [diff] [blame] | 316 | .name = "TI-am335x-adc", |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 317 | .owner = THIS_MODULE, |
| 318 | .pm = TIADC_PM_OPS, |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 319 | .of_match_table = of_match_ptr(ti_adc_dt_ids), |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 320 | }, |
| 321 | .probe = tiadc_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 322 | .remove = tiadc_remove, |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 323 | }; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 324 | module_platform_driver(tiadc_driver); |
| 325 | |
| 326 | MODULE_DESCRIPTION("TI ADC controller driver"); |
| 327 | MODULE_AUTHOR("Rachna Patil <rachna@ti.com>"); |
| 328 | MODULE_LICENSE("GPL"); |