blob: 59a42834951e84f8b41ce662bf4518ee763220ad [file] [log] [blame]
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/mfd/core.h>
17#include <linux/mfd/wcd9xxx/wcd9xxx-slimslave.h>
18#include <linux/mfd/wcd9xxx/core.h>
19#include <linux/mfd/wcd9xxx/pdata.h>
20#include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
21
22#include <linux/delay.h>
23#include <linux/gpio.h>
24#include <linux/debugfs.h>
25#include <linux/regulator/consumer.h>
26#include <linux/i2c.h>
27#include <sound/soc.h>
28
29#define WCD9XXX_SLIM_GLA_MAX_RETRIES 5
30#define WCD9XXX_REGISTER_START_OFFSET 0x800
31#define WCD9XXX_SLIM_RW_MAX_TRIES 3
32
33#define MAX_WCD9XXX_DEVICE 4
34#define WCD9XXX_I2C_MODE 0x03
35
36struct wcd9xxx_i2c {
37 struct i2c_client *client;
38 struct i2c_msg xfer_msg[2];
39 struct mutex xfer_lock;
40 int mod_id;
41};
42
43struct wcd9xxx_i2c wcd9xxx_modules[MAX_WCD9XXX_DEVICE];
44static int wcd9xxx_intf;
45
46static int wcd9xxx_read(struct wcd9xxx *wcd9xxx, unsigned short reg,
47 int bytes, void *dest, bool interface_reg)
48{
49 int ret;
50 u8 *buf = dest;
51
52 if (bytes <= 0) {
53 dev_err(wcd9xxx->dev, "Invalid byte read length %d\n", bytes);
54 return -EINVAL;
55 }
56
57 ret = wcd9xxx->read_dev(wcd9xxx, reg, bytes, dest, interface_reg);
58 if (ret < 0) {
59 dev_err(wcd9xxx->dev, "Codec read failed\n");
60 return ret;
61 } else
62 dev_dbg(wcd9xxx->dev, "Read 0x%02x from R%d(0x%x)\n",
63 *buf, reg, reg);
64
65 return 0;
66}
67int wcd9xxx_reg_read(struct wcd9xxx *wcd9xxx, unsigned short reg)
68{
69 u8 val;
70 int ret;
71
72 mutex_lock(&wcd9xxx->io_lock);
73 ret = wcd9xxx_read(wcd9xxx, reg, 1, &val, false);
74 mutex_unlock(&wcd9xxx->io_lock);
75
76 if (ret < 0)
77 return ret;
78 else
79 return val;
80}
81EXPORT_SYMBOL_GPL(wcd9xxx_reg_read);
82
83static int wcd9xxx_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
84 int bytes, void *src, bool interface_reg)
85{
86 u8 *buf = src;
87
88 if (bytes <= 0) {
89 pr_err("%s: Error, invalid write length\n", __func__);
90 return -EINVAL;
91 }
92
93 dev_dbg(wcd9xxx->dev, "Write %02x to R%d(0x%x)\n",
94 *buf, reg, reg);
95
96 return wcd9xxx->write_dev(wcd9xxx, reg, bytes, src, interface_reg);
97}
98
99int wcd9xxx_reg_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
100 u8 val)
101{
102 int ret;
103
104 mutex_lock(&wcd9xxx->io_lock);
105 ret = wcd9xxx_write(wcd9xxx, reg, 1, &val, false);
106 mutex_unlock(&wcd9xxx->io_lock);
107
108 return ret;
109}
110EXPORT_SYMBOL_GPL(wcd9xxx_reg_write);
111
112static u8 wcd9xxx_pgd_la;
113static u8 wcd9xxx_inf_la;
114
115int wcd9xxx_interface_reg_read(struct wcd9xxx *wcd9xxx, unsigned short reg)
116{
117 u8 val;
118 int ret;
119
120 mutex_lock(&wcd9xxx->io_lock);
121 ret = wcd9xxx_read(wcd9xxx, reg, 1, &val, true);
122 mutex_unlock(&wcd9xxx->io_lock);
123
124 if (ret < 0)
125 return ret;
126 else
127 return val;
128}
129EXPORT_SYMBOL_GPL(wcd9xxx_interface_reg_read);
130
131int wcd9xxx_interface_reg_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
132 u8 val)
133{
134 int ret;
135
136 mutex_lock(&wcd9xxx->io_lock);
137 ret = wcd9xxx_write(wcd9xxx, reg, 1, &val, true);
138 mutex_unlock(&wcd9xxx->io_lock);
139
140 return ret;
141}
142EXPORT_SYMBOL_GPL(wcd9xxx_interface_reg_write);
143
144int wcd9xxx_bulk_read(struct wcd9xxx *wcd9xxx, unsigned short reg,
145 int count, u8 *buf)
146{
147 int ret;
148
149 mutex_lock(&wcd9xxx->io_lock);
150
151 ret = wcd9xxx_read(wcd9xxx, reg, count, buf, false);
152
153 mutex_unlock(&wcd9xxx->io_lock);
154
155 return ret;
156}
157EXPORT_SYMBOL_GPL(wcd9xxx_bulk_read);
158
159int wcd9xxx_bulk_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
160 int count, u8 *buf)
161{
162 int ret;
163
164 mutex_lock(&wcd9xxx->io_lock);
165
166 ret = wcd9xxx_write(wcd9xxx, reg, count, buf, false);
167
168 mutex_unlock(&wcd9xxx->io_lock);
169
170 return ret;
171}
172EXPORT_SYMBOL_GPL(wcd9xxx_bulk_write);
173
174static int wcd9xxx_slim_read_device(struct wcd9xxx *wcd9xxx, unsigned short reg,
175 int bytes, void *dest, bool interface)
176{
177 int ret;
178 struct slim_ele_access msg;
179 int slim_read_tries = WCD9XXX_SLIM_RW_MAX_TRIES;
180 msg.start_offset = WCD9XXX_REGISTER_START_OFFSET + reg;
181 msg.num_bytes = bytes;
182 msg.comp = NULL;
183
184 while (1) {
185 mutex_lock(&wcd9xxx->xfer_lock);
186 ret = slim_request_val_element(interface ?
187 wcd9xxx->slim_slave : wcd9xxx->slim,
188 &msg, dest, bytes);
189 mutex_unlock(&wcd9xxx->xfer_lock);
190 if (likely(ret == 0) || (--slim_read_tries == 0))
191 break;
192 usleep_range(5000, 5000);
193 }
194
195 if (ret)
196 pr_err("%s: Error, Codec read failed (%d)\n", __func__, ret);
197
198 return ret;
199}
200/* Interface specifies whether the write is to the interface or general
201 * registers.
202 */
203static int wcd9xxx_slim_write_device(struct wcd9xxx *wcd9xxx,
204 unsigned short reg, int bytes, void *src, bool interface)
205{
206 int ret;
207 struct slim_ele_access msg;
208 int slim_write_tries = WCD9XXX_SLIM_RW_MAX_TRIES;
209 msg.start_offset = WCD9XXX_REGISTER_START_OFFSET + reg;
210 msg.num_bytes = bytes;
211 msg.comp = NULL;
212
213 while (1) {
214 mutex_lock(&wcd9xxx->xfer_lock);
215 ret = slim_change_val_element(interface ?
216 wcd9xxx->slim_slave : wcd9xxx->slim,
217 &msg, src, bytes);
218 mutex_unlock(&wcd9xxx->xfer_lock);
219 if (likely(ret == 0) || (--slim_write_tries == 0))
220 break;
221 usleep_range(5000, 5000);
222 }
223
224 if (ret)
225 pr_err("%s: Error, Codec write failed (%d)\n", __func__, ret);
226
227 return ret;
228}
229
230static struct mfd_cell tabla1x_devs[] = {
231 {
232 .name = "tabla1x_codec",
233 },
234};
235
236static struct mfd_cell tabla_devs[] = {
237 {
238 .name = "tabla_codec",
239 },
240};
241
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530242static struct mfd_cell sitar_devs[] = {
243 {
244 .name = "sitar_codec",
245 },
246};
247
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530248static void wcd9xxx_bring_up(struct wcd9xxx *wcd9xxx)
249{
250 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x4);
251 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_CDC_CTL, 0);
252 usleep_range(5000, 5000);
253 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_CDC_CTL, 3);
254 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 3);
255}
256
257static void wcd9xxx_bring_down(struct wcd9xxx *wcd9xxx)
258{
259 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x7);
260 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x6);
261 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0xe);
262 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x8);
263}
264
265static int wcd9xxx_reset(struct wcd9xxx *wcd9xxx)
266{
267 int ret;
268
269 if (wcd9xxx->reset_gpio) {
270 ret = gpio_request(wcd9xxx->reset_gpio, "CDC_RESET");
271 if (ret) {
272 pr_err("%s: Failed to request gpio %d\n", __func__,
273 wcd9xxx->reset_gpio);
274 wcd9xxx->reset_gpio = 0;
275 return ret;
276 }
277
278 gpio_direction_output(wcd9xxx->reset_gpio, 1);
279 msleep(20);
280 gpio_direction_output(wcd9xxx->reset_gpio, 0);
281 msleep(20);
282 gpio_direction_output(wcd9xxx->reset_gpio, 1);
283 msleep(20);
284 }
285 return 0;
286}
287
288static void wcd9xxx_free_reset(struct wcd9xxx *wcd9xxx)
289{
290 if (wcd9xxx->reset_gpio) {
291 gpio_free(wcd9xxx->reset_gpio);
292 wcd9xxx->reset_gpio = 0;
293 }
294}
295
296static int wcd9xxx_device_init(struct wcd9xxx *wcd9xxx, int irq)
297{
298 int ret;
299 u8 idbyte_0, idbyte_1, idbyte_2, idbyte_3;
300 struct mfd_cell *wcd9xxx_dev = NULL;
301 int wcd9xxx_dev_size = 0;
302
303 mutex_init(&wcd9xxx->io_lock);
304 mutex_init(&wcd9xxx->xfer_lock);
305
306 mutex_init(&wcd9xxx->pm_lock);
307 wcd9xxx->wlock_holders = 0;
308 wcd9xxx->pm_state = WCD9XXX_PM_SLEEPABLE;
309 init_waitqueue_head(&wcd9xxx->pm_wq);
310 wake_lock_init(&wcd9xxx->wlock, WAKE_LOCK_IDLE, "wcd9310-irq");
311
312 dev_set_drvdata(wcd9xxx->dev, wcd9xxx);
313
314 wcd9xxx_bring_up(wcd9xxx);
315
316 ret = wcd9xxx_irq_init(wcd9xxx);
317 if (ret) {
318 pr_err("IRQ initialization failed\n");
319 goto err;
320 }
321
322 idbyte_0 = wcd9xxx_reg_read(wcd9xxx, WCD9XXX_A_CHIP_ID_BYTE_0);
323 idbyte_1 = wcd9xxx_reg_read(wcd9xxx, WCD9XXX_A_CHIP_ID_BYTE_1);
324 idbyte_2 = wcd9xxx_reg_read(wcd9xxx, WCD9XXX_A_CHIP_ID_BYTE_2);
325 idbyte_3 = wcd9xxx_reg_read(wcd9xxx, WCD9XXX_A_CHIP_ID_BYTE_3);
326
327 wcd9xxx->version = wcd9xxx_reg_read(wcd9xxx,
328 WCD9XXX_A_CHIP_VERSION) & 0x1F;
329 pr_info("%s : Codec version %u initialized\n",
330 __func__, wcd9xxx->version);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530331 pr_info("idbyte_0[%08x] idbyte_1[%08x] idbyte_2[%08x] idbyte_3[%08x]\n",
332 idbyte_0, idbyte_1, idbyte_2, idbyte_3);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530333
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530334 if (!strncmp(wcd9xxx->slim->name, "tabla", 5)) {
335 if (TABLA_IS_1_X(wcd9xxx->version)) {
336 wcd9xxx_dev = tabla1x_devs;
337 wcd9xxx_dev_size = ARRAY_SIZE(tabla1x_devs);
338 } else {
339 wcd9xxx_dev = tabla_devs;
340 wcd9xxx_dev_size = ARRAY_SIZE(tabla_devs);
341 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530342 } else {
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530343 wcd9xxx_dev = sitar_devs;
344 wcd9xxx_dev_size = ARRAY_SIZE(sitar_devs);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530345 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530346
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530347 ret = mfd_add_devices(wcd9xxx->dev, -1,
348 wcd9xxx_dev, wcd9xxx_dev_size,
349 NULL, 0);
350 if (ret != 0) {
351 dev_err(wcd9xxx->dev, "Failed to add children: %d\n", ret);
352 goto err_irq;
353 }
354 return ret;
355err_irq:
356 wcd9xxx_irq_exit(wcd9xxx);
357err:
358 wcd9xxx_bring_down(wcd9xxx);
359 wake_lock_destroy(&wcd9xxx->wlock);
360 mutex_destroy(&wcd9xxx->pm_lock);
361 mutex_destroy(&wcd9xxx->io_lock);
362 mutex_destroy(&wcd9xxx->xfer_lock);
363 return ret;
364}
365
366static void wcd9xxx_device_exit(struct wcd9xxx *wcd9xxx)
367{
368 wcd9xxx_irq_exit(wcd9xxx);
369 wcd9xxx_bring_down(wcd9xxx);
370 wcd9xxx_free_reset(wcd9xxx);
371 mutex_destroy(&wcd9xxx->pm_lock);
372 wake_lock_destroy(&wcd9xxx->wlock);
373 mutex_destroy(&wcd9xxx->io_lock);
374 mutex_destroy(&wcd9xxx->xfer_lock);
375 slim_remove_device(wcd9xxx->slim_slave);
376 kfree(wcd9xxx);
377}
378
379
380#ifdef CONFIG_DEBUG_FS
381struct wcd9xxx *debugCodec;
382
383static struct dentry *debugfs_wcd9xxx_dent;
384static struct dentry *debugfs_peek;
385static struct dentry *debugfs_poke;
386
387static unsigned char read_data;
388
389static int codec_debug_open(struct inode *inode, struct file *file)
390{
391 file->private_data = inode->i_private;
392 return 0;
393}
394
395static int get_parameters(char *buf, long int *param1, int num_of_par)
396{
397 char *token;
398 int base, cnt;
399
400 token = strsep(&buf, " ");
401
402 for (cnt = 0; cnt < num_of_par; cnt++) {
403 if (token != NULL) {
404 if ((token[1] == 'x') || (token[1] == 'X'))
405 base = 16;
406 else
407 base = 10;
408
409 if (strict_strtoul(token, base, &param1[cnt]) != 0)
410 return -EINVAL;
411
412 token = strsep(&buf, " ");
413 } else
414 return -EINVAL;
415 }
416 return 0;
417}
418
419static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
420 size_t count, loff_t *ppos)
421{
422 char lbuf[8];
423
424 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
425 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
426 strnlen(lbuf, 7));
427}
428
429
430static ssize_t codec_debug_write(struct file *filp,
431 const char __user *ubuf, size_t cnt, loff_t *ppos)
432{
433 char *access_str = filp->private_data;
434 char lbuf[32];
435 int rc;
436 long int param[5];
437
438 if (cnt > sizeof(lbuf) - 1)
439 return -EINVAL;
440
441 rc = copy_from_user(lbuf, ubuf, cnt);
442 if (rc)
443 return -EFAULT;
444
445 lbuf[cnt] = '\0';
446
447 if (!strncmp(access_str, "poke", 6)) {
448 /* write */
449 rc = get_parameters(lbuf, param, 2);
450 if ((param[0] <= 0x3FF) && (param[1] <= 0xFF) &&
451 (rc == 0))
452 wcd9xxx_interface_reg_write(debugCodec, param[0],
453 param[1]);
454 else
455 rc = -EINVAL;
456 } else if (!strncmp(access_str, "peek", 6)) {
457 /* read */
458 rc = get_parameters(lbuf, param, 1);
459 if ((param[0] <= 0x3FF) && (rc == 0))
460 read_data = wcd9xxx_interface_reg_read(debugCodec,
461 param[0]);
462 else
463 rc = -EINVAL;
464 }
465
466 if (rc == 0)
467 rc = cnt;
468 else
469 pr_err("%s: rc = %d\n", __func__, rc);
470
471 return rc;
472}
473
474static const struct file_operations codec_debug_ops = {
475 .open = codec_debug_open,
476 .write = codec_debug_write,
477 .read = codec_debug_read
478};
479#endif
480
481static int wcd9xxx_enable_supplies(struct wcd9xxx *wcd9xxx)
482{
483 int ret;
484 int i;
485 struct wcd9xxx_pdata *pdata = wcd9xxx->slim->dev.platform_data;
486
487 wcd9xxx->supplies = kzalloc(sizeof(struct regulator_bulk_data) *
488 ARRAY_SIZE(pdata->regulator),
489 GFP_KERNEL);
490 if (!wcd9xxx->supplies) {
491 ret = -ENOMEM;
492 goto err;
493 }
494
495 for (i = 0; i < ARRAY_SIZE(pdata->regulator); i++)
496 wcd9xxx->supplies[i].supply = pdata->regulator[i].name;
497
498 ret = regulator_bulk_get(wcd9xxx->dev, ARRAY_SIZE(pdata->regulator),
499 wcd9xxx->supplies);
500 if (ret != 0) {
501 dev_err(wcd9xxx->dev, "Failed to get supplies: err = %d\n",
502 ret);
503 goto err_supplies;
504 }
505
506 for (i = 0; i < ARRAY_SIZE(pdata->regulator); i++) {
507 ret = regulator_set_voltage(wcd9xxx->supplies[i].consumer,
508 pdata->regulator[i].min_uV, pdata->regulator[i].max_uV);
509 if (ret) {
510 pr_err("%s: Setting regulator voltage failed for "
511 "regulator %s err = %d\n", __func__,
512 wcd9xxx->supplies[i].supply, ret);
513 goto err_get;
514 }
515
516 ret = regulator_set_optimum_mode(wcd9xxx->supplies[i].consumer,
517 pdata->regulator[i].optimum_uA);
518 if (ret < 0) {
519 pr_err("%s: Setting regulator optimum mode failed for "
520 "regulator %s err = %d\n", __func__,
521 wcd9xxx->supplies[i].supply, ret);
522 goto err_get;
523 }
524 }
525
526 ret = regulator_bulk_enable(ARRAY_SIZE(pdata->regulator),
527 wcd9xxx->supplies);
528 if (ret != 0) {
529 dev_err(wcd9xxx->dev, "Failed to enable supplies: err = %d\n",
530 ret);
531 goto err_configure;
532 }
533 return ret;
534
535err_configure:
536 for (i = 0; i < ARRAY_SIZE(pdata->regulator); i++) {
537 regulator_set_voltage(wcd9xxx->supplies[i].consumer, 0,
538 pdata->regulator[i].max_uV);
539 regulator_set_optimum_mode(wcd9xxx->supplies[i].consumer, 0);
540 }
541err_get:
542 regulator_bulk_free(ARRAY_SIZE(pdata->regulator), wcd9xxx->supplies);
543err_supplies:
544 kfree(wcd9xxx->supplies);
545err:
546 return ret;
547}
548
549static void wcd9xxx_disable_supplies(struct wcd9xxx *wcd9xxx)
550{
551 int i;
552 struct wcd9xxx_pdata *pdata = wcd9xxx->slim->dev.platform_data;
553
554 regulator_bulk_disable(ARRAY_SIZE(pdata->regulator),
555 wcd9xxx->supplies);
556 for (i = 0; i < ARRAY_SIZE(pdata->regulator); i++) {
557 regulator_set_voltage(wcd9xxx->supplies[i].consumer, 0,
558 pdata->regulator[i].max_uV);
559 regulator_set_optimum_mode(wcd9xxx->supplies[i].consumer, 0);
560 }
561 regulator_bulk_free(ARRAY_SIZE(pdata->regulator), wcd9xxx->supplies);
562 kfree(wcd9xxx->supplies);
563}
564
565int wcd9xxx_get_intf_type(void)
566{
567 return wcd9xxx_intf;
568}
569EXPORT_SYMBOL_GPL(wcd9xxx_get_intf_type);
570
571struct wcd9xxx_i2c *get_i2c_wcd9xxx_device_info(u16 reg)
572{
573 u16 mask = 0x0f00;
574 int value = 0;
575 struct wcd9xxx_i2c *wcd9xxx = NULL;
576 value = ((reg & mask) >> 8) & 0x000f;
577 switch (value) {
578 case 0:
579 wcd9xxx = &wcd9xxx_modules[0];
580 break;
581 case 1:
582 wcd9xxx = &wcd9xxx_modules[1];
583 break;
584 case 2:
585 wcd9xxx = &wcd9xxx_modules[2];
586 break;
587 case 3:
588 wcd9xxx = &wcd9xxx_modules[3];
589 break;
590 default:
591 break;
592 }
593 return wcd9xxx;
594}
595
596int wcd9xxx_i2c_write_device(u16 reg, u8 *value,
597 u32 bytes)
598{
599
600 struct i2c_msg *msg;
601 int ret = 0;
602 u8 reg_addr = 0;
603 u8 data[bytes + 1];
604 struct wcd9xxx_i2c *wcd9xxx;
605
606 wcd9xxx = get_i2c_wcd9xxx_device_info(reg);
607 if (wcd9xxx == NULL || wcd9xxx->client == NULL) {
608 pr_err("failed to get device info\n");
609 return -ENODEV;
610 }
611 reg_addr = (u8)reg;
612 msg = &wcd9xxx->xfer_msg[0];
613 msg->addr = wcd9xxx->client->addr;
614 msg->len = bytes + 1;
615 msg->flags = 0;
616 data[0] = reg;
617 data[1] = *value;
618 msg->buf = data;
619 ret = i2c_transfer(wcd9xxx->client->adapter, wcd9xxx->xfer_msg, 1);
620 /* Try again if the write fails */
621 if (ret != 1) {
622 ret = i2c_transfer(wcd9xxx->client->adapter,
623 wcd9xxx->xfer_msg, 1);
624 if (ret != 1) {
625 pr_err("failed to write the device\n");
626 return ret;
627 }
628 }
629 pr_debug("write sucess register = %x val = %x\n", reg, data[1]);
630 return 0;
631}
632
633
634int wcd9xxx_i2c_read_device(unsigned short reg,
635 int bytes, unsigned char *dest)
636{
637 struct i2c_msg *msg;
638 int ret = 0;
639 u8 reg_addr = 0;
640 struct wcd9xxx_i2c *wcd9xxx;
641 u8 i = 0;
642
643 wcd9xxx = get_i2c_wcd9xxx_device_info(reg);
644 if (wcd9xxx == NULL || wcd9xxx->client == NULL) {
645 pr_err("failed to get device info\n");
646 return -ENODEV;
647 }
648 for (i = 0; i < bytes; i++) {
649 reg_addr = (u8)reg++;
650 msg = &wcd9xxx->xfer_msg[0];
651 msg->addr = wcd9xxx->client->addr;
652 msg->len = 1;
653 msg->flags = 0;
654 msg->buf = &reg_addr;
655
656 msg = &wcd9xxx->xfer_msg[1];
657 msg->addr = wcd9xxx->client->addr;
658 msg->len = 1;
659 msg->flags = I2C_M_RD;
660 msg->buf = dest++;
661 ret = i2c_transfer(wcd9xxx->client->adapter,
662 wcd9xxx->xfer_msg, 2);
663
664 /* Try again if read fails first time */
665 if (ret != 2) {
666 ret = i2c_transfer(wcd9xxx->client->adapter,
667 wcd9xxx->xfer_msg, 2);
668 if (ret != 2) {
669 pr_err("failed to read wcd9xxx register\n");
670 return ret;
671 }
672 }
673 }
674 return 0;
675}
676
677int wcd9xxx_i2c_read(struct wcd9xxx *wcd9xxx, unsigned short reg,
678 int bytes, void *dest, bool interface_reg)
679{
680 return wcd9xxx_i2c_read_device(reg, bytes, dest);
681}
682
683int wcd9xxx_i2c_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
684 int bytes, void *src, bool interface_reg)
685{
686 return wcd9xxx_i2c_write_device(reg, src, bytes);
687}
688
689static int __devinit wcd9xxx_i2c_probe(struct i2c_client *client,
690 const struct i2c_device_id *id)
691{
692 struct wcd9xxx *wcd9xxx;
693 struct wcd9xxx_pdata *pdata = client->dev.platform_data;
694 int val = 0;
695 int ret = 0;
696 static int device_id;
697
698 if (device_id > 0) {
699 wcd9xxx_modules[device_id++].client = client;
700 pr_info("probe for other slaves devices of tabla\n");
701 return ret;
702 }
703
704 wcd9xxx = kzalloc(sizeof(struct wcd9xxx), GFP_KERNEL);
705 if (wcd9xxx == NULL) {
706 pr_err("%s: error, allocation failed\n", __func__);
707 ret = -ENOMEM;
708 goto fail;
709 }
710
711 if (!pdata) {
712 dev_dbg(&client->dev, "no platform data?\n");
713 ret = -EINVAL;
714 goto fail;
715 }
716 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
717 dev_dbg(&client->dev, "can't talk I2C?\n");
718 ret = -EIO;
719 goto fail;
720 }
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530721 dev_set_drvdata(&client->dev, wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530722 wcd9xxx->dev = &client->dev;
723 wcd9xxx->reset_gpio = pdata->reset_gpio;
724
725 ret = wcd9xxx_enable_supplies(wcd9xxx);
726 if (ret) {
727 pr_err("%s: Fail to enable Codec supplies\n", __func__);
728 goto err_codec;
729 }
730
731 usleep_range(5, 5);
732 ret = wcd9xxx_reset(wcd9xxx);
733 if (ret) {
734 pr_err("%s: Resetting Codec failed\n", __func__);
735 goto err_supplies;
736 }
737 wcd9xxx_modules[device_id++].client = client;
738
739 wcd9xxx->read_dev = wcd9xxx_i2c_read;
740 wcd9xxx->write_dev = wcd9xxx_i2c_write;
741 wcd9xxx->irq = pdata->irq;
742 wcd9xxx->irq_base = pdata->irq_base;
743
744 /*read the tabla status before initializing the device type*/
745 ret = wcd9xxx_read(wcd9xxx, WCD9XXX_A_CHIP_STATUS, 1, &val, 0);
746 if ((ret < 0) || (val != WCD9XXX_I2C_MODE)) {
747 pr_err("failed to read the wcd9xxx status\n");
748 goto err_device_init;
749 }
750
751 ret = wcd9xxx_device_init(wcd9xxx, wcd9xxx->irq);
752 if (ret) {
753 pr_err("%s: error, initializing device failed\n", __func__);
754 goto err_device_init;
755 }
756 wcd9xxx_intf = WCD9XXX_INTERFACE_TYPE_I2C;
757
758 return ret;
759err_device_init:
760 wcd9xxx_free_reset(wcd9xxx);
761err_supplies:
762 wcd9xxx_disable_supplies(wcd9xxx);
763err_codec:
764 kfree(wcd9xxx);
765fail:
766 return ret;
767}
768
769static int __devexit wcd9xxx_i2c_remove(struct i2c_client *client)
770{
771 struct wcd9xxx *wcd9xxx;
772
773 pr_debug("exit\n");
774 wcd9xxx = dev_get_drvdata(&client->dev);
775 wcd9xxx_disable_supplies(wcd9xxx);
776 wcd9xxx_device_exit(wcd9xxx);
777 return 0;
778}
779
780static int wcd9xxx_slim_probe(struct slim_device *slim)
781{
782 struct wcd9xxx *wcd9xxx;
783 struct wcd9xxx_pdata *pdata;
784 int ret = 0;
785 int sgla_retry_cnt;
786
787 dev_info(&slim->dev, "Initialized slim device %s\n", slim->name);
788 pdata = slim->dev.platform_data;
789
790 if (!pdata) {
791 dev_err(&slim->dev, "Error, no platform data\n");
792 ret = -EINVAL;
793 goto err;
794 }
795
796 wcd9xxx = kzalloc(sizeof(struct wcd9xxx), GFP_KERNEL);
797 if (wcd9xxx == NULL) {
798 pr_err("%s: error, allocation failed\n", __func__);
799 ret = -ENOMEM;
800 goto err;
801 }
802 if (!slim->ctrl) {
803 pr_err("Error, no SLIMBUS control data\n");
804 ret = -EINVAL;
805 goto err_codec;
806 }
807 wcd9xxx->slim = slim;
808 slim_set_clientdata(slim, wcd9xxx);
809 wcd9xxx->reset_gpio = pdata->reset_gpio;
810 wcd9xxx->dev = &slim->dev;
811
812 ret = wcd9xxx_enable_supplies(wcd9xxx);
813 if (ret)
814 goto err_codec;
815 usleep_range(5, 5);
816
817 ret = wcd9xxx_reset(wcd9xxx);
818 if (ret) {
819 pr_err("%s: Resetting Codec failed\n", __func__);
820 goto err_supplies;
821 }
822
823 ret = slim_get_logical_addr(wcd9xxx->slim, wcd9xxx->slim->e_addr,
824 ARRAY_SIZE(wcd9xxx->slim->e_addr), &wcd9xxx->slim->laddr);
825 if (ret) {
826 pr_err("fail to get slimbus logical address %d\n", ret);
827 goto err_reset;
828 }
829 wcd9xxx->read_dev = wcd9xxx_slim_read_device;
830 wcd9xxx->write_dev = wcd9xxx_slim_write_device;
831 wcd9xxx->irq = pdata->irq;
832 wcd9xxx->irq_base = pdata->irq_base;
833 wcd9xxx_pgd_la = wcd9xxx->slim->laddr;
834
835 if (pdata->num_irqs < TABLA_NUM_IRQS) {
836 pr_err("%s: Error, not enough interrupt lines allocated\n",
837 __func__);
838 goto err_reset;
839 }
840
841 wcd9xxx->slim_slave = &pdata->slimbus_slave_device;
842
843 ret = slim_add_device(slim->ctrl, wcd9xxx->slim_slave);
844 if (ret) {
845 pr_err("%s: error, adding SLIMBUS device failed\n", __func__);
846 goto err_reset;
847 }
848
849 sgla_retry_cnt = 0;
850
851 while (1) {
852 ret = slim_get_logical_addr(wcd9xxx->slim_slave,
853 wcd9xxx->slim_slave->e_addr,
854 ARRAY_SIZE(wcd9xxx->slim_slave->e_addr),
855 &wcd9xxx->slim_slave->laddr);
856 if (ret) {
857 if (sgla_retry_cnt++ < WCD9XXX_SLIM_GLA_MAX_RETRIES) {
858 /* Give SLIMBUS slave time to report present
859 and be ready.
860 */
861 usleep_range(1000, 1000);
862 pr_debug("%s: retry slim_get_logical_addr()\n",
863 __func__);
864 continue;
865 }
866 pr_err("fail to get slimbus slave logical address"
867 " %d\n", ret);
868 goto err_slim_add;
869 }
870 break;
871 }
872 wcd9xxx_inf_la = wcd9xxx->slim_slave->laddr;
873 wcd9xxx_intf = WCD9XXX_INTERFACE_TYPE_SLIMBUS;
874
875 ret = wcd9xxx_device_init(wcd9xxx, wcd9xxx->irq);
876 if (ret) {
877 pr_err("%s: error, initializing device failed\n", __func__);
878 goto err_slim_add;
879 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530880 wcd9xxx_init_slimslave(wcd9xxx, wcd9xxx_pgd_la);
881#ifdef CONFIG_DEBUG_FS
882 debugCodec = wcd9xxx;
883
884 debugfs_wcd9xxx_dent = debugfs_create_dir
885 ("wcd9310_slimbus_interface_device", 0);
886 if (!IS_ERR(debugfs_wcd9xxx_dent)) {
887 debugfs_peek = debugfs_create_file("peek",
888 S_IFREG | S_IRUGO, debugfs_wcd9xxx_dent,
889 (void *) "peek", &codec_debug_ops);
890
891 debugfs_poke = debugfs_create_file("poke",
892 S_IFREG | S_IRUGO, debugfs_wcd9xxx_dent,
893 (void *) "poke", &codec_debug_ops);
894 }
895#endif
896
897 return ret;
898
899err_slim_add:
900 slim_remove_device(wcd9xxx->slim_slave);
901err_reset:
902 wcd9xxx_free_reset(wcd9xxx);
903err_supplies:
904 wcd9xxx_disable_supplies(wcd9xxx);
905err_codec:
906 kfree(wcd9xxx);
907err:
908 return ret;
909}
910static int wcd9xxx_slim_remove(struct slim_device *pdev)
911{
912 struct wcd9xxx *wcd9xxx;
913
914#ifdef CONFIG_DEBUG_FS
915 debugfs_remove(debugfs_peek);
916 debugfs_remove(debugfs_poke);
917 debugfs_remove(debugfs_wcd9xxx_dent);
918#endif
919 wcd9xxx = slim_get_devicedata(pdev);
920 wcd9xxx_deinit_slimslave(wcd9xxx);
921 slim_remove_device(wcd9xxx->slim_slave);
922 wcd9xxx_disable_supplies(wcd9xxx);
923 wcd9xxx_device_exit(wcd9xxx);
924 return 0;
925}
926
927static int wcd9xxx_resume(struct wcd9xxx *wcd9xxx)
928{
929 int ret = 0;
930
931 pr_debug("%s: enter\n", __func__);
932 mutex_lock(&wcd9xxx->pm_lock);
933 if (wcd9xxx->pm_state == WCD9XXX_PM_ASLEEP) {
934 pr_debug("%s: resuming system, state %d, wlock %d\n", __func__,
935 wcd9xxx->pm_state, wcd9xxx->wlock_holders);
936 wcd9xxx->pm_state = WCD9XXX_PM_SLEEPABLE;
937 } else {
938 pr_warn("%s: system is already awake, state %d wlock %d\n",
939 __func__, wcd9xxx->pm_state, wcd9xxx->wlock_holders);
940 }
941 mutex_unlock(&wcd9xxx->pm_lock);
942 wake_up_all(&wcd9xxx->pm_wq);
943
944 return ret;
945}
946
947static int wcd9xxx_slim_resume(struct slim_device *sldev)
948{
949 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
950 return wcd9xxx_resume(wcd9xxx);
951}
952
953static int wcd9xxx_i2c_resume(struct i2c_client *i2cdev)
954{
955 struct wcd9xxx *wcd9xxx = dev_get_drvdata(&i2cdev->dev);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530956 if (wcd9xxx)
957 return wcd9xxx_resume(wcd9xxx);
958 else
959 return 0;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530960}
961
962static int wcd9xxx_suspend(struct wcd9xxx *wcd9xxx, pm_message_t pmesg)
963{
964 int ret = 0;
965
966 pr_debug("%s: enter\n", __func__);
967 /* wake_lock() can be called after this suspend chain call started.
968 * thus suspend can be called while wlock is being held */
969 mutex_lock(&wcd9xxx->pm_lock);
970 if (wcd9xxx->pm_state == WCD9XXX_PM_SLEEPABLE) {
971 pr_debug("%s: suspending system, state %d, wlock %d\n",
972 __func__, wcd9xxx->pm_state, wcd9xxx->wlock_holders);
973 wcd9xxx->pm_state = WCD9XXX_PM_ASLEEP;
974 } else if (wcd9xxx->pm_state == WCD9XXX_PM_AWAKE) {
975 /* unlock to wait for pm_state == WCD9XXX_PM_SLEEPABLE
976 * then set to WCD9XXX_PM_ASLEEP */
977 pr_debug("%s: waiting to suspend system, state %d, wlock %d\n",
978 __func__, wcd9xxx->pm_state, wcd9xxx->wlock_holders);
979 mutex_unlock(&wcd9xxx->pm_lock);
980 if (!(wait_event_timeout(wcd9xxx->pm_wq,
981 wcd9xxx_pm_cmpxchg(wcd9xxx,
982 WCD9XXX_PM_SLEEPABLE,
983 WCD9XXX_PM_ASLEEP) ==
984 WCD9XXX_PM_SLEEPABLE,
985 HZ))) {
986 pr_debug("%s: suspend failed state %d, wlock %d\n",
987 __func__, wcd9xxx->pm_state,
988 wcd9xxx->wlock_holders);
989 ret = -EBUSY;
990 } else {
991 pr_debug("%s: done, state %d, wlock %d\n", __func__,
992 wcd9xxx->pm_state, wcd9xxx->wlock_holders);
993 }
994 mutex_lock(&wcd9xxx->pm_lock);
995 } else if (wcd9xxx->pm_state == WCD9XXX_PM_ASLEEP) {
996 pr_warn("%s: system is already suspended, state %d, wlock %dn",
997 __func__, wcd9xxx->pm_state, wcd9xxx->wlock_holders);
998 }
999 mutex_unlock(&wcd9xxx->pm_lock);
1000
1001 return ret;
1002}
1003
1004static int wcd9xxx_slim_suspend(struct slim_device *sldev, pm_message_t pmesg)
1005{
1006 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1007 return wcd9xxx_suspend(wcd9xxx, pmesg);
1008}
1009
1010static int wcd9xxx_i2c_suspend(struct i2c_client *i2cdev, pm_message_t pmesg)
1011{
1012 struct wcd9xxx *wcd9xxx = dev_get_drvdata(&i2cdev->dev);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301013 if (wcd9xxx)
1014 return wcd9xxx_suspend(wcd9xxx, pmesg);
1015 else
1016 return 0;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301017}
1018
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301019static const struct slim_device_id sitar_slimtest_id[] = {
1020 {"sitar-slim", 0},
1021 {}
1022};
1023static struct slim_driver sitar_slim_driver = {
1024 .driver = {
1025 .name = "sitar-slim",
1026 .owner = THIS_MODULE,
1027 },
1028 .probe = wcd9xxx_slim_probe,
1029 .remove = wcd9xxx_slim_remove,
1030 .id_table = sitar_slimtest_id,
1031 .resume = wcd9xxx_slim_resume,
1032 .suspend = wcd9xxx_slim_suspend,
1033};
1034
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301035static const struct slim_device_id slimtest_id[] = {
1036 {"tabla-slim", 0},
1037 {}
1038};
1039
1040static struct slim_driver tabla_slim_driver = {
1041 .driver = {
1042 .name = "tabla-slim",
1043 .owner = THIS_MODULE,
1044 },
1045 .probe = wcd9xxx_slim_probe,
1046 .remove = wcd9xxx_slim_remove,
1047 .id_table = slimtest_id,
1048 .resume = wcd9xxx_slim_resume,
1049 .suspend = wcd9xxx_slim_suspend,
1050};
1051
1052static const struct slim_device_id slimtest2x_id[] = {
1053 {"tabla2x-slim", 0},
1054 {}
1055};
1056
1057static struct slim_driver tabla2x_slim_driver = {
1058 .driver = {
1059 .name = "tabla2x-slim",
1060 .owner = THIS_MODULE,
1061 },
1062 .probe = wcd9xxx_slim_probe,
1063 .remove = wcd9xxx_slim_remove,
1064 .id_table = slimtest2x_id,
1065 .resume = wcd9xxx_slim_resume,
1066 .suspend = wcd9xxx_slim_suspend,
1067};
1068
1069#define TABLA_I2C_TOP_LEVEL 0
1070#define TABLA_I2C_ANALOG 1
1071#define TABLA_I2C_DIGITAL_1 2
1072#define TABLA_I2C_DIGITAL_2 3
1073
1074static struct i2c_device_id tabla_id_table[] = {
1075 {"tabla top level", TABLA_I2C_TOP_LEVEL},
1076 {"tabla analog", TABLA_I2C_TOP_LEVEL},
1077 {"tabla digital1", TABLA_I2C_TOP_LEVEL},
1078 {"tabla digital2", TABLA_I2C_TOP_LEVEL},
1079 {}
1080};
1081MODULE_DEVICE_TABLE(i2c, tabla_id_table);
1082
1083static struct i2c_driver tabla_i2c_driver = {
1084 .driver = {
1085 .owner = THIS_MODULE,
1086 .name = "tabla-i2c-core",
1087 },
1088 .id_table = tabla_id_table,
1089 .probe = wcd9xxx_i2c_probe,
1090 .remove = __devexit_p(wcd9xxx_i2c_remove),
1091 .resume = wcd9xxx_i2c_resume,
1092 .suspend = wcd9xxx_i2c_suspend,
1093};
1094
1095static int __init wcd9xxx_init(void)
1096{
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301097 int ret1, ret2, ret3, ret4;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301098
1099 ret1 = slim_driver_register(&tabla_slim_driver);
1100 if (ret1 != 0)
1101 pr_err("Failed to register tabla SB driver: %d\n", ret1);
1102
1103 ret2 = slim_driver_register(&tabla2x_slim_driver);
1104 if (ret2 != 0)
1105 pr_err("Failed to register tabla2x SB driver: %d\n", ret2);
1106
1107 ret3 = i2c_add_driver(&tabla_i2c_driver);
1108 if (ret3 != 0)
1109 pr_err("failed to add the I2C driver\n");
1110
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301111 ret4 = slim_driver_register(&sitar_slim_driver);
1112 if (ret1 != 0)
1113 pr_err("Failed to register sitar SB driver: %d\n", ret4);
1114
1115 return (ret1 && ret2 && ret3 && ret4) ? -1 : 0;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301116}
1117module_init(wcd9xxx_init);
1118
1119static void __exit wcd9xxx_exit(void)
1120{
1121}
1122module_exit(wcd9xxx_exit);
1123
1124MODULE_DESCRIPTION("Codec core driver");
1125MODULE_VERSION("1.0");
1126MODULE_LICENSE("GPL v2");