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> 25 #include <linux/of.h> 26 #include <linux/of_device.h> 27 #include <linux/iio/machine.h> 28 #include <linux/iio/driver.h> 29 30 #include <linux/mfd/ti_am335x_tscadc.h> 31 32 struct tiadc_device { 33 struct ti_tscadc_dev *mfd_tscadc; 34 int channels; 35 }; 36 37 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg) 38 { 39 return readl(adc->mfd_tscadc->tscadc_base + reg); 40 } 41 42 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg, 43 unsigned int val) 44 { 45 writel(val, adc->mfd_tscadc->tscadc_base + reg); 46 } 47 48 static u32 get_adc_step_mask(struct tiadc_device *adc_dev) 49 { 50 u32 step_en; 51 52 step_en = ((1 << adc_dev->channels) - 1); 53 step_en <<= TOTAL_STEPS - adc_dev->channels + 1; 54 return step_en; 55 } 56 57 static void tiadc_step_config(struct tiadc_device *adc_dev) 58 { 59 unsigned int stepconfig; 60 int i, channels = 0, steps; 61 u32 step_en; 62 63 /* 64 * There are 16 configurable steps and 8 analog input 65 * lines available which are shared between Touchscreen and ADC. 66 * 67 * Steps backwards i.e. from 16 towards 0 are used by ADC 68 * depending on number of input lines needed. 69 * Channel would represent which analog input 70 * needs to be given to ADC to digitalize data. 71 */ 72 73 steps = TOTAL_STEPS - adc_dev->channels; 74 channels = TOTAL_CHANNELS - adc_dev->channels; 75 76 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1; 77 78 for (i = (steps + 1); i <= TOTAL_STEPS; i++) { 79 tiadc_writel(adc_dev, REG_STEPCONFIG(i), 80 stepconfig | STEPCONFIG_INP(channels)); 81 tiadc_writel(adc_dev, REG_STEPDELAY(i), 82 STEPCONFIG_OPENDLY); 83 channels++; 84 } 85 step_en = get_adc_step_mask(adc_dev); 86 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en); 87 } 88 89 static const char * const chan_name_ain[] = { 90 "AIN0", 91 "AIN1", 92 "AIN2", 93 "AIN3", 94 "AIN4", 95 "AIN5", 96 "AIN6", 97 "AIN7", 98 }; 99 100 static int tiadc_channel_init(struct iio_dev *indio_dev, int channels) 101 { 102 struct tiadc_device *adc_dev = iio_priv(indio_dev); 103 struct iio_chan_spec *chan_array; 104 struct iio_chan_spec *chan; 105 int i; 106 107 indio_dev->num_channels = channels; 108 chan_array = kcalloc(channels, 109 sizeof(struct iio_chan_spec), GFP_KERNEL); 110 if (chan_array == NULL) 111 return -ENOMEM; 112 113 chan = chan_array; 114 for (i = 0; i < channels; i++, chan++) { 115 116 chan->type = IIO_VOLTAGE; 117 chan->indexed = 1; 118 chan->channel = i; 119 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 120 chan->datasheet_name = chan_name_ain[i]; 121 chan->scan_type.sign = 'u'; 122 chan->scan_type.realbits = 12; 123 chan->scan_type.storagebits = 32; 124 } 125 126 indio_dev->channels = chan_array; 127 128 return 0; 129 } 130 131 static void tiadc_channels_remove(struct iio_dev *indio_dev) 132 { 133 kfree(indio_dev->channels); 134 } 135 136 static int tiadc_read_raw(struct iio_dev *indio_dev, 137 struct iio_chan_spec const *chan, 138 int *val, int *val2, long mask) 139 { 140 struct tiadc_device *adc_dev = iio_priv(indio_dev); 141 int i; 142 unsigned int fifo1count, readx1; 143 144 /* 145 * When the sub-system is first enabled, 146 * the sequencer will always start with the 147 * lowest step (1) and continue until step (16). 148 * For ex: If we have enabled 4 ADC channels and 149 * currently use only 1 out of them, the 150 * sequencer still configures all the 4 steps, 151 * leading to 3 unwanted data. 152 * Hence we need to flush out this data. 153 */ 154 155 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); 156 for (i = 0; i < fifo1count; i++) { 157 readx1 = tiadc_readl(adc_dev, REG_FIFO1); 158 if (i == chan->channel) 159 *val = readx1 & 0xfff; 160 } 161 am335x_tsc_se_update(adc_dev->mfd_tscadc); 162 163 return IIO_VAL_INT; 164 } 165 166 static const struct iio_info tiadc_info = { 167 .read_raw = &tiadc_read_raw, 168 }; 169 170 static int tiadc_probe(struct platform_device *pdev) 171 { 172 struct iio_dev *indio_dev; 173 struct tiadc_device *adc_dev; 174 struct device_node *node = pdev->dev.of_node; 175 int err; 176 u32 val32; 177 178 if (!node) { 179 dev_err(&pdev->dev, "Could not find valid DT data.\n"); 180 return -EINVAL; 181 } 182 183 indio_dev = iio_device_alloc(sizeof(struct tiadc_device)); 184 if (indio_dev == NULL) { 185 dev_err(&pdev->dev, "failed to allocate iio device\n"); 186 err = -ENOMEM; 187 goto err_ret; 188 } 189 adc_dev = iio_priv(indio_dev); 190 191 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev); 192 193 err = of_property_read_u32(node, 194 "ti,adc-channels", &val32); 195 if (err < 0) 196 goto err_free_device; 197 adc_dev->channels = val32; 198 199 indio_dev->dev.parent = &pdev->dev; 200 indio_dev->name = dev_name(&pdev->dev); 201 indio_dev->modes = INDIO_DIRECT_MODE; 202 indio_dev->info = &tiadc_info; 203 204 tiadc_step_config(adc_dev); 205 206 err = tiadc_channel_init(indio_dev, adc_dev->channels); 207 if (err < 0) 208 goto err_free_device; 209 210 err = iio_device_register(indio_dev); 211 if (err) 212 goto err_free_channels; 213 214 platform_set_drvdata(pdev, indio_dev); 215 216 return 0; 217 218 err_free_channels: 219 tiadc_channels_remove(indio_dev); 220 err_free_device: 221 iio_device_free(indio_dev); 222 err_ret: 223 return err; 224 } 225 226 static int tiadc_remove(struct platform_device *pdev) 227 { 228 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 229 struct tiadc_device *adc_dev = iio_priv(indio_dev); 230 u32 step_en; 231 232 iio_device_unregister(indio_dev); 233 tiadc_channels_remove(indio_dev); 234 235 step_en = get_adc_step_mask(adc_dev); 236 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en); 237 238 iio_device_free(indio_dev); 239 240 return 0; 241 } 242 243 #ifdef CONFIG_PM 244 static int tiadc_suspend(struct device *dev) 245 { 246 struct iio_dev *indio_dev = dev_get_drvdata(dev); 247 struct tiadc_device *adc_dev = iio_priv(indio_dev); 248 struct ti_tscadc_dev *tscadc_dev; 249 unsigned int idle; 250 251 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev)); 252 if (!device_may_wakeup(tscadc_dev->dev)) { 253 idle = tiadc_readl(adc_dev, REG_CTRL); 254 idle &= ~(CNTRLREG_TSCSSENB); 255 tiadc_writel(adc_dev, REG_CTRL, (idle | 256 CNTRLREG_POWERDOWN)); 257 } 258 259 return 0; 260 } 261 262 static int tiadc_resume(struct device *dev) 263 { 264 struct iio_dev *indio_dev = dev_get_drvdata(dev); 265 struct tiadc_device *adc_dev = iio_priv(indio_dev); 266 unsigned int restore; 267 268 /* Make sure ADC is powered up */ 269 restore = tiadc_readl(adc_dev, REG_CTRL); 270 restore &= ~(CNTRLREG_POWERDOWN); 271 tiadc_writel(adc_dev, REG_CTRL, restore); 272 273 tiadc_step_config(adc_dev); 274 275 return 0; 276 } 277 278 static const struct dev_pm_ops tiadc_pm_ops = { 279 .suspend = tiadc_suspend, 280 .resume = tiadc_resume, 281 }; 282 #define TIADC_PM_OPS (&tiadc_pm_ops) 283 #else 284 #define TIADC_PM_OPS NULL 285 #endif 286 287 static const struct of_device_id ti_adc_dt_ids[] = { 288 { .compatible = "ti,am3359-adc", }, 289 { } 290 }; 291 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids); 292 293 static struct platform_driver tiadc_driver = { 294 .driver = { 295 .name = "TI-am335x-adc", 296 .owner = THIS_MODULE, 297 .pm = TIADC_PM_OPS, 298 .of_match_table = of_match_ptr(ti_adc_dt_ids), 299 }, 300 .probe = tiadc_probe, 301 .remove = tiadc_remove, 302 }; 303 module_platform_driver(tiadc_driver); 304 305 MODULE_DESCRIPTION("TI ADC controller driver"); 306 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>"); 307 MODULE_LICENSE("GPL"); 308