Maurus Cuelenaere | 3929e1e | 2010-01-14 00:30:31 +0100 | [diff] [blame] | 1 | /* arch/arm/plat-samsung/adc.c |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 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 | * |
Maurus Cuelenaere | 3929e1e | 2010-01-14 00:30:31 +0100 | [diff] [blame] | 7 | * Samsung ADC device core |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 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> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 17 | #include <linux/sched.h> |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 18 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 20 | #include <linux/err.h> |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/io.h> |
| 24 | |
| 25 | #include <plat/regs-adc.h> |
| 26 | #include <plat/adc.h> |
| 27 | |
| 28 | /* This driver is designed to control the usage of the ADC block between |
| 29 | * the touchscreen and any other drivers that may need to use it, such as |
| 30 | * the hwmon driver. |
| 31 | * |
| 32 | * Priority will be given to the touchscreen driver, but as this itself is |
| 33 | * rate limited it should not starve other requests which are processed in |
| 34 | * order that they are received. |
| 35 | * |
| 36 | * Each user registers to get a client block which uniquely identifies it |
| 37 | * and stores information such as the necessary functions to callback when |
| 38 | * action is required. |
| 39 | */ |
| 40 | |
Maurus Cuelenaere | bcedfa9 | 2010-01-14 00:30:34 +0100 | [diff] [blame] | 41 | enum s3c_cpu_type { |
| 42 | TYPE_S3C24XX, |
| 43 | TYPE_S3C64XX |
| 44 | }; |
| 45 | |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 46 | struct s3c_adc_client { |
| 47 | struct platform_device *pdev; |
| 48 | struct list_head pend; |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 49 | wait_queue_head_t *wait; |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 50 | |
| 51 | unsigned int nr_samples; |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 52 | int result; |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 53 | unsigned char is_ts; |
| 54 | unsigned char channel; |
| 55 | |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 56 | void (*select_cb)(struct s3c_adc_client *c, unsigned selected); |
| 57 | void (*convert_cb)(struct s3c_adc_client *c, |
| 58 | unsigned val1, unsigned val2, |
Nelson Castillo | 3f7ea46 | 2009-05-08 08:10:12 -0500 | [diff] [blame] | 59 | unsigned *samples_left); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | struct adc_device { |
| 63 | struct platform_device *pdev; |
| 64 | struct platform_device *owner; |
| 65 | struct clk *clk; |
| 66 | struct s3c_adc_client *cur; |
| 67 | struct s3c_adc_client *ts_pend; |
| 68 | void __iomem *regs; |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 69 | spinlock_t lock; |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 70 | |
| 71 | unsigned int prescale; |
| 72 | |
| 73 | int irq; |
| 74 | }; |
| 75 | |
| 76 | static struct adc_device *adc_dev; |
| 77 | |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 78 | static LIST_HEAD(adc_pending); /* protected by adc_device.lock */ |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 79 | |
| 80 | #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg) |
| 81 | |
| 82 | static inline void s3c_adc_convert(struct adc_device *adc) |
| 83 | { |
| 84 | unsigned con = readl(adc->regs + S3C2410_ADCCON); |
| 85 | |
| 86 | con |= S3C2410_ADCCON_ENABLE_START; |
| 87 | writel(con, adc->regs + S3C2410_ADCCON); |
| 88 | } |
| 89 | |
| 90 | static inline void s3c_adc_select(struct adc_device *adc, |
| 91 | struct s3c_adc_client *client) |
| 92 | { |
| 93 | unsigned con = readl(adc->regs + S3C2410_ADCCON); |
| 94 | |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 95 | client->select_cb(client, 1); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 96 | |
| 97 | con &= ~S3C2410_ADCCON_MUXMASK; |
| 98 | con &= ~S3C2410_ADCCON_STDBM; |
| 99 | con &= ~S3C2410_ADCCON_STARTMASK; |
| 100 | |
| 101 | if (!client->is_ts) |
| 102 | con |= S3C2410_ADCCON_SELMUX(client->channel); |
| 103 | |
| 104 | writel(con, adc->regs + S3C2410_ADCCON); |
| 105 | } |
| 106 | |
| 107 | static void s3c_adc_dbgshow(struct adc_device *adc) |
| 108 | { |
| 109 | adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n", |
| 110 | readl(adc->regs + S3C2410_ADCCON), |
| 111 | readl(adc->regs + S3C2410_ADCTSC), |
| 112 | readl(adc->regs + S3C2410_ADCDLY)); |
| 113 | } |
| 114 | |
Ben Dooks | f8c8ac8 | 2009-04-17 12:36:49 +0100 | [diff] [blame] | 115 | static void s3c_adc_try(struct adc_device *adc) |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 116 | { |
| 117 | struct s3c_adc_client *next = adc->ts_pend; |
| 118 | |
| 119 | if (!next && !list_empty(&adc_pending)) { |
| 120 | next = list_first_entry(&adc_pending, |
| 121 | struct s3c_adc_client, pend); |
| 122 | list_del(&next->pend); |
| 123 | } else |
| 124 | adc->ts_pend = NULL; |
| 125 | |
| 126 | if (next) { |
| 127 | adc_dbg(adc, "new client is %p\n", next); |
| 128 | adc->cur = next; |
| 129 | s3c_adc_select(adc, next); |
| 130 | s3c_adc_convert(adc); |
| 131 | s3c_adc_dbgshow(adc); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | int s3c_adc_start(struct s3c_adc_client *client, |
| 136 | unsigned int channel, unsigned int nr_samples) |
| 137 | { |
| 138 | struct adc_device *adc = adc_dev; |
| 139 | unsigned long flags; |
| 140 | |
| 141 | if (!adc) { |
| 142 | printk(KERN_ERR "%s: failed to find adc\n", __func__); |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | |
| 146 | if (client->is_ts && adc->ts_pend) |
| 147 | return -EAGAIN; |
| 148 | |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 149 | spin_lock_irqsave(&adc->lock, flags); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 150 | |
| 151 | client->channel = channel; |
| 152 | client->nr_samples = nr_samples; |
| 153 | |
| 154 | if (client->is_ts) |
| 155 | adc->ts_pend = client; |
| 156 | else |
| 157 | list_add_tail(&client->pend, &adc_pending); |
| 158 | |
| 159 | if (!adc->cur) |
| 160 | s3c_adc_try(adc); |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 161 | |
| 162 | spin_unlock_irqrestore(&adc->lock, flags); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(s3c_adc_start); |
| 167 | |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 168 | static void s3c_convert_done(struct s3c_adc_client *client, |
| 169 | unsigned v, unsigned u, unsigned *left) |
| 170 | { |
| 171 | client->result = v; |
| 172 | wake_up(client->wait); |
| 173 | } |
| 174 | |
| 175 | int s3c_adc_read(struct s3c_adc_client *client, unsigned int ch) |
| 176 | { |
| 177 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake); |
| 178 | int ret; |
| 179 | |
| 180 | client->convert_cb = s3c_convert_done; |
| 181 | client->wait = &wake; |
| 182 | client->result = -1; |
| 183 | |
| 184 | ret = s3c_adc_start(client, ch, 1); |
| 185 | if (ret < 0) |
| 186 | goto err; |
| 187 | |
| 188 | ret = wait_event_timeout(wake, client->result >= 0, HZ / 2); |
| 189 | if (client->result < 0) { |
| 190 | ret = -ETIMEDOUT; |
| 191 | goto err; |
| 192 | } |
| 193 | |
| 194 | client->convert_cb = NULL; |
| 195 | return client->result; |
| 196 | |
| 197 | err: |
| 198 | return ret; |
| 199 | } |
Ryan Mallon | d3bf395 | 2009-10-14 09:18:30 +1300 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(s3c_adc_read); |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 201 | |
| 202 | static void s3c_adc_default_select(struct s3c_adc_client *client, |
| 203 | unsigned select) |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 204 | { |
| 205 | } |
| 206 | |
| 207 | struct s3c_adc_client *s3c_adc_register(struct platform_device *pdev, |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 208 | void (*select)(struct s3c_adc_client *client, |
| 209 | unsigned int selected), |
| 210 | void (*conv)(struct s3c_adc_client *client, |
| 211 | unsigned d0, unsigned d1, |
Nelson Castillo | 3f7ea46 | 2009-05-08 08:10:12 -0500 | [diff] [blame] | 212 | unsigned *samples_left), |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 213 | unsigned int is_ts) |
| 214 | { |
| 215 | struct s3c_adc_client *client; |
| 216 | |
| 217 | WARN_ON(!pdev); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 218 | |
| 219 | if (!select) |
| 220 | select = s3c_adc_default_select; |
| 221 | |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 222 | if (!pdev) |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 223 | return ERR_PTR(-EINVAL); |
| 224 | |
| 225 | client = kzalloc(sizeof(struct s3c_adc_client), GFP_KERNEL); |
| 226 | if (!client) { |
| 227 | dev_err(&pdev->dev, "no memory for adc client\n"); |
| 228 | return ERR_PTR(-ENOMEM); |
| 229 | } |
| 230 | |
| 231 | client->pdev = pdev; |
| 232 | client->is_ts = is_ts; |
| 233 | client->select_cb = select; |
| 234 | client->convert_cb = conv; |
| 235 | |
| 236 | return client; |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(s3c_adc_register); |
| 239 | |
| 240 | void s3c_adc_release(struct s3c_adc_client *client) |
| 241 | { |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 242 | unsigned long flags; |
| 243 | |
| 244 | spin_lock_irqsave(&adc_dev->lock, flags); |
| 245 | |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 246 | /* We should really check that nothing is in progress. */ |
Ramax Lo | 0c3ee07 | 2009-04-14 23:56:18 +0800 | [diff] [blame] | 247 | if (adc_dev->cur == client) |
| 248 | adc_dev->cur = NULL; |
| 249 | if (adc_dev->ts_pend == client) |
| 250 | adc_dev->ts_pend = NULL; |
| 251 | else { |
| 252 | struct list_head *p, *n; |
| 253 | struct s3c_adc_client *tmp; |
| 254 | |
| 255 | list_for_each_safe(p, n, &adc_pending) { |
| 256 | tmp = list_entry(p, struct s3c_adc_client, pend); |
| 257 | if (tmp == client) |
| 258 | list_del(&tmp->pend); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (adc_dev->cur == NULL) |
| 263 | s3c_adc_try(adc_dev); |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 264 | |
| 265 | spin_unlock_irqrestore(&adc_dev->lock, flags); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 266 | kfree(client); |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(s3c_adc_release); |
| 269 | |
| 270 | static irqreturn_t s3c_adc_irq(int irq, void *pw) |
| 271 | { |
| 272 | struct adc_device *adc = pw; |
| 273 | struct s3c_adc_client *client = adc->cur; |
Maurus Cuelenaere | 91492b4 | 2010-01-30 18:01:48 +0100 | [diff] [blame] | 274 | enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data; |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 275 | unsigned data0, data1; |
| 276 | |
| 277 | if (!client) { |
| 278 | dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__); |
Maurus Cuelenaere | bcedfa9 | 2010-01-14 00:30:34 +0100 | [diff] [blame] | 279 | goto exit; |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | data0 = readl(adc->regs + S3C2410_ADCDAT0); |
| 283 | data1 = readl(adc->regs + S3C2410_ADCDAT1); |
| 284 | adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1); |
| 285 | |
Nelson Castillo | 3f7ea46 | 2009-05-08 08:10:12 -0500 | [diff] [blame] | 286 | client->nr_samples--; |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 287 | |
Maurus Cuelenaere | 91492b4 | 2010-01-30 18:01:48 +0100 | [diff] [blame] | 288 | if (cpu == TYPE_S3C64XX) { |
| 289 | /* S3C64XX ADC resolution is 12-bit */ |
| 290 | data0 &= 0xfff; |
| 291 | data1 &= 0xfff; |
| 292 | } else { |
| 293 | data0 &= 0x3ff; |
| 294 | data1 &= 0x3ff; |
| 295 | } |
| 296 | |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 297 | if (client->convert_cb) |
Maurus Cuelenaere | 91492b4 | 2010-01-30 18:01:48 +0100 | [diff] [blame] | 298 | (client->convert_cb)(client, data0, data1, &client->nr_samples); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 299 | |
Nelson Castillo | 3f7ea46 | 2009-05-08 08:10:12 -0500 | [diff] [blame] | 300 | if (client->nr_samples > 0) { |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 301 | /* fire another conversion for this */ |
| 302 | |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 303 | client->select_cb(client, 1); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 304 | s3c_adc_convert(adc); |
| 305 | } else { |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 306 | spin_lock(&adc->lock); |
Ben Dooks | e170adc | 2009-07-18 10:12:27 +0100 | [diff] [blame] | 307 | (client->select_cb)(client, 0); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 308 | adc->cur = NULL; |
| 309 | |
| 310 | s3c_adc_try(adc); |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 311 | spin_unlock(&adc->lock); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Maurus Cuelenaere | bcedfa9 | 2010-01-14 00:30:34 +0100 | [diff] [blame] | 314 | exit: |
Maurus Cuelenaere | 91492b4 | 2010-01-30 18:01:48 +0100 | [diff] [blame] | 315 | if (cpu == TYPE_S3C64XX) { |
Maurus Cuelenaere | bcedfa9 | 2010-01-14 00:30:34 +0100 | [diff] [blame] | 316 | /* Clear ADC interrupt */ |
| 317 | writel(0, adc->regs + S3C64XX_ADCCLRINT); |
| 318 | } |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 319 | return IRQ_HANDLED; |
| 320 | } |
| 321 | |
| 322 | static int s3c_adc_probe(struct platform_device *pdev) |
| 323 | { |
| 324 | struct device *dev = &pdev->dev; |
| 325 | struct adc_device *adc; |
| 326 | struct resource *regs; |
| 327 | int ret; |
Maurus Cuelenaere | 91492b4 | 2010-01-30 18:01:48 +0100 | [diff] [blame] | 328 | unsigned tmp; |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 329 | |
| 330 | adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL); |
| 331 | if (adc == NULL) { |
| 332 | dev_err(dev, "failed to allocate adc_device\n"); |
| 333 | return -ENOMEM; |
| 334 | } |
| 335 | |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 336 | spin_lock_init(&adc->lock); |
| 337 | |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 338 | adc->pdev = pdev; |
| 339 | adc->prescale = S3C2410_ADCCON_PRSCVL(49); |
| 340 | |
| 341 | adc->irq = platform_get_irq(pdev, 1); |
| 342 | if (adc->irq <= 0) { |
| 343 | dev_err(dev, "failed to get adc irq\n"); |
| 344 | ret = -ENOENT; |
| 345 | goto err_alloc; |
| 346 | } |
| 347 | |
| 348 | ret = request_irq(adc->irq, s3c_adc_irq, 0, dev_name(dev), adc); |
| 349 | if (ret < 0) { |
| 350 | dev_err(dev, "failed to attach adc irq\n"); |
| 351 | goto err_alloc; |
| 352 | } |
| 353 | |
| 354 | adc->clk = clk_get(dev, "adc"); |
| 355 | if (IS_ERR(adc->clk)) { |
| 356 | dev_err(dev, "failed to get adc clock\n"); |
| 357 | ret = PTR_ERR(adc->clk); |
| 358 | goto err_irq; |
| 359 | } |
| 360 | |
| 361 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 362 | if (!regs) { |
| 363 | dev_err(dev, "failed to find registers\n"); |
| 364 | ret = -ENXIO; |
| 365 | goto err_clk; |
| 366 | } |
| 367 | |
| 368 | adc->regs = ioremap(regs->start, resource_size(regs)); |
| 369 | if (!adc->regs) { |
| 370 | dev_err(dev, "failed to map registers\n"); |
| 371 | ret = -ENXIO; |
| 372 | goto err_clk; |
| 373 | } |
| 374 | |
| 375 | clk_enable(adc->clk); |
| 376 | |
Maurus Cuelenaere | 91492b4 | 2010-01-30 18:01:48 +0100 | [diff] [blame] | 377 | tmp = adc->prescale | S3C2410_ADCCON_PRSCEN; |
| 378 | if (platform_get_device_id(pdev)->driver_data == TYPE_S3C64XX) { |
| 379 | /* Enable 12-bit ADC resolution */ |
| 380 | tmp |= S3C64XX_ADCCON_RESSEL; |
| 381 | } |
| 382 | writel(tmp, adc->regs + S3C2410_ADCCON); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 383 | |
| 384 | dev_info(dev, "attached adc driver\n"); |
| 385 | |
| 386 | platform_set_drvdata(pdev, adc); |
| 387 | adc_dev = adc; |
| 388 | |
| 389 | return 0; |
| 390 | |
| 391 | err_clk: |
| 392 | clk_put(adc->clk); |
| 393 | |
| 394 | err_irq: |
| 395 | free_irq(adc->irq, adc); |
| 396 | |
| 397 | err_alloc: |
| 398 | kfree(adc); |
| 399 | return ret; |
| 400 | } |
| 401 | |
Uwe Kleine-König | ad4e22f | 2009-11-24 22:07:10 +0100 | [diff] [blame] | 402 | static int __devexit s3c_adc_remove(struct platform_device *pdev) |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 403 | { |
| 404 | struct adc_device *adc = platform_get_drvdata(pdev); |
| 405 | |
| 406 | iounmap(adc->regs); |
| 407 | free_irq(adc->irq, adc); |
| 408 | clk_disable(adc->clk); |
| 409 | clk_put(adc->clk); |
| 410 | kfree(adc); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | #ifdef CONFIG_PM |
| 416 | static int s3c_adc_suspend(struct platform_device *pdev, pm_message_t state) |
| 417 | { |
| 418 | struct adc_device *adc = platform_get_drvdata(pdev); |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 419 | unsigned long flags; |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 420 | u32 con; |
| 421 | |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 422 | spin_lock_irqsave(&adc->lock, flags); |
| 423 | |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 424 | con = readl(adc->regs + S3C2410_ADCCON); |
| 425 | con |= S3C2410_ADCCON_STDBM; |
| 426 | writel(con, adc->regs + S3C2410_ADCCON); |
| 427 | |
Vasily Khoruzhick | a0af8b3 | 2010-02-18 18:32:29 +0200 | [diff] [blame] | 428 | disable_irq(adc->irq); |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 429 | spin_unlock_irqrestore(&adc->lock, flags); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 430 | clk_disable(adc->clk); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int s3c_adc_resume(struct platform_device *pdev) |
| 436 | { |
| 437 | struct adc_device *adc = platform_get_drvdata(pdev); |
Ben Dooks | 1f1f584 | 2010-05-10 13:31:36 +0900 | [diff] [blame] | 438 | unsigned long flags; |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 439 | |
| 440 | clk_enable(adc->clk); |
Vasily Khoruzhick | a0af8b3 | 2010-02-18 18:32:29 +0200 | [diff] [blame] | 441 | enable_irq(adc->irq); |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 442 | |
| 443 | writel(adc->prescale | S3C2410_ADCCON_PRSCEN, |
| 444 | adc->regs + S3C2410_ADCCON); |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | #else |
| 450 | #define s3c_adc_suspend NULL |
| 451 | #define s3c_adc_resume NULL |
| 452 | #endif |
| 453 | |
Maurus Cuelenaere | bcedfa9 | 2010-01-14 00:30:34 +0100 | [diff] [blame] | 454 | static struct platform_device_id s3c_adc_driver_ids[] = { |
| 455 | { |
| 456 | .name = "s3c24xx-adc", |
| 457 | .driver_data = TYPE_S3C24XX, |
| 458 | }, { |
| 459 | .name = "s3c64xx-adc", |
| 460 | .driver_data = TYPE_S3C64XX, |
| 461 | }, |
| 462 | { } |
| 463 | }; |
| 464 | MODULE_DEVICE_TABLE(platform, s3c_adc_driver_ids); |
| 465 | |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 466 | static struct platform_driver s3c_adc_driver = { |
Maurus Cuelenaere | bcedfa9 | 2010-01-14 00:30:34 +0100 | [diff] [blame] | 467 | .id_table = s3c_adc_driver_ids, |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 468 | .driver = { |
Maurus Cuelenaere | bcedfa9 | 2010-01-14 00:30:34 +0100 | [diff] [blame] | 469 | .name = "s3c-adc", |
Ben Dooks | 28ab44c5b | 2008-12-18 14:20:04 +0000 | [diff] [blame] | 470 | .owner = THIS_MODULE, |
| 471 | }, |
| 472 | .probe = s3c_adc_probe, |
| 473 | .remove = __devexit_p(s3c_adc_remove), |
| 474 | .suspend = s3c_adc_suspend, |
| 475 | .resume = s3c_adc_resume, |
| 476 | }; |
| 477 | |
| 478 | static int __init adc_init(void) |
| 479 | { |
| 480 | int ret; |
| 481 | |
| 482 | ret = platform_driver_register(&s3c_adc_driver); |
| 483 | if (ret) |
| 484 | printk(KERN_ERR "%s: failed to add adc driver\n", __func__); |
| 485 | |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | arch_initcall(adc_init); |