mfd: fusb302: change to host when connect type-c to standard-a cable
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / tlv320aic3262-core.c
1
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/i2c.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>
12
13 #include <linux/mfd/tlv320aic3262-core.h>
14 #include <linux/mfd/tlv320aic3262-registers.h>
15 #include <mach/gpio.h>
16 #include <mach/iomux.h>
17
18 #define DEBUG 1
19 struct aic3262_gpio
20 {
21         unsigned int reg;
22         u8 mask;
23         u8 shift;
24 };
25 struct aic3262_gpio  aic3262_gpio_control[] = {
26         {
27                 .reg = AIC3262_GPIO1_IO_CNTL,
28                 .mask = AIC3262_GPIO_D6_D2,
29                 .shift = AIC3262_GPIO_D2_SHIFT,
30         },
31         {
32                 .reg = AIC3262_GPIO2_IO_CNTL,
33                 .mask = AIC3262_GPIO_D6_D2, 
34                 .shift = AIC3262_GPIO_D2_SHIFT,
35         },
36         {
37                 .reg = AIC3262_GPI1_EN,
38                 .mask = AIC3262_GPI1_D2_D1,
39                 .shift = AIC3262_GPIO_D1_SHIFT,
40         },
41         {
42                 .reg = AIC3262_GPI2_EN,         
43                 .mask = AIC3262_GPI2_D5_D4,
44                 .shift = AIC3262_GPIO_D4_SHIFT,
45         },
46         {
47                 .reg = AIC3262_GPO1_OUT_CNTL,
48                 .mask = AIC3262_GPO1_D4_D1,
49                 .shift = AIC3262_GPIO_D1_SHIFT,
50         },
51 };
52 static int aic3262_read(struct aic3262 *aic3262, unsigned int reg,
53                        int bytes, void *dest)
54 {
55         int ret;
56         //u8 *buf = dest;
57
58 //      BUG_ON(bytes % 2);
59         BUG_ON(bytes <= 0);
60
61         ret = aic3262->read_dev(aic3262, reg, bytes, dest);
62         if (ret < 0)
63                 return ret;
64
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);
68         }*/
69
70         return ret;
71 }
72
73 /**
74  * aic3262_reg_read: Read a single TLV320AIC3262 register.
75  *
76  * @aic3262: Device to read from.
77  * @reg: Register to read.
78  */
79 int aic3262_reg_read(struct aic3262 *aic3262, unsigned int reg)
80 {
81         unsigned char val;
82         int ret;
83
84         mutex_lock(&aic3262->io_lock);
85
86         ret = aic3262_read(aic3262, reg, 1, &val);
87
88         mutex_unlock(&aic3262->io_lock);
89
90         if (ret < 0)
91                 return ret;
92         else
93                 return val;
94 }
95 EXPORT_SYMBOL_GPL(aic3262_reg_read);
96
97 /**
98  * aic3262_bulk_read: Read multiple TLV320AIC3262 registers
99  *
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.
104  */
105 int aic3262_bulk_read(struct aic3262 *aic3262, unsigned int reg,
106                      int count, u8 *buf)
107 {
108         int ret;
109
110         mutex_lock(&aic3262->io_lock);
111
112         ret = aic3262_read(aic3262, reg, count, buf);
113
114         mutex_unlock(&aic3262->io_lock);
115
116         return ret;
117 }
118 EXPORT_SYMBOL_GPL(aic3262_bulk_read);
119
120 static int aic3262_write(struct aic3262 *aic3262, unsigned int reg,
121                         int bytes, const void *src)
122 {
123         //const u8 *buf = src;
124         
125
126 //      BUG_ON(bytes % 2);
127         BUG_ON(bytes <= 0);
128
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);
132         }*/
133
134         return aic3262->write_dev(aic3262, reg, bytes, src);
135 }
136
137 /**
138  * aic3262_reg_write: Write a single TLV320AIC3262 register.
139  *
140  * @aic3262: Device to write to.
141  * @reg: Register to write to.
142  * @val: Value to write.
143  */
144 int aic3262_reg_write(struct aic3262 *aic3262, unsigned int reg,
145                      unsigned char val)
146 {
147         int ret;
148
149 //      val = cpu_to_be16(val);
150
151         mutex_lock(&aic3262->io_lock);
152
153         ret = aic3262_write(aic3262, reg, 1, &val);
154
155         mutex_unlock(&aic3262->io_lock);
156
157         return ret;
158 }
159 EXPORT_SYMBOL_GPL(aic3262_reg_write);
160
161 /**
162  * aic3262_bulk_write: Write multiple TLV320AIC3262 registers
163  *
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.
168  */
169 int aic3262_bulk_write(struct aic3262 *aic3262, unsigned int reg,
170                       int count, const u8 *buf)
171 {
172         int ret;
173
174         mutex_lock(&aic3262->io_lock);
175
176         ret = aic3262_write(aic3262, reg, count, buf);
177
178         mutex_unlock(&aic3262->io_lock);
179
180         return ret;
181 }
182 EXPORT_SYMBOL_GPL(aic3262_bulk_write);
183
184 /**
185  * aic3262_set_bits: Set the value of a bitfield in a TLV320AIC3262 register
186  *
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)
191  */
192 int aic3262_set_bits(struct aic3262 *aic3262, unsigned int reg,
193                     unsigned char mask, unsigned char val)
194 {
195         int ret;
196         u8 r;
197
198         mutex_lock(&aic3262->io_lock);
199
200         ret = aic3262_read(aic3262, reg, 1, &r);
201         if (ret < 0)
202                 goto out;
203
204
205         r &= ~mask;
206         r |= (val & mask);
207
208         ret = aic3262_write(aic3262, reg, 1, &r);
209
210 out:
211         mutex_unlock(&aic3262->io_lock);
212
213         return ret;
214 }
215 EXPORT_SYMBOL_GPL(aic3262_set_bits);
216 /* to be changed -- Mukund*/ 
217 static struct resource aic3262_codec_resources[] = {
218         {
219                 .start = AIC3262_IRQ_HEADSET_DETECT,
220                 .end   = AIC3262_IRQ_SPEAKER_OVER_TEMP,
221                 .flags = IORESOURCE_IRQ,
222         },
223 };
224
225 static struct resource aic3262_gpio_resources[] = {
226         {
227                 .start = AIC3262_GPIO1,
228                 .end   = AIC3262_GPO1,
229                 .flags = IORESOURCE_IRQ,
230         },
231 };
232
233 static struct mfd_cell aic3262_devs[] = {
234         {
235                 .name = "tlv320aic3262-codec",
236                 .num_resources = ARRAY_SIZE(aic3262_codec_resources),
237                 .resources = aic3262_codec_resources,
238         },
239
240         {
241                 .name = "tlv320aic3262-gpio",
242                 .num_resources = ARRAY_SIZE(aic3262_gpio_resources),
243                 .resources = aic3262_gpio_resources,
244                 .pm_runtime_no_callbacks = true,
245         },
246 };
247
248
249 #ifdef CONFIG_PM
250 static int aic3262_suspend(struct device *dev)
251 {
252         struct aic3262 *aic3262 = dev_get_drvdata(dev);
253         
254
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);
258 /*      if (ret < 0) {
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");
262                 return 0;
263         }
264 */
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
267          * driver for them.
268          */
269 //      ret = aic3262_read(aic3262, TLV320AIC3262_GPIO_1, TLV320AIC3262_NUM_GPIO_REGS * 2,
270 //                        &aic3262->gpio_regs);
271 /*      if (ret < 0)
272                 dev_err(dev, "Failed to save GPIO registers: %d\n", ret);*/
273
274
275         /* Explicitly put the device into reset in case regulators
276          * don't get disabled in order to ensure consistent restart.
277          */
278 //      aic3262_reg_write(aic3262, TLV320AIC3262_SOFTWARE_RESET, 0x8994);
279
280         aic3262->suspended = true;
281
282         return 0;
283 }
284
285 static int aic3262_resume(struct device *dev)
286 {
287         struct aic3262 *aic3262 = dev_get_drvdata(dev);
288
289
290         /* We may have lied to the PM core about suspending */
291 /*      if (!aic3262->suspended)
292                 return 0;
293
294         ret = aic3262_write(aic3262, TLV320AIC3262_INTERRUPT_STATUS_1_MASK,
295                            TLV320AIC3262_NUM_IRQ_REGS * 2, &aic3262->irq_masks_cur);
296         if (ret < 0)
297                 dev_err(dev, "Failed to restore interrupt masks: %d\n", ret);
298
299
300         ret = aic3262_write(aic3262, TLV320AIC3262_GPIO_1, TLV320AIC3262_NUM_GPIO_REGS * 2,
301                            &aic3262->gpio_regs);
302         if (ret < 0)
303                 dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
304 */
305         aic3262->suspended = false;
306
307         return 0;
308 }
309 #endif
310
311
312 /*
313  * Instantiate the generic non-control parts of the device.
314  */
315 static int aic3262_device_init(struct aic3262 *aic3262, int irq)
316 {
317         struct aic3262_pdata *pdata = aic3262->dev->platform_data;
318         const char *devname;
319         int ret, i;
320         u8 revID, pgID;
321         unsigned int naudint = 0;
322         u8 resetVal = 1;
323         //printk("aic3262_device_init beginning\n");
324
325         mutex_init(&aic3262->io_lock);
326         dev_set_drvdata(aic3262->dev, aic3262);
327         if(pdata){
328                 if(pdata->gpio_reset){
329                         ret = gpio_request(pdata->gpio_reset,"aic3262-reset-pin");
330                         if(ret != 0){
331                                 dev_err(aic3262->dev,"not able to acquire gpio %d for reseting the AIC3262 \n", pdata->gpio_reset);
332                                 goto err_return;
333                         }
334                         rk30_mux_api_set(GPIO0B7_I2S8CHSDO3_NAME, GPIO0B_GPIO0B7);  
335                         gpio_direction_output(pdata->gpio_reset, 1);
336                         msleep(5);
337                         gpio_direction_output(pdata->gpio_reset, 0);
338                         msleep(5);
339                         gpio_direction_output(pdata->gpio_reset, 1);
340 //                      gpio_set_value(pdata->gpio_reset, 0);
341                         msleep(5);      
342                 }       
343         }
344
345
346         /* run the codec through software reset */      
347         ret = aic3262_reg_write(aic3262, AIC3262_RESET_REG, resetVal);
348         if (ret < 0) {
349                 dev_err(aic3262->dev, "Could not write to AIC3262 register\n");
350                 goto err_return;
351         }
352                 
353         msleep(10);
354                 
355
356         ret = aic3262_reg_read(aic3262, AIC3262_REV_PG_ID);
357         if (ret < 0) {
358                 dev_err(aic3262->dev, "Failed to read ID register\n");
359                 goto err_return;
360         }
361         revID = (ret & AIC3262_REV_MASK) >> AIC3262_REV_SHIFT;
362         pgID = (ret & AIC3262_PG_MASK) >> AIC3262_PG_SHIFT;
363         switch (revID ) {
364         case 3:
365                 devname = "TLV320AIC3262";
366                 if (aic3262->type != TLV320AIC3262)
367                         dev_warn(aic3262->dev, "Device registered as type %d\n",
368                                  aic3262->type);
369                 aic3262->type = TLV320AIC3262;
370                 break;
371         default:
372                 dev_err(aic3262->dev, "Device is not a TLV320AIC3262, ID is %x\n",
373                         ret);
374                 ret = -EINVAL;
375                 goto err_return;
376         
377         }
378
379         dev_info(aic3262->dev, "%s revision %c\n", devname, 'D' + ret);
380
381         printk("aic3262_device_init %s revision %c\n", devname, 'D' + ret);
382
383         if (pdata) {
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);
388                 }
389                 else {
390                         naudint = pdata->naudint_irq;
391                 }
392                 aic3262->irq = naudint;
393                 aic3262->irq_base = pdata->irq_base;
394                 for(i = 0; i < AIC3262_NUM_GPIO;i++)
395                 {       
396                         if(pdata->gpio[i].used)
397                         {
398                                 if(pdata->gpio[i].in) // direction is input
399                                 {       
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);   
407                                 }
408                                 else    // direction is output
409                                 {
410                                         
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);
413                                 }
414                         }
415                         else // Disable the gpio/gpi/gpo
416                                 aic3262_set_bits(aic3262, aic3262_gpio_control[i].reg, aic3262_gpio_control[i].mask, 0x0);
417                 }       
418                 
419
420         }
421
422
423         if(naudint) {
424                 /* codec interrupt */
425                 ret = aic3262_irq_init(aic3262);
426                 if(ret)
427                         goto err_irq;
428         }
429
430         ret = mfd_add_devices(aic3262->dev, -1,
431                               aic3262_devs, ARRAY_SIZE(aic3262_devs),
432                               NULL, 0);
433         if (ret != 0) {
434                 dev_err(aic3262->dev, "Failed to add children: %d\n", ret);
435                 goto err_irq;
436         }
437
438         printk("aic3262_device_init added mfd devices \n");
439         pm_runtime_enable(aic3262->dev);
440         pm_runtime_resume(aic3262->dev);
441
442         return 0;
443
444 err_irq:
445         aic3262_irq_exit(aic3262);
446 //err:
447 //      mfd_remove_devices(aic3262->dev);
448 err_return:
449         kfree(aic3262);
450         return ret;
451 }
452
453 static void aic3262_device_exit(struct aic3262 *aic3262)
454 {
455         pm_runtime_disable(aic3262->dev);
456         mfd_remove_devices(aic3262->dev);
457         aic3262_irq_exit(aic3262);
458         kfree(aic3262);
459 }
460
461 static int aic3262_i2c_read_device(struct aic3262 *aic3262, unsigned int reg,
462                                   int bytes, void *dest)
463 {
464         struct i2c_client *i2c = aic3262->control_data;
465         aic326x_reg_union *aic_reg = (aic326x_reg_union *) &reg;        
466         char *value;
467         int ret;
468         u8 buf[2];
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.
474         {
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
478                 buf[0] = 0x0;
479                 buf[1] = 0x0;
480                 ret = i2c_master_send(i2c, (unsigned char *)buf, 2);
481                 if (ret < 0)
482                         return ret;
483                 if (ret != 2)
484                         return -EIO;
485                 buf[0] = 127;
486                 buf[1] = book;
487                 ret = i2c_master_send(i2c, (unsigned char *)buf, 2);
488                 if (ret < 0)
489                         return ret;
490                 if (ret != 2)
491                         return -EIO;
492                 aic3262->book_no = book;
493                 aic3262->page_no = 0x0;// To force a page change in the following if
494         }
495
496         if (aic3262->page_no != page) {
497                 buf[0] = 0x0;
498                 buf[1] = page;
499                 ret = i2c_master_send(i2c, (unsigned char *) buf, 2);
500
501                 if (ret < 0)
502                         return ret;
503                 if (ret != 2)
504                         return -EIO;
505                 aic3262->page_no = page;
506         }
507         // Send the required offset 
508         buf[0] = offset ;
509         ret = i2c_master_send(i2c, (unsigned char *)buf, 1);
510         if (ret < 0)
511                 return ret;
512         if (ret != 1)
513                 return -EIO;
514
515         ret = i2c_master_recv(i2c, dest, bytes);
516         value = dest; 
517         if (ret < 0)
518                 return ret;
519         if (ret != bytes)
520                 return -EIO;
521         return ret;
522 }
523
524 static int aic3262_i2c_write_device(struct aic3262 *aic3262, unsigned int reg,
525                                    int bytes, const void *src)
526 {
527         struct i2c_client *i2c = aic3262->control_data;
528         int ret;
529         
530         //char *value;
531         aic326x_reg_union *aic_reg = (aic326x_reg_union *) &reg;        
532         
533         //struct i2c_msg xfer[2];
534         u8 buf[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.
541         {
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
545                 buf[0] = 0x0;
546                 buf[1] = 0x0;
547                 ret = i2c_master_send(i2c, (unsigned char *)buf, 2);
548                 
549                 if (ret < 0)
550                         return ret;
551                 if (ret != 2)
552                         return -EIO;
553                 buf[0] = 127;
554                 buf[1] = book;
555                 ret = i2c_master_send(i2c, (unsigned char *)buf, 2);
556                 if (ret < 0)
557                         return ret;
558                 if (ret != 2)
559                         return -EIO;
560                 aic3262->book_no = book;
561                 aic3262->page_no = 0x0;// To force a page change in the following if
562         }
563
564         if (aic3262->page_no != page) {
565                 buf[0] = 0x0;
566                 buf[1] = page;
567                 ret = i2c_master_send(i2c, (unsigned char *) buf, 2);
568                 if (ret < 0)
569                         return ret;
570                 if (ret != 2)
571                         return -EIO;
572                 aic3262->page_no = page;
573         }
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;
580         xfer[0].len = 1;
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;
585         xfer[1].len = bytes;
586         xfer[1].buf = (char *)src;
587
588         ret = i2c_transfer(i2c->adapter, xfer, 2);*/
589 /*      buf[0] = offset;
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;
595         buf[0] = offset;
596         buf[1] = *value; 
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);
601         if (ret < 0)
602                 return ret;
603         if (ret != (bytes + 1) )
604                 return -EIO;
605
606         return 0;
607 }
608
609 static int aic3262_i2c_probe(struct i2c_client *i2c,
610                             const struct i2c_device_id *id)
611 {
612         struct aic3262 *aic3262;
613
614         printk("%s: entered \n", __FUNCTION__);
615         aic3262 = kzalloc(sizeof(struct aic3262), GFP_KERNEL);
616         if (aic3262 == NULL)
617                 return -ENOMEM;
618
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;
629
630         return aic3262_device_init(aic3262, i2c->irq);
631 }
632
633 static int aic3262_i2c_remove(struct i2c_client *i2c)
634 {
635         struct aic3262 *aic3262 = i2c_get_clientdata(i2c);
636
637         aic3262_device_exit(aic3262);
638
639         return 0;
640 }
641
642 static const struct i2c_device_id aic3262_i2c_id[] = {
643         { "tlv320aic3262", TLV320AIC3262 },
644         { }
645 };
646 MODULE_DEVICE_TABLE(i2c, aic3262_i2c_id);
647
648 static UNIVERSAL_DEV_PM_OPS(aic3262_pm_ops, aic3262_suspend, aic3262_resume,
649                             NULL);
650
651 static struct i2c_driver aic3262_i2c_driver = {
652         .driver = {
653                 .name = "tlv320aic3262",
654                 .owner = THIS_MODULE,
655                 .pm = &aic3262_pm_ops,
656         },
657         .probe = aic3262_i2c_probe,
658         .remove = aic3262_i2c_remove,
659         .id_table = aic3262_i2c_id,
660 };
661
662 static int __init aic3262_i2c_init(void)
663 {
664         int ret;
665         printk("aic3262_mfd i2c_init \n");      
666         ret = i2c_add_driver(&aic3262_i2c_driver);
667         if (ret != 0)
668                 pr_err("Failed to register aic3262 I2C driver: %d\n", ret);
669
670         return ret;
671 }
672 module_init(aic3262_i2c_init);
673
674 static void __exit aic3262_i2c_exit(void)
675 {
676         i2c_del_driver(&aic3262_i2c_driver);
677 }
678 module_exit(aic3262_i2c_exit);
679
680 MODULE_DESCRIPTION("Core support for the TLV320AIC3262 audio CODEC");
681 MODULE_LICENSE("GPL");
682 MODULE_AUTHOR("Mukund Navada <navada@ti.comm>");