--- /dev/null
+/* drivers/adc/chips/rk30_adc.c\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License.\r
+*/\r
+#include <linux/kernel.h>\r
+#include <linux/module.h>\r
+#include <linux/init.h>\r
+#include <linux/device.h>\r
+#include <linux/platform_device.h>\r
+#include <linux/err.h>\r
+#include <linux/clk.h>\r
+#include <linux/interrupt.h>\r
+#include <linux/io.h>\r
+#include <linux/adc.h>\r
+#include <linux/delay.h>\r
+#include <linux/slab.h>\r
+\r
+\r
+#include "rk30_adc.h"\r
+\r
+//#define ADC_TEST\r
+\r
+struct rk30_adc_device {\r
+ int irq;\r
+ void __iomem *regs;\r
+ struct clk * clk;\r
+ struct resource *ioarea;\r
+ struct adc_host *adc;\r
+};\r
+static void rk30_adc_start(struct adc_host *adc)\r
+{\r
+ struct rk30_adc_device *dev = adc_priv(adc);\r
+ int chn = adc->cur->chn;\r
+\r
+ writel(0, dev->regs + ADC_CTRL);\r
+ writel(ADC_CTRL_POWER_UP|ADC_CTRL_CH(chn), dev->regs + ADC_CTRL);\r
+ udelay(SAMPLE_RATE);\r
+\r
+ writel(readl(dev->regs + ADC_CTRL)|ADC_CTRL_IRQ_ENABLE|ADC_CTRL_START, \r
+ dev->regs + ADC_CTRL);\r
+ return;\r
+}\r
+static void rk30_adc_stop(struct adc_host *adc)\r
+{\r
+ struct rk30_adc_device *dev = adc_priv(adc);\r
+ \r
+ writel(0, dev->regs + ADC_CTRL);\r
+}\r
+static int rk30_adc_read(struct adc_host *adc)\r
+{\r
+ struct rk30_adc_device *dev = adc_priv(adc);\r
+\r
+ udelay(SAMPLE_RATE);\r
+ return readl(dev->regs + ADC_DATA) & ADC_DATA_MASK;\r
+}\r
+static irqreturn_t rk30_adc_irq(int irq, void *data)\r
+{\r
+ struct rk30_adc_device *dev = data;\r
+ adc_core_irq_handle(dev->adc);\r
+ return IRQ_HANDLED;\r
+}\r
+static const struct adc_ops rk30_adc_ops = {\r
+ .start = rk30_adc_start,\r
+ .stop = rk30_adc_stop,\r
+ .read = rk30_adc_read,\r
+};\r
+#ifdef ADC_TEST\r
+struct adc_test_data {\r
+ struct adc_client *client;\r
+ struct timer_list timer;\r
+ struct work_struct timer_work;\r
+};\r
+static void callback(struct adc_client *client, void *param, int result)\r
+{\r
+ dev_info(client->adc->dev, "[chn%d] async_read = %d\n", client->chn, result);\r
+ return;\r
+}\r
+static void adc_timer(unsigned long data)\r
+{\r
+ //int sync_read = 0;\r
+ struct adc_test_data *test=(struct adc_test_data *)data;\r
+ \r
+ //sync_read = adc_sync_read(test->client);\r
+ //dev_info(test->client->adc->dev, "[chn%d] sync_read = %d\n", 0, sync_read);\r
+ schedule_work(&test->timer_work);\r
+ add_timer(&test->timer);\r
+}\r
+static void adc_timer_work(struct work_struct *work)\r
+{ \r
+ int sync_read = 0;\r
+ struct adc_test_data *test = container_of(work, struct adc_test_data,\r
+ timer_work);\r
+ adc_async_read(test->client);\r
+ sync_read = adc_sync_read(test->client);\r
+ dev_info(test->client->adc->dev, "[chn%d] sync_read = %d\n", 0, sync_read);\r
+}\r
+\r
+static int rk30_adc_test(void)\r
+{\r
+ struct adc_test_data *test = NULL;\r
+\r
+ test = kzalloc(sizeof(struct adc_test_data), GFP_KERNEL);\r
+ \r
+ test->client = adc_register(0, callback, NULL);\r
+ INIT_WORK(&test->timer_work, adc_timer_work);\r
+ setup_timer(&test->timer, adc_timer, (unsigned long)test);\r
+ test->timer.expires = jiffies + 100;\r
+ add_timer(&test->timer);\r
+ \r
+ return 0;\r
+\r
+}\r
+#endif\r
+\r
+static int rk30_adc_probe(struct platform_device *pdev)\r
+{\r
+ struct adc_host *adc = NULL;\r
+ struct rk30_adc_device *dev;\r
+ struct resource *res;\r
+ int ret;\r
+\r
+ adc = adc_alloc_host(sizeof(struct rk30_adc_device), &pdev->dev);\r
+ if (!adc)\r
+ return -ENOMEM;\r
+ spin_lock_init(&adc->lock);\r
+ adc->dev = &pdev->dev;\r
+ adc->is_suspended = 0;\r
+ adc->ops = &rk30_adc_ops;\r
+ dev = adc_priv(adc);\r
+ dev->adc = adc;\r
+ dev->irq = platform_get_irq(pdev, 0);\r
+ if (dev->irq <= 0) {\r
+ dev_err(&pdev->dev, "failed to get adc irq\n");\r
+ ret = -ENOENT;\r
+ goto err_alloc;\r
+ }\r
+\r
+ ret = request_irq(dev->irq, rk30_adc_irq, 0, pdev->name, dev);\r
+ if (ret < 0) {\r
+ dev_err(&pdev->dev, "failed to attach adc irq\n");\r
+ goto err_alloc;\r
+ }\r
+ dev->clk = clk_get(&pdev->dev, "saradc");\r
+ if (IS_ERR(dev->clk)) {\r
+ dev_err(&pdev->dev, "failed to get adc clock\n");\r
+ ret = PTR_ERR(dev->clk);\r
+ goto err_irq;\r
+ }\r
+\r
+ ret = clk_set_rate(dev->clk, ADC_CLK_RATE * 1000 * 1000);\r
+ if(ret < 0) {\r
+ dev_err(&pdev->dev, "failed to set adc clk\n");\r
+ goto err_clk;\r
+ }\r
+ clk_enable(dev->clk);\r
+\r
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);\r
+ if (!res) {\r
+ dev_err(&pdev->dev, "cannot find IO resource\n");\r
+ ret = -ENOENT;\r
+ goto err_clk;\r
+ }\r
+ dev->ioarea = request_mem_region(res->start, (res->end - res->start) + 1, \r
+ pdev->name);\r
+ if(dev->ioarea == NULL) {\r
+ dev_err(&pdev->dev, "cannot request IO\n");\r
+ ret = -ENXIO;\r
+ goto err_clk;\r
+ }\r
+ dev->regs = ioremap(res->start, (res->end - res->start) + 1);\r
+ if (!dev->regs) {\r
+ dev_err(&pdev->dev, "cannot map IO\n");\r
+ ret = -ENXIO;\r
+ goto err_ioarea;\r
+ }\r
+ platform_set_drvdata(pdev, dev);\r
+ dev_info(&pdev->dev, "rk30 adc: driver initialized\n");\r
+ return 0;\r
+// err_iomap:\r
+// iounmap(dev->regs);\r
+\r
+ err_ioarea:\r
+ release_resource(dev->ioarea);\r
+ kfree(dev->ioarea);\r
+ clk_disable(dev->clk);\r
+\r
+ err_clk:\r
+ clk_put(dev->clk);\r
+\r
+ err_irq:\r
+ free_irq(dev->irq, dev);\r
+\r
+ err_alloc:\r
+ adc_free_host(dev->adc);\r
+ return ret;\r
+}\r
+\r
+static int rk30_adc_remove(struct platform_device *pdev)\r
+{\r
+ struct rk30_adc_device *dev = platform_get_drvdata(pdev);\r
+\r
+ iounmap(dev->regs);\r
+ release_resource(dev->ioarea);\r
+ kfree(dev->ioarea);\r
+ free_irq(dev->irq, dev);\r
+ clk_disable(dev->clk);\r
+ clk_put(dev->clk);\r
+ adc_free_host(dev->adc);\r
+\r
+ return 0;\r
+}\r
+\r
+#ifdef CONFIG_PM\r
+static int rk30_adc_suspend(struct platform_device *pdev, pm_message_t state)\r
+{\r
+ struct rk30_adc_device *dev = platform_get_drvdata(pdev);\r
+\r
+ dev->adc->is_suspended = 1;\r
+ return 0;\r
+}\r
+\r
+static int rk30_adc_resume(struct platform_device *pdev)\r
+{\r
+ struct rk30_adc_device *dev = platform_get_drvdata(pdev);\r
+\r
+ dev->adc->is_suspended = 0;\r
+ return 0;\r
+}\r
+\r
+#else\r
+#define rk30_adc_suspend NULL\r
+#define rk30_adc_resume NULL\r
+#endif\r
+\r
+static struct platform_driver rk30_adc_driver = {\r
+ .driver = {\r
+ .name = "rk30-adc",\r
+ .owner = THIS_MODULE,\r
+ },\r
+ .probe = rk30_adc_probe,\r
+ .remove = __devexit_p(rk30_adc_remove),\r
+ .suspend = rk30_adc_suspend,\r
+ .resume = rk30_adc_resume,\r
+};\r
+\r
+static int __init rk30_adc_init(void)\r
+{\r
+ return platform_driver_register(&rk30_adc_driver);\r
+}\r
+subsys_initcall(rk30_adc_init);\r
+\r
+static void __exit rk30_adc_exit(void)\r
+{\r
+ platform_driver_unregister(&rk30_adc_driver);\r
+}\r
+module_exit(rk30_adc_exit);\r
+\r
+MODULE_DESCRIPTION("Driver for ADC");\r
+MODULE_AUTHOR("kfx, kfx@rock-chips.com");\r
+MODULE_LICENSE("GPL");\r
+static int __init adc_test_init(void)\r
+{\r
+#ifdef ADC_TEST \r
+ rk30_adc_test();\r
+#endif\r
+ return 0;\r
+\r
+}\r
+module_init(adc_test_init);\r