Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 1 | /* arch/arm/plat-s3c24xx/adc.c |
| 2 | * |
| 3 | * Copyright (c) 2008 Simtec Electronics |
| 4 | * http://armlinux.simtec.co.uk/ |
| 5 | * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org> |
| 6 | * |
| 7 | * S3C24XX ADC device core |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/clk.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
| 22 | |
| 23 | #include <plat/regs-adc.h> |
| 24 | #include <plat/adc.h> |
| 25 | |
| 26 | /* This driver is designed to control the usage of the ADC block between |
| 27 | * the touchscreen and any other drivers that may need to use it, such as |
| 28 | * the hwmon driver. |
| 29 | * |
| 30 | * Priority will be given to the touchscreen driver, but as this itself is |
| 31 | * rate limited it should not starve other requests which are processed in |
| 32 | * order that they are received. |
| 33 | * |
| 34 | * Each user registers to get a client block which uniquely identifies it |
| 35 | * and stores information such as the necessary functions to callback when |
| 36 | * action is required. |
| 37 | */ |
| 38 | |
| 39 | struct s3c_adc_client { |
| 40 | struct platform_device *pdev; |
| 41 | struct list_head pend; |
| 42 | |
| 43 | unsigned int nr_samples; |
| 44 | unsigned char is_ts; |
| 45 | unsigned char channel; |
| 46 | |
| 47 | void (*select_cb)(unsigned selected); |
Nelson Castillo | 3f7ea46 | 2009-05-08 08:10:12 -0500 | [diff] [blame^] | 48 | void (*convert_cb)(unsigned val1, unsigned val2, |
| 49 | unsigned *samples_left); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct adc_device { |
| 53 | struct platform_device *pdev; |
| 54 | struct platform_device *owner; |
| 55 | struct clk *clk; |
| 56 | struct s3c_adc_client *cur; |
| 57 | struct s3c_adc_client *ts_pend; |
| 58 | void __iomem *regs; |
| 59 | |
| 60 | unsigned int prescale; |
| 61 | |
| 62 | int irq; |
| 63 | }; |
| 64 | |
| 65 | static struct adc_device *adc_dev; |
| 66 | |
| 67 | static LIST_HEAD(adc_pending); |
| 68 | |
| 69 | #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg) |
| 70 | |
| 71 | static inline void s3c_adc_convert(struct adc_device *adc) |
| 72 | { |
| 73 | unsigned con = readl(adc->regs + S3C2410_ADCCON); |
| 74 | |
| 75 | con |= S3C2410_ADCCON_ENABLE_START; |
| 76 | writel(con, adc->regs + S3C2410_ADCCON); |
| 77 | } |
| 78 | |
| 79 | static inline void s3c_adc_select(struct adc_device *adc, |
| 80 | struct s3c_adc_client *client) |
| 81 | { |
| 82 | unsigned con = readl(adc->regs + S3C2410_ADCCON); |
| 83 | |
| 84 | client->select_cb(1); |
| 85 | |
| 86 | con &= ~S3C2410_ADCCON_MUXMASK; |
| 87 | con &= ~S3C2410_ADCCON_STDBM; |
| 88 | con &= ~S3C2410_ADCCON_STARTMASK; |
| 89 | |
| 90 | if (!client->is_ts) |
| 91 | con |= S3C2410_ADCCON_SELMUX(client->channel); |
| 92 | |
| 93 | writel(con, adc->regs + S3C2410_ADCCON); |
| 94 | } |
| 95 | |
| 96 | static void s3c_adc_dbgshow(struct adc_device *adc) |
| 97 | { |
| 98 | adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n", |
| 99 | readl(adc->regs + S3C2410_ADCCON), |
| 100 | readl(adc->regs + S3C2410_ADCTSC), |
| 101 | readl(adc->regs + S3C2410_ADCDLY)); |
| 102 | } |
| 103 | |
Ben Dooks | f8c8ac8 | 2009-04-17 12:36:49 +0100 | [diff] [blame] | 104 | static void s3c_adc_try(struct adc_device *adc) |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 105 | { |
| 106 | struct s3c_adc_client *next = adc->ts_pend; |
| 107 | |
| 108 | if (!next && !list_empty(&adc_pending)) { |
| 109 | next = list_first_entry(&adc_pending, |
| 110 | struct s3c_adc_client, pend); |
| 111 | list_del(&next->pend); |
| 112 | } else |
| 113 | adc->ts_pend = NULL; |
| 114 | |
| 115 | if (next) { |
| 116 | adc_dbg(adc, "new client is %p\n", next); |
| 117 | adc->cur = next; |
| 118 | s3c_adc_select(adc, next); |
| 119 | s3c_adc_convert(adc); |
| 120 | s3c_adc_dbgshow(adc); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | int s3c_adc_start(struct s3c_adc_client *client, |
| 125 | unsigned int channel, unsigned int nr_samples) |
| 126 | { |
| 127 | struct adc_device *adc = adc_dev; |
| 128 | unsigned long flags; |
| 129 | |
| 130 | if (!adc) { |
| 131 | printk(KERN_ERR "%s: failed to find adc\n", __func__); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | if (client->is_ts && adc->ts_pend) |
| 136 | return -EAGAIN; |
| 137 | |
| 138 | local_irq_save(flags); |
| 139 | |
| 140 | client->channel = channel; |
| 141 | client->nr_samples = nr_samples; |
| 142 | |
| 143 | if (client->is_ts) |
| 144 | adc->ts_pend = client; |
| 145 | else |
| 146 | list_add_tail(&client->pend, &adc_pending); |
| 147 | |
| 148 | if (!adc->cur) |
| 149 | s3c_adc_try(adc); |
| 150 | local_irq_restore(flags); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | EXPORT_SYMBOL_GPL(s3c_adc_start); |
| 155 | |
| 156 | static void s3c_adc_default_select(unsigned select) |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev, |
| 161 | void (*select)(unsigned int selected), |
Nelson Castillo | 3f7ea46 | 2009-05-08 08:10:12 -0500 | [diff] [blame^] | 162 | void (*conv)(unsigned d0, unsigned d1, |
| 163 | unsigned *samples_left), |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 164 | unsigned int is_ts) |
| 165 | { |
| 166 | struct s3c_adc_client *client; |
| 167 | |
| 168 | WARN_ON(!pdev); |
| 169 | WARN_ON(!conv); |
| 170 | |
| 171 | if (!select) |
| 172 | select = s3c_adc_default_select; |
| 173 | |
| 174 | if (!conv || !pdev) |
| 175 | return ERR_PTR(-EINVAL); |
| 176 | |
| 177 | client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL); |
| 178 | if (!client) { |
| 179 | dev_err(&pdev->dev, "no memory for adc client\n"); |
| 180 | return ERR_PTR(-ENOMEM); |
| 181 | } |
| 182 | |
| 183 | client->pdev = pdev; |
| 184 | client->is_ts = is_ts; |
| 185 | client->select_cb = select; |
| 186 | client->convert_cb = conv; |
| 187 | |
| 188 | return client; |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(s3c_adc_register); |
| 191 | |
| 192 | void s3c_adc_release(struct s3c_adc_client *client) |
| 193 | { |
| 194 | /* We should really check that nothing is in progress. */ |
Ramax Lo | 0c3ee07 | 2009-04-14 23:56:18 +0800 | [diff] [blame] | 195 | if (adc_dev->cur == client) |
| 196 | adc_dev->cur = NULL; |
| 197 | if (adc_dev->ts_pend == client) |
| 198 | adc_dev->ts_pend = NULL; |
| 199 | else { |
| 200 | struct list_head *p, *n; |
| 201 | struct s3c_adc_client *tmp; |
| 202 | |
| 203 | list_for_each_safe(p, n, &adc_pending) { |
| 204 | tmp = list_entry(p, struct s3c_adc_client, pend); |
| 205 | if (tmp == client) |
| 206 | list_del(&tmp->pend); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (adc_dev->cur == NULL) |
| 211 | s3c_adc_try(adc_dev); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 212 | kfree(client); |
| 213 | } |
| 214 | EXPORT_SYMBOL_GPL(s3c_adc_release); |
| 215 | |
| 216 | static irqreturn_t s3c_adc_irq(int irq, void *pw) |
| 217 | { |
| 218 | struct adc_device *adc = pw; |
| 219 | struct s3c_adc_client *client = adc->cur; |
| 220 | unsigned long flags; |
| 221 | unsigned data0, data1; |
| 222 | |
| 223 | if (!client) { |
| 224 | dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__); |
| 225 | return IRQ_HANDLED; |
| 226 | } |
| 227 | |
| 228 | data0 = readl(adc->regs + S3C2410_ADCDAT0); |
| 229 | data1 = readl(adc->regs + S3C2410_ADCDAT1); |
| 230 | adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1); |
| 231 | |
Nelson Castillo | 3f7ea46 | 2009-05-08 08:10:12 -0500 | [diff] [blame^] | 232 | client->nr_samples--; |
| 233 | (client->convert_cb)(data0 & 0x3ff, data1 & 0x3ff, &client->nr_samples); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 234 | |
Nelson Castillo | 3f7ea46 | 2009-05-08 08:10:12 -0500 | [diff] [blame^] | 235 | if (client->nr_samples > 0) { |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 236 | /* fire another conversion for this */ |
| 237 | |
| 238 | client->select_cb(1); |
| 239 | s3c_adc_convert(adc); |
| 240 | } else { |
| 241 | local_irq_save(flags); |
| 242 | (client->select_cb)(0); |
| 243 | adc->cur = NULL; |
| 244 | |
| 245 | s3c_adc_try(adc); |
| 246 | local_irq_restore(flags); |
| 247 | } |
| 248 | |
| 249 | return IRQ_HANDLED; |
| 250 | } |
| 251 | |
| 252 | static int s3c_adc_probe(struct platform_device *pdev) |
| 253 | { |
| 254 | struct device *dev = &pdev->dev; |
| 255 | struct adc_device *adc; |
| 256 | struct resource *regs; |
| 257 | int ret; |
| 258 | |
| 259 | adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL); |
| 260 | if (adc == NULL) { |
| 261 | dev_err(dev, "failed to allocate adc_device\n"); |
| 262 | return -ENOMEM; |
| 263 | } |
| 264 | |
| 265 | adc->pdev = pdev; |
| 266 | adc->prescale = S3C2410_ADCCON_PRSCVL(49); |
| 267 | |
| 268 | adc->irq = platform_get_irq(pdev, 1); |
| 269 | if (adc->irq <= 0) { |
| 270 | dev_err(dev, "failed to get adc irq\n"); |
| 271 | ret = -ENOENT; |
| 272 | goto err_alloc; |
| 273 | } |
| 274 | |
| 275 | ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc); |
| 276 | if (ret < 0) { |
| 277 | dev_err(dev, "failed to attach adc irq\n"); |
| 278 | goto err_alloc; |
| 279 | } |
| 280 | |
| 281 | adc->clk = clk_get(dev, "adc"); |
| 282 | if (IS_ERR(adc->clk)) { |
| 283 | dev_err(dev, "failed to get adc clock\n"); |
| 284 | ret = PTR_ERR(adc->clk); |
| 285 | goto err_irq; |
| 286 | } |
| 287 | |
| 288 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 289 | if (!regs) { |
| 290 | dev_err(dev, "failed to find registers\n"); |
| 291 | ret = -ENXIO; |
| 292 | goto err_clk; |
| 293 | } |
| 294 | |
| 295 | adc->regs = ioremap(regs->start, resource_size(regs)); |
| 296 | if (!adc->regs) { |
| 297 | dev_err(dev, "failed to map registers\n"); |
| 298 | ret = -ENXIO; |
| 299 | goto err_clk; |
| 300 | } |
| 301 | |
| 302 | clk_enable(adc->clk); |
| 303 | |
| 304 | writel(adc->prescale | S3C2410_ADCCON_PRSCEN, |
| 305 | adc->regs + S3C2410_ADCCON); |
| 306 | |
| 307 | dev_info(dev, "attached adc driver\n"); |
| 308 | |
| 309 | platform_set_drvdata(pdev, adc); |
| 310 | adc_dev = adc; |
| 311 | |
| 312 | return 0; |
| 313 | |
| 314 | err_clk: |
| 315 | clk_put(adc->clk); |
| 316 | |
| 317 | err_irq: |
| 318 | free_irq(adc->irq, adc); |
| 319 | |
| 320 | err_alloc: |
| 321 | kfree(adc); |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | static int s3c_adc_remove(struct platform_device *pdev) |
| 326 | { |
| 327 | struct adc_device *adc = platform_get_drvdata(pdev); |
| 328 | |
| 329 | iounmap(adc->regs); |
| 330 | free_irq(adc->irq, adc); |
| 331 | clk_disable(adc->clk); |
| 332 | clk_put(adc->clk); |
| 333 | kfree(adc); |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | #ifdef CONFIG_PM |
| 339 | static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state) |
| 340 | { |
| 341 | struct adc_device *adc = platform_get_drvdata(pdev); |
| 342 | u32 con; |
| 343 | |
| 344 | con = readl(adc->regs + S3C2410_ADCCON); |
| 345 | con |= S3C2410_ADCCON_STDBM; |
| 346 | writel(con, adc->regs + S3C2410_ADCCON); |
| 347 | |
| 348 | clk_disable(adc->clk); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int s3c_adc_resume(struct platform_device *pdev) |
| 354 | { |
| 355 | struct adc_device *adc = platform_get_drvdata(pdev); |
| 356 | |
| 357 | clk_enable(adc->clk); |
| 358 | |
| 359 | writel(adc->prescale | S3C2410_ADCCON_PRSCEN, |
| 360 | adc->regs + S3C2410_ADCCON); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | #else |
| 366 | #define s3c_adc_suspend NULL |
| 367 | #define s3c_adc_resume NULL |
| 368 | #endif |
| 369 | |
| 370 | static struct platform_driver s3c_adc_driver = { |
| 371 | .driver = { |
| 372 | .name = "s3c24xx-adc", |
| 373 | .owner = THIS_MODULE, |
| 374 | }, |
| 375 | .probe = s3c_adc_probe, |
| 376 | .remove = __devexit_p(s3c_adc_remove), |
| 377 | .suspend = s3c_adc_suspend, |
| 378 | .resume = s3c_adc_resume, |
| 379 | }; |
| 380 | |
| 381 | static int __init adc_init(void) |
| 382 | { |
| 383 | int ret; |
| 384 | |
| 385 | ret = platform_driver_register(&s3c_adc_driver); |
| 386 | if (ret) |
| 387 | printk(KERN_ERR "%s: failed to add adc driver\n", __func__); |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | arch_initcall(adc_init); |