Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 1 | /* NXP PCF50633 ADC Driver |
| 2 | * |
| 3 | * (C) 2006-2008 by Openmoko, Inc. |
| 4 | * Author: Balaji Rao <balajirrao@openmoko.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Broken down from monstrous PCF50633 driver mainly by |
| 8 | * Harald Welte, Andy Green and Werner Almesberger |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | * |
| 15 | * NOTE: This driver does not yet support subtractive ADC mode, which means |
| 16 | * you can do only one measurement per read request. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 21 | #include <linux/module.h> |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 22 | #include <linux/device.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/completion.h> |
| 25 | |
| 26 | #include <linux/mfd/pcf50633/core.h> |
| 27 | #include <linux/mfd/pcf50633/adc.h> |
| 28 | |
| 29 | struct pcf50633_adc_request { |
| 30 | int mux; |
| 31 | int avg; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 32 | void (*callback)(struct pcf50633 *, void *, int); |
| 33 | void *callback_param; |
Lars-Peter Clausen | 6438a69 | 2010-05-12 02:10:54 +0200 | [diff] [blame] | 34 | }; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 35 | |
Lars-Peter Clausen | 6438a69 | 2010-05-12 02:10:54 +0200 | [diff] [blame] | 36 | struct pcf50633_adc_sync_request { |
| 37 | int result; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 38 | struct completion completion; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define PCF50633_MAX_ADC_FIFO_DEPTH 8 |
| 42 | |
| 43 | struct pcf50633_adc { |
| 44 | struct pcf50633 *pcf; |
| 45 | |
| 46 | /* Private stuff */ |
| 47 | struct pcf50633_adc_request *queue[PCF50633_MAX_ADC_FIFO_DEPTH]; |
| 48 | int queue_head; |
| 49 | int queue_tail; |
| 50 | struct mutex queue_mutex; |
| 51 | }; |
| 52 | |
| 53 | static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf) |
| 54 | { |
| 55 | return platform_get_drvdata(pcf->adc_pdev); |
| 56 | } |
| 57 | |
| 58 | static void adc_setup(struct pcf50633 *pcf, int channel, int avg) |
| 59 | { |
| 60 | channel &= PCF50633_ADCC1_ADCMUX_MASK; |
| 61 | |
| 62 | /* kill ratiometric, but enable ACCSW biasing */ |
| 63 | pcf50633_reg_write(pcf, PCF50633_REG_ADCC2, 0x00); |
| 64 | pcf50633_reg_write(pcf, PCF50633_REG_ADCC3, 0x01); |
| 65 | |
| 66 | /* start ADC conversion on selected channel */ |
| 67 | pcf50633_reg_write(pcf, PCF50633_REG_ADCC1, channel | avg | |
| 68 | PCF50633_ADCC1_ADCSTART | PCF50633_ADCC1_RES_10BIT); |
| 69 | } |
| 70 | |
| 71 | static void trigger_next_adc_job_if_any(struct pcf50633 *pcf) |
| 72 | { |
| 73 | struct pcf50633_adc *adc = __to_adc(pcf); |
| 74 | int head; |
| 75 | |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 76 | head = adc->queue_head; |
| 77 | |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 78 | if (!adc->queue[head]) |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 79 | return; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 80 | |
| 81 | adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg); |
| 82 | } |
| 83 | |
| 84 | static int |
| 85 | adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req) |
| 86 | { |
| 87 | struct pcf50633_adc *adc = __to_adc(pcf); |
| 88 | int head, tail; |
| 89 | |
| 90 | mutex_lock(&adc->queue_mutex); |
| 91 | |
| 92 | head = adc->queue_head; |
| 93 | tail = adc->queue_tail; |
| 94 | |
| 95 | if (adc->queue[tail]) { |
| 96 | mutex_unlock(&adc->queue_mutex); |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 97 | dev_err(pcf->dev, "ADC queue is full, dropping request\n"); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 98 | return -EBUSY; |
| 99 | } |
| 100 | |
| 101 | adc->queue[tail] = req; |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 102 | if (head == tail) |
| 103 | trigger_next_adc_job_if_any(pcf); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 104 | adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1); |
| 105 | |
| 106 | mutex_unlock(&adc->queue_mutex); |
| 107 | |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
Lars-Peter Clausen | 6438a69 | 2010-05-12 02:10:54 +0200 | [diff] [blame] | 111 | static void pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param, |
| 112 | int result) |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 113 | { |
Lars-Peter Clausen | 6438a69 | 2010-05-12 02:10:54 +0200 | [diff] [blame] | 114 | struct pcf50633_adc_sync_request *req = param; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 115 | |
| 116 | req->result = result; |
| 117 | complete(&req->completion); |
| 118 | } |
| 119 | |
| 120 | int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg) |
| 121 | { |
Lars-Peter Clausen | 6438a69 | 2010-05-12 02:10:54 +0200 | [diff] [blame] | 122 | struct pcf50633_adc_sync_request req; |
| 123 | int ret; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 124 | |
Lars-Peter Clausen | 6438a69 | 2010-05-12 02:10:54 +0200 | [diff] [blame] | 125 | init_completion(&req.completion); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 126 | |
Lars-Peter Clausen | 6438a69 | 2010-05-12 02:10:54 +0200 | [diff] [blame] | 127 | ret = pcf50633_adc_async_read(pcf, mux, avg, |
| 128 | pcf50633_adc_sync_read_callback, &req); |
| 129 | if (ret) |
| 130 | return ret; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 131 | |
Lars-Peter Clausen | 6438a69 | 2010-05-12 02:10:54 +0200 | [diff] [blame] | 132 | wait_for_completion(&req.completion); |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 133 | |
Lars-Peter Clausen | 6438a69 | 2010-05-12 02:10:54 +0200 | [diff] [blame] | 134 | return req.result; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 135 | } |
| 136 | EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read); |
| 137 | |
| 138 | int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg, |
| 139 | void (*callback)(struct pcf50633 *, void *, int), |
| 140 | void *callback_param) |
| 141 | { |
| 142 | struct pcf50633_adc_request *req; |
| 143 | |
| 144 | /* req is freed when the result is ready, in interrupt handler */ |
| 145 | req = kmalloc(sizeof(*req), GFP_KERNEL); |
| 146 | if (!req) |
| 147 | return -ENOMEM; |
| 148 | |
| 149 | req->mux = mux; |
| 150 | req->avg = avg; |
| 151 | req->callback = callback; |
| 152 | req->callback_param = callback_param; |
| 153 | |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 154 | return adc_enqueue_request(pcf, req); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 155 | } |
| 156 | EXPORT_SYMBOL_GPL(pcf50633_adc_async_read); |
| 157 | |
| 158 | static int adc_result(struct pcf50633 *pcf) |
| 159 | { |
| 160 | u8 adcs1, adcs3; |
| 161 | u16 result; |
| 162 | |
| 163 | adcs1 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS1); |
| 164 | adcs3 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS3); |
| 165 | result = (adcs1 << 2) | (adcs3 & PCF50633_ADCS3_ADCDAT1L_MASK); |
| 166 | |
| 167 | dev_dbg(pcf->dev, "adc result = %d\n", result); |
| 168 | |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | static void pcf50633_adc_irq(int irq, void *data) |
| 173 | { |
| 174 | struct pcf50633_adc *adc = data; |
| 175 | struct pcf50633 *pcf = adc->pcf; |
| 176 | struct pcf50633_adc_request *req; |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 177 | int head, res; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 178 | |
| 179 | mutex_lock(&adc->queue_mutex); |
| 180 | head = adc->queue_head; |
| 181 | |
| 182 | req = adc->queue[head]; |
| 183 | if (WARN_ON(!req)) { |
| 184 | dev_err(pcf->dev, "pcf50633-adc irq: ADC queue empty!\n"); |
| 185 | mutex_unlock(&adc->queue_mutex); |
| 186 | return; |
| 187 | } |
| 188 | adc->queue[head] = NULL; |
| 189 | adc->queue_head = (head + 1) & |
| 190 | (PCF50633_MAX_ADC_FIFO_DEPTH - 1); |
| 191 | |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 192 | res = adc_result(pcf); |
| 193 | trigger_next_adc_job_if_any(pcf); |
| 194 | |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 195 | mutex_unlock(&adc->queue_mutex); |
| 196 | |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 197 | req->callback(pcf, req->callback_param, res); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 198 | kfree(req); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 201 | static int pcf50633_adc_probe(struct platform_device *pdev) |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 202 | { |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 203 | struct pcf50633_adc *adc; |
| 204 | |
Jingoo Han | 8a105ca | 2013-08-20 16:05:29 +0900 | [diff] [blame] | 205 | adc = devm_kzalloc(&pdev->dev, sizeof(*adc), GFP_KERNEL); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 206 | if (!adc) |
| 207 | return -ENOMEM; |
| 208 | |
Lars-Peter Clausen | 68d641e | 2009-10-14 02:12:33 +0400 | [diff] [blame] | 209 | adc->pcf = dev_to_pcf50633(pdev->dev.parent); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 210 | platform_set_drvdata(pdev, adc); |
| 211 | |
Lars-Peter Clausen | 68d641e | 2009-10-14 02:12:33 +0400 | [diff] [blame] | 212 | pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY, |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 213 | pcf50633_adc_irq, adc); |
| 214 | |
| 215 | mutex_init(&adc->queue_mutex); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 220 | static int pcf50633_adc_remove(struct platform_device *pdev) |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 221 | { |
| 222 | struct pcf50633_adc *adc = platform_get_drvdata(pdev); |
| 223 | int i, head; |
| 224 | |
| 225 | pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY); |
| 226 | |
| 227 | mutex_lock(&adc->queue_mutex); |
| 228 | head = adc->queue_head; |
| 229 | |
| 230 | if (WARN_ON(adc->queue[head])) |
| 231 | dev_err(adc->pcf->dev, |
| 232 | "adc driver removed with request pending\n"); |
| 233 | |
| 234 | for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++) |
| 235 | kfree(adc->queue[i]); |
| 236 | |
| 237 | mutex_unlock(&adc->queue_mutex); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static struct platform_driver pcf50633_adc_driver = { |
| 243 | .driver = { |
| 244 | .name = "pcf50633-adc", |
| 245 | }, |
| 246 | .probe = pcf50633_adc_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 247 | .remove = pcf50633_adc_remove, |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 248 | }; |
| 249 | |
Mark Brown | 65349d6 | 2011-11-23 22:58:34 +0000 | [diff] [blame] | 250 | module_platform_driver(pcf50633_adc_driver); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 251 | |
| 252 | MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>"); |
| 253 | MODULE_DESCRIPTION("PCF50633 adc driver"); |
| 254 | MODULE_LICENSE("GPL"); |
| 255 | MODULE_ALIAS("platform:pcf50633-adc"); |
| 256 | |