Sascha Hauer | 8238add | 2009-08-19 01:40:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
| 3 | * |
| 4 | * This code is in parts based on wm8350-core.c and pcf50633-core.c |
| 5 | * |
| 6 | * Initial development of this code was funded by |
| 7 | * Phytec Messtechnik GmbH, http://www.phytec.de |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/mfd/mc13783-private.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/mfd/mc13783.h> |
| 27 | #include <linux/completion.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/mfd/core.h> |
| 30 | #include <linux/spi/spi.h> |
| 31 | #include <linux/uaccess.h> |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/irq.h> |
| 37 | |
| 38 | #define MC13783_MAX_REG_NUM 0x3f |
| 39 | #define MC13783_FRAME_MASK 0x00ffffff |
| 40 | #define MC13783_MAX_REG_NUM 0x3f |
| 41 | #define MC13783_REG_NUM_SHIFT 0x19 |
| 42 | #define MC13783_WRITE_BIT_SHIFT 31 |
| 43 | |
| 44 | static inline int spi_rw(struct spi_device *spi, u8 * buf, size_t len) |
| 45 | { |
| 46 | struct spi_transfer t = { |
| 47 | .tx_buf = (const void *)buf, |
| 48 | .rx_buf = buf, |
| 49 | .len = len, |
| 50 | .cs_change = 0, |
| 51 | .delay_usecs = 0, |
| 52 | }; |
| 53 | struct spi_message m; |
| 54 | |
| 55 | spi_message_init(&m); |
| 56 | spi_message_add_tail(&t, &m); |
| 57 | if (spi_sync(spi, &m) != 0 || m.status != 0) |
| 58 | return -EINVAL; |
| 59 | return len - m.actual_length; |
| 60 | } |
| 61 | |
| 62 | static int mc13783_read(struct mc13783 *mc13783, int reg_num, u32 *reg_val) |
| 63 | { |
| 64 | unsigned int frame = 0; |
| 65 | int ret = 0; |
| 66 | |
| 67 | if (reg_num > MC13783_MAX_REG_NUM) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | frame |= reg_num << MC13783_REG_NUM_SHIFT; |
| 71 | |
| 72 | ret = spi_rw(mc13783->spi_device, (u8 *)&frame, 4); |
| 73 | |
| 74 | *reg_val = frame & MC13783_FRAME_MASK; |
| 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | static int mc13783_write(struct mc13783 *mc13783, int reg_num, u32 reg_val) |
| 80 | { |
| 81 | unsigned int frame = 0; |
| 82 | |
| 83 | if (reg_num > MC13783_MAX_REG_NUM) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | frame |= (1 << MC13783_WRITE_BIT_SHIFT); |
| 87 | frame |= reg_num << MC13783_REG_NUM_SHIFT; |
| 88 | frame |= reg_val & MC13783_FRAME_MASK; |
| 89 | |
| 90 | return spi_rw(mc13783->spi_device, (u8 *)&frame, 4); |
| 91 | } |
| 92 | |
| 93 | int mc13783_reg_read(struct mc13783 *mc13783, int reg_num, u32 *reg_val) |
| 94 | { |
| 95 | int ret; |
| 96 | |
| 97 | mutex_lock(&mc13783->io_lock); |
| 98 | ret = mc13783_read(mc13783, reg_num, reg_val); |
| 99 | mutex_unlock(&mc13783->io_lock); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(mc13783_reg_read); |
| 104 | |
| 105 | int mc13783_reg_write(struct mc13783 *mc13783, int reg_num, u32 reg_val) |
| 106 | { |
| 107 | int ret; |
| 108 | |
| 109 | mutex_lock(&mc13783->io_lock); |
| 110 | ret = mc13783_write(mc13783, reg_num, reg_val); |
| 111 | mutex_unlock(&mc13783->io_lock); |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(mc13783_reg_write); |
| 116 | |
| 117 | /** |
| 118 | * mc13783_set_bits - Bitmask write |
| 119 | * |
| 120 | * @mc13783: Pointer to mc13783 control structure |
| 121 | * @reg: Register to access |
| 122 | * @mask: Mask of bits to change |
| 123 | * @val: Value to set for masked bits |
| 124 | */ |
| 125 | int mc13783_set_bits(struct mc13783 *mc13783, int reg, u32 mask, u32 val) |
| 126 | { |
| 127 | u32 tmp; |
| 128 | int ret; |
| 129 | |
| 130 | mutex_lock(&mc13783->io_lock); |
| 131 | |
| 132 | ret = mc13783_read(mc13783, reg, &tmp); |
| 133 | tmp = (tmp & ~mask) | val; |
| 134 | if (ret == 0) |
| 135 | ret = mc13783_write(mc13783, reg, tmp); |
| 136 | |
| 137 | mutex_unlock(&mc13783->io_lock); |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(mc13783_set_bits); |
| 142 | |
| 143 | int mc13783_register_irq(struct mc13783 *mc13783, int irq, |
| 144 | void (*handler) (int, void *), void *data) |
| 145 | { |
| 146 | if (irq < 0 || irq > MC13783_NUM_IRQ || !handler) |
| 147 | return -EINVAL; |
| 148 | |
| 149 | if (WARN_ON(mc13783->irq_handler[irq].handler)) |
| 150 | return -EBUSY; |
| 151 | |
| 152 | mutex_lock(&mc13783->io_lock); |
| 153 | mc13783->irq_handler[irq].handler = handler; |
| 154 | mc13783->irq_handler[irq].data = data; |
| 155 | mutex_unlock(&mc13783->io_lock); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | EXPORT_SYMBOL_GPL(mc13783_register_irq); |
| 160 | |
| 161 | int mc13783_free_irq(struct mc13783 *mc13783, int irq) |
| 162 | { |
| 163 | if (irq < 0 || irq > MC13783_NUM_IRQ) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | mutex_lock(&mc13783->io_lock); |
| 167 | mc13783->irq_handler[irq].handler = NULL; |
| 168 | mutex_unlock(&mc13783->io_lock); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | EXPORT_SYMBOL_GPL(mc13783_free_irq); |
| 173 | |
| 174 | static void mc13783_irq_work(struct work_struct *work) |
| 175 | { |
| 176 | struct mc13783 *mc13783 = container_of(work, struct mc13783, work); |
| 177 | int i; |
| 178 | unsigned int adc_sts; |
| 179 | |
| 180 | /* check if the adc has finished any completion */ |
| 181 | mc13783_reg_read(mc13783, MC13783_REG_INTERRUPT_STATUS_0, &adc_sts); |
| 182 | mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_STATUS_0, |
| 183 | adc_sts & MC13783_INT_STAT_ADCDONEI); |
| 184 | |
| 185 | if (adc_sts & MC13783_INT_STAT_ADCDONEI) |
| 186 | complete_all(&mc13783->adc_done); |
| 187 | |
| 188 | for (i = 0; i < MC13783_NUM_IRQ; i++) |
| 189 | if (mc13783->irq_handler[i].handler) |
| 190 | mc13783->irq_handler[i].handler(i, |
| 191 | mc13783->irq_handler[i].data); |
| 192 | enable_irq(mc13783->irq); |
| 193 | } |
| 194 | |
| 195 | static irqreturn_t mc13783_interrupt(int irq, void *dev_id) |
| 196 | { |
| 197 | struct mc13783 *mc13783 = dev_id; |
| 198 | |
| 199 | disable_irq_nosync(irq); |
| 200 | |
| 201 | schedule_work(&mc13783->work); |
| 202 | return IRQ_HANDLED; |
| 203 | } |
| 204 | |
| 205 | /* set adc to ts interrupt mode, which generates touchscreen wakeup interrupt */ |
| 206 | static inline void mc13783_adc_set_ts_irq_mode(struct mc13783 *mc13783) |
| 207 | { |
| 208 | unsigned int reg_adc0, reg_adc1; |
| 209 | |
| 210 | reg_adc0 = MC13783_ADC0_ADREFEN | MC13783_ADC0_ADREFMODE |
| 211 | | MC13783_ADC0_TSMOD0; |
| 212 | reg_adc1 = MC13783_ADC1_ADEN | MC13783_ADC1_ADTRIGIGN; |
| 213 | |
| 214 | mc13783_reg_write(mc13783, MC13783_REG_ADC_0, reg_adc0); |
| 215 | mc13783_reg_write(mc13783, MC13783_REG_ADC_1, reg_adc1); |
| 216 | } |
| 217 | |
| 218 | int mc13783_adc_do_conversion(struct mc13783 *mc13783, unsigned int mode, |
| 219 | unsigned int channel, unsigned int *sample) |
| 220 | { |
| 221 | unsigned int reg_adc0, reg_adc1; |
| 222 | int i; |
| 223 | |
| 224 | mutex_lock(&mc13783->adc_conv_lock); |
| 225 | |
| 226 | /* set up auto incrementing anyway to make quick read */ |
| 227 | reg_adc0 = MC13783_ADC0_ADINC1 | MC13783_ADC0_ADINC2; |
| 228 | /* enable the adc, ignore external triggering and set ASC to trigger |
| 229 | * conversion */ |
| 230 | reg_adc1 = MC13783_ADC1_ADEN | MC13783_ADC1_ADTRIGIGN |
| 231 | | MC13783_ADC1_ASC; |
| 232 | |
| 233 | /* setup channel number */ |
| 234 | if (channel > 7) |
| 235 | reg_adc1 |= MC13783_ADC1_ADSEL; |
| 236 | |
| 237 | switch (mode) { |
| 238 | case MC13783_ADC_MODE_TS: |
| 239 | /* enables touch screen reference mode and set touchscreen mode |
| 240 | * to position mode */ |
| 241 | reg_adc0 |= MC13783_ADC0_ADREFEN | MC13783_ADC0_ADREFMODE |
| 242 | | MC13783_ADC0_TSMOD0 | MC13783_ADC0_TSMOD1; |
| 243 | reg_adc1 |= 4 << MC13783_ADC1_CHAN1_SHIFT; |
| 244 | break; |
| 245 | case MC13783_ADC_MODE_SINGLE_CHAN: |
| 246 | reg_adc1 |= (channel & 0x7) << MC13783_ADC1_CHAN0_SHIFT; |
| 247 | reg_adc1 |= MC13783_ADC1_RAND; |
| 248 | break; |
| 249 | case MC13783_ADC_MODE_MULT_CHAN: |
| 250 | reg_adc1 |= 4 << MC13783_ADC1_CHAN1_SHIFT; |
| 251 | break; |
| 252 | default: |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | mc13783_reg_write(mc13783, MC13783_REG_ADC_0, reg_adc0); |
| 257 | mc13783_reg_write(mc13783, MC13783_REG_ADC_1, reg_adc1); |
| 258 | |
| 259 | wait_for_completion_interruptible(&mc13783->adc_done); |
| 260 | |
| 261 | for (i = 0; i < 4; i++) |
| 262 | mc13783_reg_read(mc13783, MC13783_REG_ADC_2, &sample[i]); |
| 263 | |
| 264 | if (mc13783->ts_active) |
| 265 | mc13783_adc_set_ts_irq_mode(mc13783); |
| 266 | |
| 267 | mutex_unlock(&mc13783->adc_conv_lock); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | EXPORT_SYMBOL_GPL(mc13783_adc_do_conversion); |
| 272 | |
| 273 | void mc13783_adc_set_ts_status(struct mc13783 *mc13783, unsigned int status) |
| 274 | { |
| 275 | mc13783->ts_active = status; |
| 276 | } |
| 277 | EXPORT_SYMBOL_GPL(mc13783_adc_set_ts_status); |
| 278 | |
| 279 | static int mc13783_check_revision(struct mc13783 *mc13783) |
| 280 | { |
| 281 | u32 rev_id, rev1, rev2, finid, icid; |
| 282 | |
| 283 | mc13783_read(mc13783, MC13783_REG_REVISION, &rev_id); |
| 284 | |
| 285 | rev1 = (rev_id & 0x018) >> 3; |
| 286 | rev2 = (rev_id & 0x007); |
| 287 | icid = (rev_id & 0x01C0) >> 6; |
| 288 | finid = (rev_id & 0x01E00) >> 9; |
| 289 | |
| 290 | /* Ver 0.2 is actually 3.2a. Report as 3.2 */ |
| 291 | if ((rev1 == 0) && (rev2 == 2)) |
| 292 | rev1 = 3; |
| 293 | |
| 294 | if (rev1 == 0 || icid != 2) { |
| 295 | dev_err(mc13783->dev, "No MC13783 detected.\n"); |
| 296 | return -ENODEV; |
| 297 | } |
| 298 | |
| 299 | mc13783->revision = ((rev1 * 10) + rev2); |
| 300 | dev_info(mc13783->dev, "MC13783 Rev %d.%d FinVer %x detected\n", rev1, |
| 301 | rev2, finid); |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Register a client device. This is non-fatal since there is no need to |
| 308 | * fail the entire device init due to a single platform device failing. |
| 309 | */ |
| 310 | static void mc13783_client_dev_register(struct mc13783 *mc13783, |
| 311 | const char *name) |
| 312 | { |
| 313 | struct mfd_cell cell = {}; |
| 314 | |
| 315 | cell.name = name; |
| 316 | |
| 317 | mfd_add_devices(mc13783->dev, -1, &cell, 1, NULL, 0); |
| 318 | } |
| 319 | |
| 320 | static int __devinit mc13783_probe(struct spi_device *spi) |
| 321 | { |
| 322 | struct mc13783 *mc13783; |
| 323 | struct mc13783_platform_data *pdata = spi->dev.platform_data; |
| 324 | int ret; |
| 325 | |
| 326 | mc13783 = kzalloc(sizeof(struct mc13783), GFP_KERNEL); |
| 327 | if (!mc13783) |
| 328 | return -ENOMEM; |
| 329 | |
| 330 | dev_set_drvdata(&spi->dev, mc13783); |
| 331 | spi->mode = SPI_MODE_0 | SPI_CS_HIGH; |
| 332 | spi->bits_per_word = 32; |
| 333 | spi_setup(spi); |
| 334 | |
| 335 | mc13783->spi_device = spi; |
| 336 | mc13783->dev = &spi->dev; |
| 337 | mc13783->irq = spi->irq; |
| 338 | |
| 339 | INIT_WORK(&mc13783->work, mc13783_irq_work); |
| 340 | mutex_init(&mc13783->io_lock); |
| 341 | mutex_init(&mc13783->adc_conv_lock); |
| 342 | init_completion(&mc13783->adc_done); |
| 343 | |
| 344 | if (pdata) { |
| 345 | mc13783->flags = pdata->flags; |
| 346 | mc13783->regulators = pdata->regulators; |
| 347 | mc13783->num_regulators = pdata->num_regulators; |
| 348 | } |
| 349 | |
| 350 | if (mc13783_check_revision(mc13783)) { |
| 351 | ret = -ENODEV; |
| 352 | goto err_out; |
| 353 | } |
| 354 | |
| 355 | /* clear and mask all interrupts */ |
| 356 | mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_STATUS_0, 0x00ffffff); |
| 357 | mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_MASK_0, 0x00ffffff); |
| 358 | mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_STATUS_1, 0x00ffffff); |
| 359 | mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_MASK_1, 0x00ffffff); |
| 360 | |
| 361 | /* unmask adcdone interrupts */ |
| 362 | mc13783_set_bits(mc13783, MC13783_REG_INTERRUPT_MASK_0, |
| 363 | MC13783_INT_MASK_ADCDONEM, 0); |
| 364 | |
| 365 | ret = request_irq(mc13783->irq, mc13783_interrupt, |
| 366 | IRQF_DISABLED | IRQF_TRIGGER_HIGH, "mc13783", |
| 367 | mc13783); |
| 368 | if (ret) |
| 369 | goto err_out; |
| 370 | |
| 371 | if (mc13783->flags & MC13783_USE_CODEC) |
| 372 | mc13783_client_dev_register(mc13783, "mc13783-codec"); |
| 373 | if (mc13783->flags & MC13783_USE_ADC) |
| 374 | mc13783_client_dev_register(mc13783, "mc13783-adc"); |
| 375 | if (mc13783->flags & MC13783_USE_RTC) |
| 376 | mc13783_client_dev_register(mc13783, "mc13783-rtc"); |
| 377 | if (mc13783->flags & MC13783_USE_REGULATOR) |
| 378 | mc13783_client_dev_register(mc13783, "mc13783-regulator"); |
| 379 | if (mc13783->flags & MC13783_USE_TOUCHSCREEN) |
| 380 | mc13783_client_dev_register(mc13783, "mc13783-ts"); |
| 381 | |
| 382 | return 0; |
| 383 | |
| 384 | err_out: |
| 385 | kfree(mc13783); |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | static int __devexit mc13783_remove(struct spi_device *spi) |
| 390 | { |
| 391 | struct mc13783 *mc13783; |
| 392 | |
| 393 | mc13783 = dev_get_drvdata(&spi->dev); |
| 394 | |
| 395 | free_irq(mc13783->irq, mc13783); |
| 396 | |
| 397 | mfd_remove_devices(&spi->dev); |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static struct spi_driver pmic_driver = { |
| 403 | .driver = { |
| 404 | .name = "mc13783", |
| 405 | .bus = &spi_bus_type, |
| 406 | .owner = THIS_MODULE, |
| 407 | }, |
| 408 | .probe = mc13783_probe, |
| 409 | .remove = __devexit_p(mc13783_remove), |
| 410 | }; |
| 411 | |
| 412 | static int __init pmic_init(void) |
| 413 | { |
| 414 | return spi_register_driver(&pmic_driver); |
| 415 | } |
| 416 | subsys_initcall(pmic_init); |
| 417 | |
| 418 | static void __exit pmic_exit(void) |
| 419 | { |
| 420 | spi_unregister_driver(&pmic_driver); |
| 421 | } |
| 422 | module_exit(pmic_exit); |
| 423 | |
| 424 | MODULE_DESCRIPTION("Core/Protocol driver for Freescale MC13783 PMIC"); |
| 425 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |
| 426 | MODULE_LICENSE("GPL"); |
| 427 | |