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