Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Motorola PCAP2 as present in EZX phones |
| 3 | * |
| 4 | * Copyright (C) 2006 Harald Welte <laforge@openezx.org> |
| 5 | * Copyright (C) 2009 Daniel Ribeiro <drwyrm@gmail.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/mfd/ezx-pcap.h> |
| 19 | #include <linux/spi/spi.h> |
Daniel Ribeiro | b1148fd | 2009-06-23 12:34:13 -0300 | [diff] [blame] | 20 | #include <linux/gpio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 22 | |
| 23 | #define PCAP_ADC_MAXQ 8 |
| 24 | struct pcap_adc_request { |
| 25 | u8 bank; |
| 26 | u8 ch[2]; |
| 27 | u32 flags; |
| 28 | void (*callback)(void *, u16[]); |
| 29 | void *data; |
| 30 | }; |
| 31 | |
| 32 | struct pcap_adc_sync_request { |
| 33 | u16 res[2]; |
| 34 | struct completion completion; |
| 35 | }; |
| 36 | |
| 37 | struct pcap_chip { |
| 38 | struct spi_device *spi; |
| 39 | |
| 40 | /* IO */ |
| 41 | u32 buf; |
| 42 | struct mutex io_mutex; |
| 43 | |
| 44 | /* IRQ */ |
| 45 | unsigned int irq_base; |
| 46 | u32 msr; |
| 47 | struct work_struct isr_work; |
| 48 | struct work_struct msr_work; |
| 49 | struct workqueue_struct *workqueue; |
| 50 | |
| 51 | /* ADC */ |
| 52 | struct pcap_adc_request *adc_queue[PCAP_ADC_MAXQ]; |
| 53 | u8 adc_head; |
| 54 | u8 adc_tail; |
| 55 | struct mutex adc_mutex; |
| 56 | }; |
| 57 | |
| 58 | /* IO */ |
| 59 | static int ezx_pcap_putget(struct pcap_chip *pcap, u32 *data) |
| 60 | { |
| 61 | struct spi_transfer t; |
| 62 | struct spi_message m; |
| 63 | int status; |
| 64 | |
Lee Jones | 0309528 | 2014-07-25 15:31:02 +0100 | [diff] [blame] | 65 | memset(&t, 0, sizeof(t)); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 66 | spi_message_init(&m); |
| 67 | t.len = sizeof(u32); |
| 68 | spi_message_add_tail(&t, &m); |
| 69 | |
| 70 | pcap->buf = *data; |
| 71 | t.tx_buf = (u8 *) &pcap->buf; |
| 72 | t.rx_buf = (u8 *) &pcap->buf; |
| 73 | status = spi_sync(pcap->spi, &m); |
| 74 | |
| 75 | if (status == 0) |
| 76 | *data = pcap->buf; |
| 77 | |
| 78 | return status; |
| 79 | } |
| 80 | |
| 81 | int ezx_pcap_write(struct pcap_chip *pcap, u8 reg_num, u32 value) |
| 82 | { |
| 83 | int ret; |
| 84 | |
| 85 | mutex_lock(&pcap->io_mutex); |
| 86 | value &= PCAP_REGISTER_VALUE_MASK; |
| 87 | value |= PCAP_REGISTER_WRITE_OP_BIT |
| 88 | | (reg_num << PCAP_REGISTER_ADDRESS_SHIFT); |
| 89 | ret = ezx_pcap_putget(pcap, &value); |
| 90 | mutex_unlock(&pcap->io_mutex); |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | EXPORT_SYMBOL_GPL(ezx_pcap_write); |
| 95 | |
| 96 | int ezx_pcap_read(struct pcap_chip *pcap, u8 reg_num, u32 *value) |
| 97 | { |
| 98 | int ret; |
| 99 | |
| 100 | mutex_lock(&pcap->io_mutex); |
| 101 | *value = PCAP_REGISTER_READ_OP_BIT |
| 102 | | (reg_num << PCAP_REGISTER_ADDRESS_SHIFT); |
| 103 | |
| 104 | ret = ezx_pcap_putget(pcap, value); |
| 105 | mutex_unlock(&pcap->io_mutex); |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | EXPORT_SYMBOL_GPL(ezx_pcap_read); |
| 110 | |
Daniel Ribeiro | e9a2263 | 2009-06-27 00:17:20 -0300 | [diff] [blame] | 111 | int ezx_pcap_set_bits(struct pcap_chip *pcap, u8 reg_num, u32 mask, u32 val) |
| 112 | { |
| 113 | int ret; |
| 114 | u32 tmp = PCAP_REGISTER_READ_OP_BIT | |
| 115 | (reg_num << PCAP_REGISTER_ADDRESS_SHIFT); |
| 116 | |
| 117 | mutex_lock(&pcap->io_mutex); |
| 118 | ret = ezx_pcap_putget(pcap, &tmp); |
| 119 | if (ret) |
| 120 | goto out_unlock; |
| 121 | |
| 122 | tmp &= (PCAP_REGISTER_VALUE_MASK & ~mask); |
| 123 | tmp |= (val & mask) | PCAP_REGISTER_WRITE_OP_BIT | |
| 124 | (reg_num << PCAP_REGISTER_ADDRESS_SHIFT); |
| 125 | |
| 126 | ret = ezx_pcap_putget(pcap, &tmp); |
| 127 | out_unlock: |
| 128 | mutex_unlock(&pcap->io_mutex); |
| 129 | |
| 130 | return ret; |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(ezx_pcap_set_bits); |
| 133 | |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 134 | /* IRQ */ |
Daniel Ribeiro | 9f7b07d | 2009-06-23 12:32:11 -0300 | [diff] [blame] | 135 | int irq_to_pcap(struct pcap_chip *pcap, int irq) |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 136 | { |
Daniel Ribeiro | 9f7b07d | 2009-06-23 12:32:11 -0300 | [diff] [blame] | 137 | return irq - pcap->irq_base; |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 138 | } |
Daniel Ribeiro | 9f7b07d | 2009-06-23 12:32:11 -0300 | [diff] [blame] | 139 | EXPORT_SYMBOL_GPL(irq_to_pcap); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 140 | |
| 141 | int pcap_to_irq(struct pcap_chip *pcap, int irq) |
| 142 | { |
| 143 | return pcap->irq_base + irq; |
| 144 | } |
| 145 | EXPORT_SYMBOL_GPL(pcap_to_irq); |
| 146 | |
Lennert Buytenhek | c232f22 | 2010-12-13 13:30:09 +0100 | [diff] [blame] | 147 | static void pcap_mask_irq(struct irq_data *d) |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 148 | { |
Lennert Buytenhek | c232f22 | 2010-12-13 13:30:09 +0100 | [diff] [blame] | 149 | struct pcap_chip *pcap = irq_data_get_irq_chip_data(d); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 150 | |
Lennert Buytenhek | c232f22 | 2010-12-13 13:30:09 +0100 | [diff] [blame] | 151 | pcap->msr |= 1 << irq_to_pcap(pcap, d->irq); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 152 | queue_work(pcap->workqueue, &pcap->msr_work); |
| 153 | } |
| 154 | |
Lennert Buytenhek | c232f22 | 2010-12-13 13:30:09 +0100 | [diff] [blame] | 155 | static void pcap_unmask_irq(struct irq_data *d) |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 156 | { |
Lennert Buytenhek | c232f22 | 2010-12-13 13:30:09 +0100 | [diff] [blame] | 157 | struct pcap_chip *pcap = irq_data_get_irq_chip_data(d); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 158 | |
Lennert Buytenhek | c232f22 | 2010-12-13 13:30:09 +0100 | [diff] [blame] | 159 | pcap->msr &= ~(1 << irq_to_pcap(pcap, d->irq)); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 160 | queue_work(pcap->workqueue, &pcap->msr_work); |
| 161 | } |
| 162 | |
| 163 | static struct irq_chip pcap_irq_chip = { |
Lennert Buytenhek | c232f22 | 2010-12-13 13:30:09 +0100 | [diff] [blame] | 164 | .name = "pcap", |
Thomas Gleixner | 73a6839 | 2011-03-25 11:12:27 +0000 | [diff] [blame] | 165 | .irq_disable = pcap_mask_irq, |
Lennert Buytenhek | c232f22 | 2010-12-13 13:30:09 +0100 | [diff] [blame] | 166 | .irq_mask = pcap_mask_irq, |
| 167 | .irq_unmask = pcap_unmask_irq, |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | static void pcap_msr_work(struct work_struct *work) |
| 171 | { |
| 172 | struct pcap_chip *pcap = container_of(work, struct pcap_chip, msr_work); |
| 173 | |
| 174 | ezx_pcap_write(pcap, PCAP_REG_MSR, pcap->msr); |
| 175 | } |
| 176 | |
| 177 | static void pcap_isr_work(struct work_struct *work) |
| 178 | { |
| 179 | struct pcap_chip *pcap = container_of(work, struct pcap_chip, isr_work); |
Jingoo Han | 334a41c | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 180 | struct pcap_platform_data *pdata = dev_get_platdata(&pcap->spi->dev); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 181 | u32 msr, isr, int_sel, service; |
| 182 | int irq; |
| 183 | |
Daniel Ribeiro | b1148fd | 2009-06-23 12:34:13 -0300 | [diff] [blame] | 184 | do { |
| 185 | ezx_pcap_read(pcap, PCAP_REG_MSR, &msr); |
| 186 | ezx_pcap_read(pcap, PCAP_REG_ISR, &isr); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 187 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 188 | /* We can't service/ack irqs that are assigned to port 2 */ |
Daniel Ribeiro | b1148fd | 2009-06-23 12:34:13 -0300 | [diff] [blame] | 189 | if (!(pdata->config & PCAP_SECOND_PORT)) { |
| 190 | ezx_pcap_read(pcap, PCAP_REG_INT_SEL, &int_sel); |
| 191 | isr &= ~int_sel; |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 192 | } |
Daniel Ribeiro | b1148fd | 2009-06-23 12:34:13 -0300 | [diff] [blame] | 193 | |
| 194 | ezx_pcap_write(pcap, PCAP_REG_MSR, isr | msr); |
| 195 | ezx_pcap_write(pcap, PCAP_REG_ISR, isr); |
| 196 | |
| 197 | local_irq_disable(); |
| 198 | service = isr & ~msr; |
| 199 | for (irq = pcap->irq_base; service; service >>= 1, irq++) { |
Thomas Gleixner | 73a6839 | 2011-03-25 11:12:27 +0000 | [diff] [blame] | 200 | if (service & 1) |
| 201 | generic_handle_irq(irq); |
Daniel Ribeiro | b1148fd | 2009-06-23 12:34:13 -0300 | [diff] [blame] | 202 | } |
| 203 | local_irq_enable(); |
| 204 | ezx_pcap_write(pcap, PCAP_REG_MSR, pcap->msr); |
Arnd Bergmann | 59ee93a | 2012-08-05 14:58:37 +0000 | [diff] [blame] | 205 | } while (gpio_get_value(pdata->gpio)); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 206 | } |
| 207 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 208 | static void pcap_irq_handler(struct irq_desc *desc) |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 209 | { |
Jiang Liu | 1e84aa4 | 2015-07-13 20:44:56 +0000 | [diff] [blame] | 210 | struct pcap_chip *pcap = irq_desc_get_handler_data(desc); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 211 | |
Lennert Buytenhek | c232f22 | 2010-12-13 13:30:09 +0100 | [diff] [blame] | 212 | desc->irq_data.chip->irq_ack(&desc->irq_data); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 213 | queue_work(pcap->workqueue, &pcap->isr_work); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* ADC */ |
Daniel Ribeiro | ecd78cb | 2009-06-23 12:33:10 -0300 | [diff] [blame] | 217 | void pcap_set_ts_bits(struct pcap_chip *pcap, u32 bits) |
| 218 | { |
| 219 | u32 tmp; |
| 220 | |
| 221 | mutex_lock(&pcap->adc_mutex); |
| 222 | ezx_pcap_read(pcap, PCAP_REG_ADC, &tmp); |
| 223 | tmp &= ~(PCAP_ADC_TS_M_MASK | PCAP_ADC_TS_REF_LOWPWR); |
| 224 | tmp |= bits & (PCAP_ADC_TS_M_MASK | PCAP_ADC_TS_REF_LOWPWR); |
| 225 | ezx_pcap_write(pcap, PCAP_REG_ADC, tmp); |
| 226 | mutex_unlock(&pcap->adc_mutex); |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(pcap_set_ts_bits); |
| 229 | |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 230 | static void pcap_disable_adc(struct pcap_chip *pcap) |
| 231 | { |
| 232 | u32 tmp; |
| 233 | |
| 234 | ezx_pcap_read(pcap, PCAP_REG_ADC, &tmp); |
| 235 | tmp &= ~(PCAP_ADC_ADEN|PCAP_ADC_BATT_I_ADC|PCAP_ADC_BATT_I_POLARITY); |
| 236 | ezx_pcap_write(pcap, PCAP_REG_ADC, tmp); |
| 237 | } |
| 238 | |
| 239 | static void pcap_adc_trigger(struct pcap_chip *pcap) |
| 240 | { |
| 241 | u32 tmp; |
| 242 | u8 head; |
| 243 | |
| 244 | mutex_lock(&pcap->adc_mutex); |
| 245 | head = pcap->adc_head; |
| 246 | if (!pcap->adc_queue[head]) { |
| 247 | /* queue is empty, save power */ |
| 248 | pcap_disable_adc(pcap); |
| 249 | mutex_unlock(&pcap->adc_mutex); |
| 250 | return; |
| 251 | } |
Daniel Ribeiro | ecd78cb | 2009-06-23 12:33:10 -0300 | [diff] [blame] | 252 | /* start conversion on requested bank, save TS_M bits */ |
| 253 | ezx_pcap_read(pcap, PCAP_REG_ADC, &tmp); |
| 254 | tmp &= (PCAP_ADC_TS_M_MASK | PCAP_ADC_TS_REF_LOWPWR); |
| 255 | tmp |= pcap->adc_queue[head]->flags | PCAP_ADC_ADEN; |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 256 | |
| 257 | if (pcap->adc_queue[head]->bank == PCAP_ADC_BANK_1) |
| 258 | tmp |= PCAP_ADC_AD_SEL1; |
| 259 | |
| 260 | ezx_pcap_write(pcap, PCAP_REG_ADC, tmp); |
Daniel Ribeiro | ecd78cb | 2009-06-23 12:33:10 -0300 | [diff] [blame] | 261 | mutex_unlock(&pcap->adc_mutex); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 262 | ezx_pcap_write(pcap, PCAP_REG_ADR, PCAP_ADR_ASC); |
| 263 | } |
| 264 | |
| 265 | static irqreturn_t pcap_adc_irq(int irq, void *_pcap) |
| 266 | { |
| 267 | struct pcap_chip *pcap = _pcap; |
| 268 | struct pcap_adc_request *req; |
| 269 | u16 res[2]; |
| 270 | u32 tmp; |
| 271 | |
| 272 | mutex_lock(&pcap->adc_mutex); |
| 273 | req = pcap->adc_queue[pcap->adc_head]; |
| 274 | |
Joe Perches | e0084aa | 2010-10-30 14:08:32 -0700 | [diff] [blame] | 275 | if (WARN(!req, "adc irq without pending request\n")) { |
Daniel Ribeiro | 1c90ea2 | 2009-06-23 12:30:58 -0300 | [diff] [blame] | 276 | mutex_unlock(&pcap->adc_mutex); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 277 | return IRQ_HANDLED; |
Daniel Ribeiro | 1c90ea2 | 2009-06-23 12:30:58 -0300 | [diff] [blame] | 278 | } |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 279 | |
| 280 | /* read requested channels results */ |
| 281 | ezx_pcap_read(pcap, PCAP_REG_ADC, &tmp); |
| 282 | tmp &= ~(PCAP_ADC_ADA1_MASK | PCAP_ADC_ADA2_MASK); |
| 283 | tmp |= (req->ch[0] << PCAP_ADC_ADA1_SHIFT); |
| 284 | tmp |= (req->ch[1] << PCAP_ADC_ADA2_SHIFT); |
| 285 | ezx_pcap_write(pcap, PCAP_REG_ADC, tmp); |
| 286 | ezx_pcap_read(pcap, PCAP_REG_ADR, &tmp); |
| 287 | res[0] = (tmp & PCAP_ADR_ADD1_MASK) >> PCAP_ADR_ADD1_SHIFT; |
| 288 | res[1] = (tmp & PCAP_ADR_ADD2_MASK) >> PCAP_ADR_ADD2_SHIFT; |
| 289 | |
| 290 | pcap->adc_queue[pcap->adc_head] = NULL; |
| 291 | pcap->adc_head = (pcap->adc_head + 1) & (PCAP_ADC_MAXQ - 1); |
| 292 | mutex_unlock(&pcap->adc_mutex); |
| 293 | |
| 294 | /* pass the results and release memory */ |
| 295 | req->callback(req->data, res); |
| 296 | kfree(req); |
| 297 | |
| 298 | /* trigger next conversion (if any) on queue */ |
| 299 | pcap_adc_trigger(pcap); |
| 300 | |
| 301 | return IRQ_HANDLED; |
| 302 | } |
| 303 | |
| 304 | int pcap_adc_async(struct pcap_chip *pcap, u8 bank, u32 flags, u8 ch[], |
| 305 | void *callback, void *data) |
| 306 | { |
| 307 | struct pcap_adc_request *req; |
| 308 | |
| 309 | /* This will be freed after we have a result */ |
| 310 | req = kmalloc(sizeof(struct pcap_adc_request), GFP_KERNEL); |
| 311 | if (!req) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | req->bank = bank; |
| 315 | req->flags = flags; |
| 316 | req->ch[0] = ch[0]; |
| 317 | req->ch[1] = ch[1]; |
| 318 | req->callback = callback; |
| 319 | req->data = data; |
| 320 | |
| 321 | mutex_lock(&pcap->adc_mutex); |
| 322 | if (pcap->adc_queue[pcap->adc_tail]) { |
| 323 | mutex_unlock(&pcap->adc_mutex); |
| 324 | kfree(req); |
| 325 | return -EBUSY; |
| 326 | } |
| 327 | pcap->adc_queue[pcap->adc_tail] = req; |
| 328 | pcap->adc_tail = (pcap->adc_tail + 1) & (PCAP_ADC_MAXQ - 1); |
| 329 | mutex_unlock(&pcap->adc_mutex); |
| 330 | |
| 331 | /* start conversion */ |
| 332 | pcap_adc_trigger(pcap); |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | EXPORT_SYMBOL_GPL(pcap_adc_async); |
| 337 | |
| 338 | static void pcap_adc_sync_cb(void *param, u16 res[]) |
| 339 | { |
| 340 | struct pcap_adc_sync_request *req = param; |
| 341 | |
| 342 | req->res[0] = res[0]; |
| 343 | req->res[1] = res[1]; |
| 344 | complete(&req->completion); |
| 345 | } |
| 346 | |
| 347 | int pcap_adc_sync(struct pcap_chip *pcap, u8 bank, u32 flags, u8 ch[], |
| 348 | u16 res[]) |
| 349 | { |
| 350 | struct pcap_adc_sync_request sync_data; |
| 351 | int ret; |
| 352 | |
| 353 | init_completion(&sync_data.completion); |
| 354 | ret = pcap_adc_async(pcap, bank, flags, ch, pcap_adc_sync_cb, |
| 355 | &sync_data); |
| 356 | if (ret) |
| 357 | return ret; |
| 358 | wait_for_completion(&sync_data.completion); |
| 359 | res[0] = sync_data.res[0]; |
| 360 | res[1] = sync_data.res[1]; |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | EXPORT_SYMBOL_GPL(pcap_adc_sync); |
| 365 | |
| 366 | /* subdevs */ |
| 367 | static int pcap_remove_subdev(struct device *dev, void *unused) |
| 368 | { |
| 369 | platform_device_unregister(to_platform_device(dev)); |
| 370 | return 0; |
| 371 | } |
| 372 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 373 | static int pcap_add_subdev(struct pcap_chip *pcap, |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 374 | struct pcap_subdev *subdev) |
| 375 | { |
| 376 | struct platform_device *pdev; |
Axel Lin | 09ff21e | 2010-08-24 13:45:49 +0800 | [diff] [blame] | 377 | int ret; |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 378 | |
| 379 | pdev = platform_device_alloc(subdev->name, subdev->id); |
Axel Lin | 09ff21e | 2010-08-24 13:45:49 +0800 | [diff] [blame] | 380 | if (!pdev) |
| 381 | return -ENOMEM; |
| 382 | |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 383 | pdev->dev.parent = &pcap->spi->dev; |
| 384 | pdev->dev.platform_data = subdev->platform_data; |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 385 | |
Axel Lin | 09ff21e | 2010-08-24 13:45:49 +0800 | [diff] [blame] | 386 | ret = platform_device_add(pdev); |
| 387 | if (ret) |
| 388 | platform_device_put(pdev); |
| 389 | |
| 390 | return ret; |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 391 | } |
| 392 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 393 | static int ezx_pcap_remove(struct spi_device *spi) |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 394 | { |
Jingoo Han | 7c478b4 | 2013-04-06 15:43:48 +0900 | [diff] [blame] | 395 | struct pcap_chip *pcap = spi_get_drvdata(spi); |
Wei Yongjun | 8972026 | 2013-09-25 15:37:45 +0800 | [diff] [blame] | 396 | int i; |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 397 | |
| 398 | /* remove all registered subdevs */ |
| 399 | device_for_each_child(&spi->dev, NULL, pcap_remove_subdev); |
| 400 | |
| 401 | /* cleanup ADC */ |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 402 | mutex_lock(&pcap->adc_mutex); |
| 403 | for (i = 0; i < PCAP_ADC_MAXQ; i++) |
| 404 | kfree(pcap->adc_queue[i]); |
| 405 | mutex_unlock(&pcap->adc_mutex); |
| 406 | |
| 407 | /* cleanup irqchip */ |
| 408 | for (i = pcap->irq_base; i < (pcap->irq_base + PCAP_NIRQS); i++) |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 409 | irq_set_chip_and_handler(i, NULL, NULL); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 410 | |
| 411 | destroy_workqueue(pcap->workqueue); |
| 412 | |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 416 | static int ezx_pcap_probe(struct spi_device *spi) |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 417 | { |
Jingoo Han | 334a41c | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 418 | struct pcap_platform_data *pdata = dev_get_platdata(&spi->dev); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 419 | struct pcap_chip *pcap; |
| 420 | int i, adc_irq; |
| 421 | int ret = -ENODEV; |
| 422 | |
| 423 | /* platform data is required */ |
| 424 | if (!pdata) |
| 425 | goto ret; |
| 426 | |
Jingoo Han | 1ba895e | 2013-02-20 18:30:55 +0900 | [diff] [blame] | 427 | pcap = devm_kzalloc(&spi->dev, sizeof(*pcap), GFP_KERNEL); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 428 | if (!pcap) { |
| 429 | ret = -ENOMEM; |
| 430 | goto ret; |
| 431 | } |
| 432 | |
| 433 | mutex_init(&pcap->io_mutex); |
| 434 | mutex_init(&pcap->adc_mutex); |
| 435 | INIT_WORK(&pcap->isr_work, pcap_isr_work); |
| 436 | INIT_WORK(&pcap->msr_work, pcap_msr_work); |
Jingoo Han | 7c478b4 | 2013-04-06 15:43:48 +0900 | [diff] [blame] | 437 | spi_set_drvdata(spi, pcap); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 438 | |
| 439 | /* setup spi */ |
| 440 | spi->bits_per_word = 32; |
| 441 | spi->mode = SPI_MODE_0 | (pdata->config & PCAP_CS_AH ? SPI_CS_HIGH : 0); |
| 442 | ret = spi_setup(spi); |
| 443 | if (ret) |
Jingoo Han | 1ba895e | 2013-02-20 18:30:55 +0900 | [diff] [blame] | 444 | goto ret; |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 445 | |
| 446 | pcap->spi = spi; |
| 447 | |
| 448 | /* setup irq */ |
| 449 | pcap->irq_base = pdata->irq_base; |
| 450 | pcap->workqueue = create_singlethread_workqueue("pcapd"); |
| 451 | if (!pcap->workqueue) { |
Axel Lin | 47dabae | 2010-10-19 20:28:24 +0800 | [diff] [blame] | 452 | ret = -ENOMEM; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 453 | dev_err(&spi->dev, "can't create pcap thread\n"); |
Jingoo Han | 1ba895e | 2013-02-20 18:30:55 +0900 | [diff] [blame] | 454 | goto ret; |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* redirect interrupts to AP, except adcdone2 */ |
| 458 | if (!(pdata->config & PCAP_SECOND_PORT)) |
| 459 | ezx_pcap_write(pcap, PCAP_REG_INT_SEL, |
| 460 | (1 << PCAP_IRQ_ADCDONE2)); |
| 461 | |
| 462 | /* setup irq chip */ |
| 463 | for (i = pcap->irq_base; i < (pcap->irq_base + PCAP_NIRQS); i++) { |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 464 | irq_set_chip_and_handler(i, &pcap_irq_chip, handle_simple_irq); |
| 465 | irq_set_chip_data(i, pcap); |
Rob Herring | 9bd09f3 | 2015-07-27 15:55:20 -0500 | [diff] [blame] | 466 | irq_clear_status_flags(i, IRQ_NOREQUEST | IRQ_NOPROBE); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* mask/ack all PCAP interrupts */ |
| 470 | ezx_pcap_write(pcap, PCAP_REG_MSR, PCAP_MASK_ALL_INTERRUPT); |
| 471 | ezx_pcap_write(pcap, PCAP_REG_ISR, PCAP_CLEAR_INTERRUPT_REGISTER); |
| 472 | pcap->msr = PCAP_MASK_ALL_INTERRUPT; |
| 473 | |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 474 | irq_set_irq_type(spi->irq, IRQ_TYPE_EDGE_RISING); |
Thomas Gleixner | c89fc9a | 2015-07-13 20:44:46 +0000 | [diff] [blame] | 475 | irq_set_chained_handler_and_data(spi->irq, pcap_irq_handler, pcap); |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 476 | irq_set_irq_wake(spi->irq, 1); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 477 | |
| 478 | /* ADC */ |
| 479 | adc_irq = pcap_to_irq(pcap, (pdata->config & PCAP_SECOND_PORT) ? |
| 480 | PCAP_IRQ_ADCDONE2 : PCAP_IRQ_ADCDONE); |
| 481 | |
Jingoo Han | 1ba895e | 2013-02-20 18:30:55 +0900 | [diff] [blame] | 482 | ret = devm_request_irq(&spi->dev, adc_irq, pcap_adc_irq, 0, "ADC", |
| 483 | pcap); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 484 | if (ret) |
| 485 | goto free_irqchip; |
| 486 | |
| 487 | /* setup subdevs */ |
| 488 | for (i = 0; i < pdata->num_subdevs; i++) { |
| 489 | ret = pcap_add_subdev(pcap, &pdata->subdevs[i]); |
| 490 | if (ret) |
| 491 | goto remove_subdevs; |
| 492 | } |
| 493 | |
| 494 | /* board specific quirks */ |
| 495 | if (pdata->init) |
| 496 | pdata->init(pcap); |
| 497 | |
| 498 | return 0; |
| 499 | |
| 500 | remove_subdevs: |
| 501 | device_for_each_child(&spi->dev, NULL, pcap_remove_subdev); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 502 | free_irqchip: |
| 503 | for (i = pcap->irq_base; i < (pcap->irq_base + PCAP_NIRQS); i++) |
Thomas Gleixner | d5bb122 | 2011-03-25 11:12:32 +0000 | [diff] [blame] | 504 | irq_set_chip_and_handler(i, NULL, NULL); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 505 | /* destroy_workqueue: */ |
| 506 | destroy_workqueue(pcap->workqueue); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 507 | ret: |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | static struct spi_driver ezxpcap_driver = { |
| 512 | .probe = ezx_pcap_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 513 | .remove = ezx_pcap_remove, |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 514 | .driver = { |
| 515 | .name = "ezx-pcap", |
| 516 | .owner = THIS_MODULE, |
| 517 | }, |
| 518 | }; |
| 519 | |
| 520 | static int __init ezx_pcap_init(void) |
| 521 | { |
| 522 | return spi_register_driver(&ezxpcap_driver); |
| 523 | } |
| 524 | |
| 525 | static void __exit ezx_pcap_exit(void) |
| 526 | { |
| 527 | spi_unregister_driver(&ezxpcap_driver); |
| 528 | } |
| 529 | |
Antonio Ospite | f078237 | 2009-07-31 15:55:45 -0700 | [diff] [blame] | 530 | subsys_initcall(ezx_pcap_init); |
Daniel Ribeiro | 13a09f9 | 2009-05-28 15:43:37 -0300 | [diff] [blame] | 531 | module_exit(ezx_pcap_exit); |
| 532 | |
| 533 | MODULE_LICENSE("GPL"); |
| 534 | MODULE_AUTHOR("Daniel Ribeiro / Harald Welte"); |
| 535 | MODULE_DESCRIPTION("Motorola PCAP2 ASIC Driver"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 536 | MODULE_ALIAS("spi:ezx-pcap"); |