Lines Matching +full:adc +full:- +full:dev
1 // SPDX-License-Identifier: GPL-1.0+
5 // Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
7 // Samsung ADC device core
22 #include "regs-adc.h"
23 #include <linux/soc/samsung/s3c-adc.h>
25 /* This driver is designed to control the usage of the ADC block between
81 #define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)
83 static inline void s3c_adc_convert(struct adc_device *adc) in s3c_adc_convert() argument
85 unsigned con = readl(adc->regs + S3C2410_ADCCON); in s3c_adc_convert()
88 writel(con, adc->regs + S3C2410_ADCCON); in s3c_adc_convert()
91 static inline void s3c_adc_select(struct adc_device *adc, in s3c_adc_select() argument
94 unsigned con = readl(adc->regs + S3C2410_ADCCON); in s3c_adc_select()
95 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data; in s3c_adc_select()
97 client->select_cb(client, 1); in s3c_adc_select()
104 if (!client->is_ts) { in s3c_adc_select()
106 writel(client->channel & 0xf, adc->regs + S5P_ADCMUX); in s3c_adc_select()
108 writel(client->channel & 0xf, in s3c_adc_select()
109 adc->regs + S3C2443_ADCMUX); in s3c_adc_select()
111 con |= S3C2410_ADCCON_SELMUX(client->channel); in s3c_adc_select()
114 writel(con, adc->regs + S3C2410_ADCCON); in s3c_adc_select()
117 static void s3c_adc_dbgshow(struct adc_device *adc) in s3c_adc_dbgshow() argument
119 adc_dbg(adc, "CON=%08x, TSC=%08x, DLY=%08x\n", in s3c_adc_dbgshow()
120 readl(adc->regs + S3C2410_ADCCON), in s3c_adc_dbgshow()
121 readl(adc->regs + S3C2410_ADCTSC), in s3c_adc_dbgshow()
122 readl(adc->regs + S3C2410_ADCDLY)); in s3c_adc_dbgshow()
125 static void s3c_adc_try(struct adc_device *adc) in s3c_adc_try() argument
127 struct s3c_adc_client *next = adc->ts_pend; in s3c_adc_try()
132 list_del(&next->pend); in s3c_adc_try()
134 adc->ts_pend = NULL; in s3c_adc_try()
137 adc_dbg(adc, "new client is %p\n", next); in s3c_adc_try()
138 adc->cur = next; in s3c_adc_try()
139 s3c_adc_select(adc, next); in s3c_adc_try()
140 s3c_adc_convert(adc); in s3c_adc_try()
141 s3c_adc_dbgshow(adc); in s3c_adc_try()
148 struct adc_device *adc = adc_dev; in s3c_adc_start() local
151 if (!adc) { in s3c_adc_start()
152 printk(KERN_ERR "%s: failed to find adc\n", __func__); in s3c_adc_start()
153 return -EINVAL; in s3c_adc_start()
156 spin_lock_irqsave(&adc->lock, flags); in s3c_adc_start()
158 if (client->is_ts && adc->ts_pend) { in s3c_adc_start()
159 spin_unlock_irqrestore(&adc->lock, flags); in s3c_adc_start()
160 return -EAGAIN; in s3c_adc_start()
163 client->channel = channel; in s3c_adc_start()
164 client->nr_samples = nr_samples; in s3c_adc_start()
166 if (client->is_ts) in s3c_adc_start()
167 adc->ts_pend = client; in s3c_adc_start()
169 list_add_tail(&client->pend, &adc_pending); in s3c_adc_start()
171 if (!adc->cur) in s3c_adc_start()
172 s3c_adc_try(adc); in s3c_adc_start()
174 spin_unlock_irqrestore(&adc->lock, flags); in s3c_adc_start()
183 client->result = v; in s3c_convert_done()
184 wake_up(client->wait); in s3c_convert_done()
192 client->convert_cb = s3c_convert_done; in s3c_adc_read()
193 client->wait = &wake; in s3c_adc_read()
194 client->result = -1; in s3c_adc_read()
200 ret = wait_event_timeout(wake, client->result >= 0, HZ / 2); in s3c_adc_read()
201 if (client->result < 0) { in s3c_adc_read()
202 ret = -ETIMEDOUT; in s3c_adc_read()
206 client->convert_cb = NULL; in s3c_adc_read()
207 return client->result; in s3c_adc_read()
235 return ERR_PTR(-EINVAL); in s3c_adc_register()
239 return ERR_PTR(-ENOMEM); in s3c_adc_register()
241 client->pdev = pdev; in s3c_adc_register()
242 client->is_ts = is_ts; in s3c_adc_register()
243 client->select_cb = select; in s3c_adc_register()
244 client->convert_cb = conv; in s3c_adc_register()
254 spin_lock_irqsave(&adc_dev->lock, flags); in s3c_adc_release()
257 if (adc_dev->cur == client) in s3c_adc_release()
258 adc_dev->cur = NULL; in s3c_adc_release()
259 if (adc_dev->ts_pend == client) in s3c_adc_release()
260 adc_dev->ts_pend = NULL; in s3c_adc_release()
268 list_del(&tmp->pend); in s3c_adc_release()
272 if (adc_dev->cur == NULL) in s3c_adc_release()
275 spin_unlock_irqrestore(&adc_dev->lock, flags); in s3c_adc_release()
282 struct adc_device *adc = pw; in s3c_adc_irq() local
283 struct s3c_adc_client *client = adc->cur; in s3c_adc_irq()
284 enum s3c_cpu_type cpu = platform_get_device_id(adc->pdev)->driver_data; in s3c_adc_irq()
288 dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__); in s3c_adc_irq()
292 data0 = readl(adc->regs + S3C2410_ADCDAT0); in s3c_adc_irq()
293 data1 = readl(adc->regs + S3C2410_ADCDAT1); in s3c_adc_irq()
294 adc_dbg(adc, "read %d: 0x%04x, 0x%04x\n", client->nr_samples, data0, data1); in s3c_adc_irq()
296 client->nr_samples--; in s3c_adc_irq()
302 /* S3C2416/S3C64XX/S5P ADC resolution is 12-bit */ in s3c_adc_irq()
307 if (client->convert_cb) in s3c_adc_irq()
308 (client->convert_cb)(client, data0, data1, &client->nr_samples); in s3c_adc_irq()
310 if (client->nr_samples > 0) { in s3c_adc_irq()
313 client->select_cb(client, 1); in s3c_adc_irq()
314 s3c_adc_convert(adc); in s3c_adc_irq()
316 spin_lock(&adc->lock); in s3c_adc_irq()
317 (client->select_cb)(client, 0); in s3c_adc_irq()
318 adc->cur = NULL; in s3c_adc_irq()
320 s3c_adc_try(adc); in s3c_adc_irq()
321 spin_unlock(&adc->lock); in s3c_adc_irq()
326 /* Clear ADC interrupt */ in s3c_adc_irq()
327 writel(0, adc->regs + S3C64XX_ADCCLRINT); in s3c_adc_irq()
334 struct device *dev = &pdev->dev; in s3c_adc_probe() local
335 struct adc_device *adc; in s3c_adc_probe() local
336 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data; in s3c_adc_probe()
340 adc = devm_kzalloc(dev, sizeof(*adc), GFP_KERNEL); in s3c_adc_probe()
341 if (!adc) in s3c_adc_probe()
342 return -ENOMEM; in s3c_adc_probe()
344 spin_lock_init(&adc->lock); in s3c_adc_probe()
346 adc->pdev = pdev; in s3c_adc_probe()
347 adc->prescale = S3C2410_ADCCON_PRSCVL(49); in s3c_adc_probe()
349 adc->vdd = devm_regulator_get(dev, "vdd"); in s3c_adc_probe()
350 if (IS_ERR(adc->vdd)) { in s3c_adc_probe()
351 dev_err(dev, "operating without regulator \"vdd\" .\n"); in s3c_adc_probe()
352 return PTR_ERR(adc->vdd); in s3c_adc_probe()
355 adc->irq = platform_get_irq(pdev, 1); in s3c_adc_probe()
356 if (adc->irq <= 0) in s3c_adc_probe()
357 return -ENOENT; in s3c_adc_probe()
359 ret = devm_request_irq(dev, adc->irq, s3c_adc_irq, 0, dev_name(dev), in s3c_adc_probe()
360 adc); in s3c_adc_probe()
362 dev_err(dev, "failed to attach adc irq\n"); in s3c_adc_probe()
366 adc->clk = devm_clk_get(dev, "adc"); in s3c_adc_probe()
367 if (IS_ERR(adc->clk)) { in s3c_adc_probe()
368 dev_err(dev, "failed to get adc clock\n"); in s3c_adc_probe()
369 return PTR_ERR(adc->clk); in s3c_adc_probe()
372 adc->regs = devm_platform_ioremap_resource(pdev, 0); in s3c_adc_probe()
373 if (IS_ERR(adc->regs)) in s3c_adc_probe()
374 return PTR_ERR(adc->regs); in s3c_adc_probe()
376 ret = regulator_enable(adc->vdd); in s3c_adc_probe()
380 clk_prepare_enable(adc->clk); in s3c_adc_probe()
382 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN; in s3c_adc_probe()
384 /* Enable 12-bit ADC resolution */ in s3c_adc_probe()
390 writel(tmp, adc->regs + S3C2410_ADCCON); in s3c_adc_probe()
392 dev_info(dev, "attached adc driver\n"); in s3c_adc_probe()
394 platform_set_drvdata(pdev, adc); in s3c_adc_probe()
395 adc_dev = adc; in s3c_adc_probe()
402 struct adc_device *adc = platform_get_drvdata(pdev); in s3c_adc_remove() local
404 clk_disable_unprepare(adc->clk); in s3c_adc_remove()
405 regulator_disable(adc->vdd); in s3c_adc_remove()
411 static int s3c_adc_suspend(struct device *dev) in s3c_adc_suspend() argument
413 struct adc_device *adc = dev_get_drvdata(dev); in s3c_adc_suspend() local
417 spin_lock_irqsave(&adc->lock, flags); in s3c_adc_suspend()
419 con = readl(adc->regs + S3C2410_ADCCON); in s3c_adc_suspend()
421 writel(con, adc->regs + S3C2410_ADCCON); in s3c_adc_suspend()
423 disable_irq(adc->irq); in s3c_adc_suspend()
424 spin_unlock_irqrestore(&adc->lock, flags); in s3c_adc_suspend()
425 clk_disable(adc->clk); in s3c_adc_suspend()
426 regulator_disable(adc->vdd); in s3c_adc_suspend()
431 static int s3c_adc_resume(struct device *dev) in s3c_adc_resume() argument
433 struct platform_device *pdev = to_platform_device(dev); in s3c_adc_resume()
434 struct adc_device *adc = platform_get_drvdata(pdev); in s3c_adc_resume() local
435 enum s3c_cpu_type cpu = platform_get_device_id(pdev)->driver_data; in s3c_adc_resume()
439 ret = regulator_enable(adc->vdd); in s3c_adc_resume()
442 clk_enable(adc->clk); in s3c_adc_resume()
443 enable_irq(adc->irq); in s3c_adc_resume()
445 tmp = adc->prescale | S3C2410_ADCCON_PRSCEN; in s3c_adc_resume()
447 /* Enable 12-bit ADC resolution */ in s3c_adc_resume()
453 writel(tmp, adc->regs + S3C2410_ADCCON); in s3c_adc_resume()
465 .name = "s3c24xx-adc",
468 .name = "s3c2443-adc",
471 .name = "s3c2416-adc",
474 .name = "s3c64xx-adc",
477 .name = "samsung-adc-v3",
492 .name = "s3c-adc",
505 printk(KERN_ERR "%s: failed to add adc driver\n", __func__); in adc_init()