2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/slab.h>
6 #include <linux/delay.h>
7 #include <linux/mfd/core.h>
8 #include <linux/pm_runtime.h>
9 #include <linux/regulator/consumer.h>
10 #include <linux/regulator/machine.h>
11 #include <linux/gpio.h>
13 #include <linux/mfd/tlv320aic3262-core.h>
14 #include <linux/mfd/tlv320aic3262-registers.h>
15 #include <mach/gpio.h>
16 #include <mach/iomux.h>
25 struct aic3262_gpio aic3262_gpio_control[] = {
27 .reg = AIC3262_GPIO1_IO_CNTL,
28 .mask = AIC3262_GPIO_D6_D2,
29 .shift = AIC3262_GPIO_D2_SHIFT,
32 .reg = AIC3262_GPIO2_IO_CNTL,
33 .mask = AIC3262_GPIO_D6_D2,
34 .shift = AIC3262_GPIO_D2_SHIFT,
37 .reg = AIC3262_GPI1_EN,
38 .mask = AIC3262_GPI1_D2_D1,
39 .shift = AIC3262_GPIO_D1_SHIFT,
42 .reg = AIC3262_GPI2_EN,
43 .mask = AIC3262_GPI2_D5_D4,
44 .shift = AIC3262_GPIO_D4_SHIFT,
47 .reg = AIC3262_GPO1_OUT_CNTL,
48 .mask = AIC3262_GPO1_D4_D1,
49 .shift = AIC3262_GPIO_D1_SHIFT,
52 static int aic3262_read(struct aic3262 *aic3262, unsigned int reg,
53 int bytes, void *dest)
61 ret = aic3262->read_dev(aic3262, reg, bytes, dest);
65 /* for (i = 0; i < bytes / 2; i++) {
66 dev_vdbg(aic3262->dev, "Read %04x from R%d(0x%x)\n",
67 buf[i], reg + i, reg + i);
74 * aic3262_reg_read: Read a single TLV320AIC3262 register.
76 * @aic3262: Device to read from.
77 * @reg: Register to read.
79 int aic3262_reg_read(struct aic3262 *aic3262, unsigned int reg)
84 mutex_lock(&aic3262->io_lock);
86 ret = aic3262_read(aic3262, reg, 1, &val);
88 mutex_unlock(&aic3262->io_lock);
95 EXPORT_SYMBOL_GPL(aic3262_reg_read);
98 * aic3262_bulk_read: Read multiple TLV320AIC3262 registers
100 * @aic3262: Device to read from
101 * @reg: First register
102 * @count: Number of registers
103 * @buf: Buffer to fill. The data will be returned big endian.
105 int aic3262_bulk_read(struct aic3262 *aic3262, unsigned int reg,
110 mutex_lock(&aic3262->io_lock);
112 ret = aic3262_read(aic3262, reg, count, buf);
114 mutex_unlock(&aic3262->io_lock);
118 EXPORT_SYMBOL_GPL(aic3262_bulk_read);
120 static int aic3262_write(struct aic3262 *aic3262, unsigned int reg,
121 int bytes, const void *src)
123 //const u8 *buf = src;
126 // BUG_ON(bytes % 2);
129 /* for (i = 0; i < bytes / 2; i++) {
130 dev_vdbg(aic3262->dev, "Write %04x to R%d(0x%x)\n",
131 buf[i], reg + i, reg + i);
134 return aic3262->write_dev(aic3262, reg, bytes, src);
138 * aic3262_reg_write: Write a single TLV320AIC3262 register.
140 * @aic3262: Device to write to.
141 * @reg: Register to write to.
142 * @val: Value to write.
144 int aic3262_reg_write(struct aic3262 *aic3262, unsigned int reg,
149 // val = cpu_to_be16(val);
151 mutex_lock(&aic3262->io_lock);
153 ret = aic3262_write(aic3262, reg, 1, &val);
155 mutex_unlock(&aic3262->io_lock);
159 EXPORT_SYMBOL_GPL(aic3262_reg_write);
162 * aic3262_bulk_write: Write multiple TLV320AIC3262 registers
164 * @aic3262: Device to write to
165 * @reg: First register
166 * @count: Number of registers
167 * @buf: Buffer to write from. Data must be big-endian formatted.
169 int aic3262_bulk_write(struct aic3262 *aic3262, unsigned int reg,
170 int count, const u8 *buf)
174 mutex_lock(&aic3262->io_lock);
176 ret = aic3262_write(aic3262, reg, count, buf);
178 mutex_unlock(&aic3262->io_lock);
182 EXPORT_SYMBOL_GPL(aic3262_bulk_write);
185 * aic3262_set_bits: Set the value of a bitfield in a TLV320AIC3262 register
187 * @aic3262: Device to write to.
188 * @reg: Register to write to.
189 * @mask: Mask of bits to set.
190 * @val: Value to set (unshifted)
192 int aic3262_set_bits(struct aic3262 *aic3262, unsigned int reg,
193 unsigned char mask, unsigned char val)
198 mutex_lock(&aic3262->io_lock);
200 ret = aic3262_read(aic3262, reg, 1, &r);
208 ret = aic3262_write(aic3262, reg, 1, &r);
211 mutex_unlock(&aic3262->io_lock);
215 EXPORT_SYMBOL_GPL(aic3262_set_bits);
216 /* to be changed -- Mukund*/
217 static struct resource aic3262_codec_resources[] = {
219 .start = AIC3262_IRQ_HEADSET_DETECT,
220 .end = AIC3262_IRQ_SPEAKER_OVER_TEMP,
221 .flags = IORESOURCE_IRQ,
225 static struct resource aic3262_gpio_resources[] = {
227 .start = AIC3262_GPIO1,
229 .flags = IORESOURCE_IRQ,
233 static struct mfd_cell aic3262_devs[] = {
235 .name = "tlv320aic3262-codec",
236 .num_resources = ARRAY_SIZE(aic3262_codec_resources),
237 .resources = aic3262_codec_resources,
241 .name = "tlv320aic3262-gpio",
242 .num_resources = ARRAY_SIZE(aic3262_gpio_resources),
243 .resources = aic3262_gpio_resources,
244 .pm_runtime_no_callbacks = true,
250 static int aic3262_suspend(struct device *dev)
252 struct aic3262 *aic3262 = dev_get_drvdata(dev);
255 /* Don't actually go through with the suspend if the CODEC is
256 * still active (eg, for audio passthrough from CP. */
257 // ret = aic3262_reg_read(aic3262, 20AIC3262_POWER_MANAGEMENT_1);
259 dev_err(dev, "Failed to read power status: %d\n", ret);
260 } else if (ret & TLV320AIC3262_VMID_SEL_MASK) {
261 dev_dbg(dev, "CODEC still active, ignoring suspend\n");
265 /* GPIO configuration state is saved here since we may be configuring
266 * the GPIO alternate functions even if we're not using the gpiolib
269 // ret = aic3262_read(aic3262, TLV320AIC3262_GPIO_1, TLV320AIC3262_NUM_GPIO_REGS * 2,
270 // &aic3262->gpio_regs);
272 dev_err(dev, "Failed to save GPIO registers: %d\n", ret);*/
275 /* Explicitly put the device into reset in case regulators
276 * don't get disabled in order to ensure consistent restart.
278 // aic3262_reg_write(aic3262, TLV320AIC3262_SOFTWARE_RESET, 0x8994);
280 aic3262->suspended = true;
285 static int aic3262_resume(struct device *dev)
287 struct aic3262 *aic3262 = dev_get_drvdata(dev);
290 /* We may have lied to the PM core about suspending */
291 /* if (!aic3262->suspended)
294 ret = aic3262_write(aic3262, TLV320AIC3262_INTERRUPT_STATUS_1_MASK,
295 TLV320AIC3262_NUM_IRQ_REGS * 2, &aic3262->irq_masks_cur);
297 dev_err(dev, "Failed to restore interrupt masks: %d\n", ret);
300 ret = aic3262_write(aic3262, TLV320AIC3262_GPIO_1, TLV320AIC3262_NUM_GPIO_REGS * 2,
301 &aic3262->gpio_regs);
303 dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
305 aic3262->suspended = false;
313 * Instantiate the generic non-control parts of the device.
315 static int aic3262_device_init(struct aic3262 *aic3262, int irq)
317 struct aic3262_pdata *pdata = aic3262->dev->platform_data;
321 unsigned int naudint = 0;
323 //printk("aic3262_device_init beginning\n");
325 mutex_init(&aic3262->io_lock);
326 dev_set_drvdata(aic3262->dev, aic3262);
328 if(pdata->gpio_reset){
329 ret = gpio_request(pdata->gpio_reset,"aic3262-reset-pin");
331 dev_err(aic3262->dev,"not able to acquire gpio %d for reseting the AIC3262 \n", pdata->gpio_reset);
334 rk30_mux_api_set(GPIO0B7_I2S8CHSDO3_NAME, GPIO0B_GPIO0B7);
335 gpio_direction_output(pdata->gpio_reset, 1);
337 gpio_direction_output(pdata->gpio_reset, 0);
339 gpio_direction_output(pdata->gpio_reset, 1);
340 // gpio_set_value(pdata->gpio_reset, 0);
346 /* run the codec through software reset */
347 ret = aic3262_reg_write(aic3262, AIC3262_RESET_REG, resetVal);
349 dev_err(aic3262->dev, "Could not write to AIC3262 register\n");
356 ret = aic3262_reg_read(aic3262, AIC3262_REV_PG_ID);
358 dev_err(aic3262->dev, "Failed to read ID register\n");
361 revID = (ret & AIC3262_REV_MASK) >> AIC3262_REV_SHIFT;
362 pgID = (ret & AIC3262_PG_MASK) >> AIC3262_PG_SHIFT;
365 devname = "TLV320AIC3262";
366 if (aic3262->type != TLV320AIC3262)
367 dev_warn(aic3262->dev, "Device registered as type %d\n",
369 aic3262->type = TLV320AIC3262;
372 dev_err(aic3262->dev, "Device is not a TLV320AIC3262, ID is %x\n",
379 dev_info(aic3262->dev, "%s revision %c\n", devname, 'D' + ret);
381 printk("aic3262_device_init %s revision %c\n", devname, 'D' + ret);
384 if(pdata->gpio_irq == 1) {
385 naudint = gpio_to_irq(pdata->naudint_irq);
386 gpio_request(pdata->naudint_irq,"aic3262-gpio-irq");
387 gpio_direction_input(pdata->naudint_irq);
390 naudint = pdata->naudint_irq;
392 aic3262->irq = naudint;
393 aic3262->irq_base = pdata->irq_base;
394 for(i = 0; i < AIC3262_NUM_GPIO;i++)
396 if(pdata->gpio[i].used)
398 if(pdata->gpio[i].in) // direction is input
400 // set direction to input for GPIO, and enable for GPI
401 aic3262_set_bits(aic3262, aic3262_gpio_control[i].reg,
402 aic3262_gpio_control[i].mask, 0x1 << aic3262_gpio_control[i].shift);
403 if(pdata->gpio[i].in_reg) // Some input modes, does not need extra registers to be written
404 aic3262_set_bits(aic3262, pdata->gpio[i].in_reg,
405 pdata->gpio[i].in_reg_bitmask,
406 pdata->gpio[i].value << pdata->gpio[i].in_reg_shift);
408 else // direction is output
411 aic3262_set_bits(aic3262, aic3262_gpio_control[i].reg,
412 aic3262_gpio_control[i].mask, pdata->gpio[i].value << aic3262_gpio_control[i].shift);
415 else // Disable the gpio/gpi/gpo
416 aic3262_set_bits(aic3262, aic3262_gpio_control[i].reg, aic3262_gpio_control[i].mask, 0x0);
424 /* codec interrupt */
425 ret = aic3262_irq_init(aic3262);
430 ret = mfd_add_devices(aic3262->dev, -1,
431 aic3262_devs, ARRAY_SIZE(aic3262_devs),
434 dev_err(aic3262->dev, "Failed to add children: %d\n", ret);
438 printk("aic3262_device_init added mfd devices \n");
439 pm_runtime_enable(aic3262->dev);
440 pm_runtime_resume(aic3262->dev);
445 aic3262_irq_exit(aic3262);
447 // mfd_remove_devices(aic3262->dev);
453 static void aic3262_device_exit(struct aic3262 *aic3262)
455 pm_runtime_disable(aic3262->dev);
456 mfd_remove_devices(aic3262->dev);
457 aic3262_irq_exit(aic3262);
461 static int aic3262_i2c_read_device(struct aic3262 *aic3262, unsigned int reg,
462 int bytes, void *dest)
464 struct i2c_client *i2c = aic3262->control_data;
465 aic326x_reg_union *aic_reg = (aic326x_reg_union *) ®
469 u8 page, book, offset;
470 page = aic_reg->aic326x_register.page;
471 book = aic_reg->aic326x_register.book;
472 offset = aic_reg->aic326x_register.offset;
473 if(aic3262->book_no != book) // change in book required.
475 // We should change to page 0.
476 // Change the book by writing to offset 127 of page 0
477 // Change the page back to whatever was set before change page
480 ret = i2c_master_send(i2c, (unsigned char *)buf, 2);
487 ret = i2c_master_send(i2c, (unsigned char *)buf, 2);
492 aic3262->book_no = book;
493 aic3262->page_no = 0x0;// To force a page change in the following if
496 if (aic3262->page_no != page) {
499 ret = i2c_master_send(i2c, (unsigned char *) buf, 2);
505 aic3262->page_no = page;
507 // Send the required offset
509 ret = i2c_master_send(i2c, (unsigned char *)buf, 1);
515 ret = i2c_master_recv(i2c, dest, bytes);
524 static int aic3262_i2c_write_device(struct aic3262 *aic3262, unsigned int reg,
525 int bytes, const void *src)
527 struct i2c_client *i2c = aic3262->control_data;
531 aic326x_reg_union *aic_reg = (aic326x_reg_union *) ®
533 //struct i2c_msg xfer[2];
535 u8 write_buf[bytes + 1];
536 u8 page, book, offset;
537 page = aic_reg->aic326x_register.page;
538 book = aic_reg->aic326x_register.book;
539 offset = aic_reg->aic326x_register.offset;
540 if(aic3262->book_no != book) // change in book required.
542 // We should change to page 0.
543 // Change the book by writing to offset 127 of page 0
544 // Change the page back to whatever was set before change page
547 ret = i2c_master_send(i2c, (unsigned char *)buf, 2);
555 ret = i2c_master_send(i2c, (unsigned char *)buf, 2);
560 aic3262->book_no = book;
561 aic3262->page_no = 0x0;// To force a page change in the following if
564 if (aic3262->page_no != page) {
567 ret = i2c_master_send(i2c, (unsigned char *) buf, 2);
572 aic3262->page_no = page;
574 // value = (char *) src;
575 //ret = i2c_transfer(i2c->adapter, xfer, 2);
576 //printk("%s:write ret = %d\n",__func__, ret);
577 // send the offset as first message
578 /* xfer[0].addr = i2c->addr;
579 xfer[0].flags = i2c->flags & I2C_M_TEN;
581 xfer[0].buf = (char *)&offset;
582 // Send the values in bulk
583 xfer[1].addr = i2c->addr;
584 xfer[1].flags = i2c->flags & I2C_M_TEN;
586 xfer[1].buf = (char *)src;
588 ret = i2c_transfer(i2c->adapter, xfer, 2);*/
590 memcpy(&buf[1], src, bytes);*/
591 // ret = i2c_master_send(i2c, (unsigned char *)buf, 1);
592 // printk("%s:write offset ret = %d\n",__func__, ret);
593 // ret = i2c_master_send(i2c, (unsigned char *)src, bytes);
594 /* value = (char *) src;
597 ret = i2c_master_send(i2c, buf, 2); */
598 write_buf[0] = offset;
599 memcpy(&write_buf[1], src, bytes);
600 ret = i2c_master_send(i2c, write_buf, bytes + 1);
603 if (ret != (bytes + 1) )
609 static int aic3262_i2c_probe(struct i2c_client *i2c,
610 const struct i2c_device_id *id)
612 struct aic3262 *aic3262;
614 printk("%s: entered \n", __FUNCTION__);
615 aic3262 = kzalloc(sizeof(struct aic3262), GFP_KERNEL);
619 printk("%s: allocated memory \n", __FUNCTION__);
620 i2c_set_clientdata(i2c, aic3262);
621 aic3262->dev = &i2c->dev;
622 aic3262->control_data = i2c;
623 aic3262->read_dev = aic3262_i2c_read_device;
624 aic3262->write_dev = aic3262_i2c_write_device;
625 // aic3262->irq = i2c->irq;
626 aic3262->type = id->driver_data;
627 aic3262->book_no = 255;
628 aic3262->page_no = 255;
630 return aic3262_device_init(aic3262, i2c->irq);
633 static int aic3262_i2c_remove(struct i2c_client *i2c)
635 struct aic3262 *aic3262 = i2c_get_clientdata(i2c);
637 aic3262_device_exit(aic3262);
642 static const struct i2c_device_id aic3262_i2c_id[] = {
643 { "tlv320aic3262", TLV320AIC3262 },
646 MODULE_DEVICE_TABLE(i2c, aic3262_i2c_id);
648 static UNIVERSAL_DEV_PM_OPS(aic3262_pm_ops, aic3262_suspend, aic3262_resume,
651 static struct i2c_driver aic3262_i2c_driver = {
653 .name = "tlv320aic3262",
654 .owner = THIS_MODULE,
655 .pm = &aic3262_pm_ops,
657 .probe = aic3262_i2c_probe,
658 .remove = aic3262_i2c_remove,
659 .id_table = aic3262_i2c_id,
662 static int __init aic3262_i2c_init(void)
665 printk("aic3262_mfd i2c_init \n");
666 ret = i2c_add_driver(&aic3262_i2c_driver);
668 pr_err("Failed to register aic3262 I2C driver: %d\n", ret);
672 module_init(aic3262_i2c_init);
674 static void __exit aic3262_i2c_exit(void)
676 i2c_del_driver(&aic3262_i2c_driver);
678 module_exit(aic3262_i2c_exit);
680 MODULE_DESCRIPTION("Core support for the TLV320AIC3262 audio CODEC");
681 MODULE_LICENSE("GPL");
682 MODULE_AUTHOR("Mukund Navada <navada@ti.comm>");