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