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> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/completion.h> |
| 26 | |
| 27 | #include <linux/mfd/pcf50633/core.h> |
| 28 | #include <linux/mfd/pcf50633/adc.h> |
| 29 | |
| 30 | struct pcf50633_adc_request { |
| 31 | int mux; |
| 32 | int avg; |
| 33 | int result; |
| 34 | void (*callback)(struct pcf50633 *, void *, int); |
| 35 | void *callback_param; |
| 36 | |
| 37 | /* Used in case of sync requests */ |
| 38 | struct completion completion; |
| 39 | |
| 40 | }; |
| 41 | |
| 42 | #define PCF50633_MAX_ADC_FIFO_DEPTH 8 |
| 43 | |
| 44 | struct pcf50633_adc { |
| 45 | struct pcf50633 *pcf; |
| 46 | |
| 47 | /* Private stuff */ |
| 48 | struct pcf50633_adc_request *queue[PCF50633_MAX_ADC_FIFO_DEPTH]; |
| 49 | int queue_head; |
| 50 | int queue_tail; |
| 51 | struct mutex queue_mutex; |
| 52 | }; |
| 53 | |
| 54 | static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf) |
| 55 | { |
| 56 | return platform_get_drvdata(pcf->adc_pdev); |
| 57 | } |
| 58 | |
| 59 | static void adc_setup(struct pcf50633 *pcf, int channel, int avg) |
| 60 | { |
| 61 | channel &= PCF50633_ADCC1_ADCMUX_MASK; |
| 62 | |
| 63 | /* kill ratiometric, but enable ACCSW biasing */ |
| 64 | pcf50633_reg_write(pcf, PCF50633_REG_ADCC2, 0x00); |
| 65 | pcf50633_reg_write(pcf, PCF50633_REG_ADCC3, 0x01); |
| 66 | |
| 67 | /* start ADC conversion on selected channel */ |
| 68 | pcf50633_reg_write(pcf, PCF50633_REG_ADCC1, channel | avg | |
| 69 | PCF50633_ADCC1_ADCSTART | PCF50633_ADCC1_RES_10BIT); |
| 70 | } |
| 71 | |
| 72 | static void trigger_next_adc_job_if_any(struct pcf50633 *pcf) |
| 73 | { |
| 74 | struct pcf50633_adc *adc = __to_adc(pcf); |
| 75 | int head; |
| 76 | |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 77 | head = adc->queue_head; |
| 78 | |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 79 | if (!adc->queue[head]) |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 80 | return; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 81 | |
| 82 | adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg); |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req) |
| 87 | { |
| 88 | struct pcf50633_adc *adc = __to_adc(pcf); |
| 89 | int head, tail; |
| 90 | |
| 91 | mutex_lock(&adc->queue_mutex); |
| 92 | |
| 93 | head = adc->queue_head; |
| 94 | tail = adc->queue_tail; |
| 95 | |
| 96 | if (adc->queue[tail]) { |
| 97 | mutex_unlock(&adc->queue_mutex); |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 98 | dev_err(pcf->dev, "ADC queue is full, dropping request\n"); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 99 | return -EBUSY; |
| 100 | } |
| 101 | |
| 102 | adc->queue[tail] = req; |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 103 | if (head == tail) |
| 104 | trigger_next_adc_job_if_any(pcf); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 105 | adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1); |
| 106 | |
| 107 | mutex_unlock(&adc->queue_mutex); |
| 108 | |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param, int result) |
| 114 | { |
| 115 | struct pcf50633_adc_request *req = param; |
| 116 | |
| 117 | req->result = result; |
| 118 | complete(&req->completion); |
| 119 | } |
| 120 | |
| 121 | int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg) |
| 122 | { |
| 123 | struct pcf50633_adc_request *req; |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 124 | int err; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 125 | |
| 126 | /* req is freed when the result is ready, in interrupt handler */ |
| 127 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 128 | if (!req) |
| 129 | return -ENOMEM; |
| 130 | |
| 131 | req->mux = mux; |
| 132 | req->avg = avg; |
| 133 | req->callback = pcf50633_adc_sync_read_callback; |
| 134 | req->callback_param = req; |
| 135 | |
| 136 | init_completion(&req->completion); |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 137 | err = adc_enqueue_request(pcf, req); |
| 138 | if (err) |
| 139 | return err; |
| 140 | |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 141 | wait_for_completion(&req->completion); |
| 142 | |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 143 | /* FIXME by this time req might be already freed */ |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 144 | return req->result; |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read); |
| 147 | |
| 148 | int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg, |
| 149 | void (*callback)(struct pcf50633 *, void *, int), |
| 150 | void *callback_param) |
| 151 | { |
| 152 | struct pcf50633_adc_request *req; |
| 153 | |
| 154 | /* req is freed when the result is ready, in interrupt handler */ |
| 155 | req = kmalloc(sizeof(*req), GFP_KERNEL); |
| 156 | if (!req) |
| 157 | return -ENOMEM; |
| 158 | |
| 159 | req->mux = mux; |
| 160 | req->avg = avg; |
| 161 | req->callback = callback; |
| 162 | req->callback_param = callback_param; |
| 163 | |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 164 | return adc_enqueue_request(pcf, req); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 165 | } |
| 166 | EXPORT_SYMBOL_GPL(pcf50633_adc_async_read); |
| 167 | |
| 168 | static int adc_result(struct pcf50633 *pcf) |
| 169 | { |
| 170 | u8 adcs1, adcs3; |
| 171 | u16 result; |
| 172 | |
| 173 | adcs1 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS1); |
| 174 | adcs3 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS3); |
| 175 | result = (adcs1 << 2) | (adcs3 & PCF50633_ADCS3_ADCDAT1L_MASK); |
| 176 | |
| 177 | dev_dbg(pcf->dev, "adc result = %d\n", result); |
| 178 | |
| 179 | return result; |
| 180 | } |
| 181 | |
| 182 | static void pcf50633_adc_irq(int irq, void *data) |
| 183 | { |
| 184 | struct pcf50633_adc *adc = data; |
| 185 | struct pcf50633 *pcf = adc->pcf; |
| 186 | struct pcf50633_adc_request *req; |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 187 | int head, res; |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 188 | |
| 189 | mutex_lock(&adc->queue_mutex); |
| 190 | head = adc->queue_head; |
| 191 | |
| 192 | req = adc->queue[head]; |
| 193 | if (WARN_ON(!req)) { |
| 194 | dev_err(pcf->dev, "pcf50633-adc irq: ADC queue empty!\n"); |
| 195 | mutex_unlock(&adc->queue_mutex); |
| 196 | return; |
| 197 | } |
| 198 | adc->queue[head] = NULL; |
| 199 | adc->queue_head = (head + 1) & |
| 200 | (PCF50633_MAX_ADC_FIFO_DEPTH - 1); |
| 201 | |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 202 | res = adc_result(pcf); |
| 203 | trigger_next_adc_job_if_any(pcf); |
| 204 | |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 205 | mutex_unlock(&adc->queue_mutex); |
| 206 | |
Paul Fertser | bd8ef10 | 2009-07-28 00:58:48 +0400 | [diff] [blame] | 207 | req->callback(pcf, req->callback_param, res); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 208 | kfree(req); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static int __devinit pcf50633_adc_probe(struct platform_device *pdev) |
| 212 | { |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 213 | struct pcf50633_adc *adc; |
| 214 | |
| 215 | adc = kzalloc(sizeof(*adc), GFP_KERNEL); |
| 216 | if (!adc) |
| 217 | return -ENOMEM; |
| 218 | |
Lars-Peter Clausen | 68d641e | 2009-10-14 02:12:33 +0400 | [diff] [blame] | 219 | adc->pcf = dev_to_pcf50633(pdev->dev.parent); |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 220 | platform_set_drvdata(pdev, adc); |
| 221 | |
Lars-Peter Clausen | 68d641e | 2009-10-14 02:12:33 +0400 | [diff] [blame] | 222 | pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY, |
Balaji Rao | 08c3e06 | 2009-01-09 01:49:26 +0100 | [diff] [blame] | 223 | pcf50633_adc_irq, adc); |
| 224 | |
| 225 | mutex_init(&adc->queue_mutex); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int __devexit pcf50633_adc_remove(struct platform_device *pdev) |
| 231 | { |
| 232 | struct pcf50633_adc *adc = platform_get_drvdata(pdev); |
| 233 | int i, head; |
| 234 | |
| 235 | pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY); |
| 236 | |
| 237 | mutex_lock(&adc->queue_mutex); |
| 238 | head = adc->queue_head; |
| 239 | |
| 240 | if (WARN_ON(adc->queue[head])) |
| 241 | dev_err(adc->pcf->dev, |
| 242 | "adc driver removed with request pending\n"); |
| 243 | |
| 244 | for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++) |
| 245 | kfree(adc->queue[i]); |
| 246 | |
| 247 | mutex_unlock(&adc->queue_mutex); |
| 248 | kfree(adc); |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static struct platform_driver pcf50633_adc_driver = { |
| 254 | .driver = { |
| 255 | .name = "pcf50633-adc", |
| 256 | }, |
| 257 | .probe = pcf50633_adc_probe, |
| 258 | .remove = __devexit_p(pcf50633_adc_remove), |
| 259 | }; |
| 260 | |
| 261 | static int __init pcf50633_adc_init(void) |
| 262 | { |
| 263 | return platform_driver_register(&pcf50633_adc_driver); |
| 264 | } |
| 265 | module_init(pcf50633_adc_init); |
| 266 | |
| 267 | static void __exit pcf50633_adc_exit(void) |
| 268 | { |
| 269 | platform_driver_unregister(&pcf50633_adc_driver); |
| 270 | } |
| 271 | module_exit(pcf50633_adc_exit); |
| 272 | |
| 273 | MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>"); |
| 274 | MODULE_DESCRIPTION("PCF50633 adc driver"); |
| 275 | MODULE_LICENSE("GPL"); |
| 276 | MODULE_ALIAS("platform:pcf50633-adc"); |
| 277 | |