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