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