0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/kernel.h>
0016 #include <linux/slab.h>
0017 #include <linux/module.h>
0018 #include <linux/device.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/completion.h>
0021
0022 #include <linux/mfd/pcf50633/core.h>
0023 #include <linux/mfd/pcf50633/adc.h>
0024
0025 struct pcf50633_adc_request {
0026 int mux;
0027 int avg;
0028 void (*callback)(struct pcf50633 *, void *, int);
0029 void *callback_param;
0030 };
0031
0032 struct pcf50633_adc_sync_request {
0033 int result;
0034 struct completion completion;
0035 };
0036
0037 #define PCF50633_MAX_ADC_FIFO_DEPTH 8
0038
0039 struct pcf50633_adc {
0040 struct pcf50633 *pcf;
0041
0042
0043 struct pcf50633_adc_request *queue[PCF50633_MAX_ADC_FIFO_DEPTH];
0044 int queue_head;
0045 int queue_tail;
0046 struct mutex queue_mutex;
0047 };
0048
0049 static inline struct pcf50633_adc *__to_adc(struct pcf50633 *pcf)
0050 {
0051 return platform_get_drvdata(pcf->adc_pdev);
0052 }
0053
0054 static void adc_setup(struct pcf50633 *pcf, int channel, int avg)
0055 {
0056 channel &= PCF50633_ADCC1_ADCMUX_MASK;
0057
0058
0059 pcf50633_reg_write(pcf, PCF50633_REG_ADCC2, 0x00);
0060 pcf50633_reg_write(pcf, PCF50633_REG_ADCC3, 0x01);
0061
0062
0063 pcf50633_reg_write(pcf, PCF50633_REG_ADCC1, channel | avg |
0064 PCF50633_ADCC1_ADCSTART | PCF50633_ADCC1_RES_10BIT);
0065 }
0066
0067 static void trigger_next_adc_job_if_any(struct pcf50633 *pcf)
0068 {
0069 struct pcf50633_adc *adc = __to_adc(pcf);
0070 int head;
0071
0072 head = adc->queue_head;
0073
0074 if (!adc->queue[head])
0075 return;
0076
0077 adc_setup(pcf, adc->queue[head]->mux, adc->queue[head]->avg);
0078 }
0079
0080 static int
0081 adc_enqueue_request(struct pcf50633 *pcf, struct pcf50633_adc_request *req)
0082 {
0083 struct pcf50633_adc *adc = __to_adc(pcf);
0084 int head, tail;
0085
0086 mutex_lock(&adc->queue_mutex);
0087
0088 head = adc->queue_head;
0089 tail = adc->queue_tail;
0090
0091 if (adc->queue[tail]) {
0092 mutex_unlock(&adc->queue_mutex);
0093 dev_err(pcf->dev, "ADC queue is full, dropping request\n");
0094 return -EBUSY;
0095 }
0096
0097 adc->queue[tail] = req;
0098 if (head == tail)
0099 trigger_next_adc_job_if_any(pcf);
0100 adc->queue_tail = (tail + 1) & (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
0101
0102 mutex_unlock(&adc->queue_mutex);
0103
0104 return 0;
0105 }
0106
0107 static void pcf50633_adc_sync_read_callback(struct pcf50633 *pcf, void *param,
0108 int result)
0109 {
0110 struct pcf50633_adc_sync_request *req = param;
0111
0112 req->result = result;
0113 complete(&req->completion);
0114 }
0115
0116 int pcf50633_adc_sync_read(struct pcf50633 *pcf, int mux, int avg)
0117 {
0118 struct pcf50633_adc_sync_request req;
0119 int ret;
0120
0121 init_completion(&req.completion);
0122
0123 ret = pcf50633_adc_async_read(pcf, mux, avg,
0124 pcf50633_adc_sync_read_callback, &req);
0125 if (ret)
0126 return ret;
0127
0128 wait_for_completion(&req.completion);
0129
0130 return req.result;
0131 }
0132 EXPORT_SYMBOL_GPL(pcf50633_adc_sync_read);
0133
0134 int pcf50633_adc_async_read(struct pcf50633 *pcf, int mux, int avg,
0135 void (*callback)(struct pcf50633 *, void *, int),
0136 void *callback_param)
0137 {
0138 struct pcf50633_adc_request *req;
0139
0140
0141 req = kmalloc(sizeof(*req), GFP_KERNEL);
0142 if (!req)
0143 return -ENOMEM;
0144
0145 req->mux = mux;
0146 req->avg = avg;
0147 req->callback = callback;
0148 req->callback_param = callback_param;
0149
0150 return adc_enqueue_request(pcf, req);
0151 }
0152 EXPORT_SYMBOL_GPL(pcf50633_adc_async_read);
0153
0154 static int adc_result(struct pcf50633 *pcf)
0155 {
0156 u8 adcs1, adcs3;
0157 u16 result;
0158
0159 adcs1 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS1);
0160 adcs3 = pcf50633_reg_read(pcf, PCF50633_REG_ADCS3);
0161 result = (adcs1 << 2) | (adcs3 & PCF50633_ADCS3_ADCDAT1L_MASK);
0162
0163 dev_dbg(pcf->dev, "adc result = %d\n", result);
0164
0165 return result;
0166 }
0167
0168 static void pcf50633_adc_irq(int irq, void *data)
0169 {
0170 struct pcf50633_adc *adc = data;
0171 struct pcf50633 *pcf = adc->pcf;
0172 struct pcf50633_adc_request *req;
0173 int head, res;
0174
0175 mutex_lock(&adc->queue_mutex);
0176 head = adc->queue_head;
0177
0178 req = adc->queue[head];
0179 if (WARN_ON(!req)) {
0180 dev_err(pcf->dev, "pcf50633-adc irq: ADC queue empty!\n");
0181 mutex_unlock(&adc->queue_mutex);
0182 return;
0183 }
0184 adc->queue[head] = NULL;
0185 adc->queue_head = (head + 1) &
0186 (PCF50633_MAX_ADC_FIFO_DEPTH - 1);
0187
0188 res = adc_result(pcf);
0189 trigger_next_adc_job_if_any(pcf);
0190
0191 mutex_unlock(&adc->queue_mutex);
0192
0193 req->callback(pcf, req->callback_param, res);
0194 kfree(req);
0195 }
0196
0197 static int pcf50633_adc_probe(struct platform_device *pdev)
0198 {
0199 struct pcf50633_adc *adc;
0200
0201 adc = devm_kzalloc(&pdev->dev, sizeof(*adc), GFP_KERNEL);
0202 if (!adc)
0203 return -ENOMEM;
0204
0205 adc->pcf = dev_to_pcf50633(pdev->dev.parent);
0206 platform_set_drvdata(pdev, adc);
0207
0208 pcf50633_register_irq(adc->pcf, PCF50633_IRQ_ADCRDY,
0209 pcf50633_adc_irq, adc);
0210
0211 mutex_init(&adc->queue_mutex);
0212
0213 return 0;
0214 }
0215
0216 static int pcf50633_adc_remove(struct platform_device *pdev)
0217 {
0218 struct pcf50633_adc *adc = platform_get_drvdata(pdev);
0219 int i, head;
0220
0221 pcf50633_free_irq(adc->pcf, PCF50633_IRQ_ADCRDY);
0222
0223 mutex_lock(&adc->queue_mutex);
0224 head = adc->queue_head;
0225
0226 if (WARN_ON(adc->queue[head]))
0227 dev_err(adc->pcf->dev,
0228 "adc driver removed with request pending\n");
0229
0230 for (i = 0; i < PCF50633_MAX_ADC_FIFO_DEPTH; i++)
0231 kfree(adc->queue[i]);
0232
0233 mutex_unlock(&adc->queue_mutex);
0234
0235 return 0;
0236 }
0237
0238 static struct platform_driver pcf50633_adc_driver = {
0239 .driver = {
0240 .name = "pcf50633-adc",
0241 },
0242 .probe = pcf50633_adc_probe,
0243 .remove = pcf50633_adc_remove,
0244 };
0245
0246 module_platform_driver(pcf50633_adc_driver);
0247
0248 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
0249 MODULE_DESCRIPTION("PCF50633 adc driver");
0250 MODULE_LICENSE("GPL");
0251 MODULE_ALIAS("platform:pcf50633-adc");
0252