blob: 6d80ebd5859031e198f0fa2649d2e32b5add8024 [file] [log] [blame]
Ravishankar Sarawadi839fcf32012-11-14 12:13:00 -08001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05302 *
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>
Kiran Kandi725f8492012-08-06 13:45:16 -070015#include <linux/of_gpio.h>
Joonwoo Parkf6574c72012-10-10 17:29:57 -070016#include <linux/of_irq.h>
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053017#include <linux/slab.h>
Joonwoo Park7bd7d842013-04-09 18:10:25 -070018#include <linux/ratelimit.h>
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053019#include <linux/mfd/core.h>
20#include <linux/mfd/wcd9xxx/wcd9xxx-slimslave.h>
21#include <linux/mfd/wcd9xxx/core.h>
22#include <linux/mfd/wcd9xxx/pdata.h>
23#include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
24
25#include <linux/delay.h>
26#include <linux/gpio.h>
27#include <linux/debugfs.h>
28#include <linux/regulator/consumer.h>
29#include <linux/i2c.h>
30#include <sound/soc.h>
31
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053032#define WCD9XXX_REGISTER_START_OFFSET 0x800
33#define WCD9XXX_SLIM_RW_MAX_TRIES 3
Joonwoo Park3c5b2df2012-08-28 15:36:55 -070034#define SLIMBUS_PRESENT_TIMEOUT 100
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053035
36#define MAX_WCD9XXX_DEVICE 4
Venkat Sudhira41630a2012-10-27 00:57:31 -070037#define CODEC_DT_MAX_PROP_SIZE 40
38#define WCD9XXX_I2C_GSBI_SLAVE_ID "3-000d"
Venkat Sudhira50a3762012-11-26 12:12:15 -080039#define WCD9XXX_I2C_TOP_SLAVE_ADDR 0x0d
40#define WCD9XXX_ANALOG_I2C_SLAVE_ADDR 0x77
41#define WCD9XXX_DIGITAL1_I2C_SLAVE_ADDR 0x66
42#define WCD9XXX_DIGITAL2_I2C_SLAVE_ADDR 0x55
43#define WCD9XXX_I2C_TOP_LEVEL 0
44#define WCD9XXX_I2C_ANALOG 1
45#define WCD9XXX_I2C_DIGITAL_1 2
46#define WCD9XXX_I2C_DIGITAL_2 3
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053047
Bhalchandra Gajare8e3c09f2013-08-23 11:58:35 -070048#define ONDEMAND_REGULATOR true
49#define STATIC_REGULATOR (!ONDEMAND_REGULATOR)
50
Banajit Goswami4d6d891b2012-12-12 23:59:07 -080051/* Number of return values needs to be checked for each
52 * registration of Slimbus of I2C bus for each codec
53 */
54#define NUM_WCD9XXX_REG_RET 8
55
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053056struct wcd9xxx_i2c {
57 struct i2c_client *client;
58 struct i2c_msg xfer_msg[2];
59 struct mutex xfer_lock;
60 int mod_id;
61};
62
Venkat Sudhira41630a2012-10-27 00:57:31 -070063static int wcd9xxx_dt_parse_vreg_info(struct device *dev,
Joonwoo Park5d170a42013-04-10 15:16:36 -070064 struct wcd9xxx_regulator *vreg,
65 const char *vreg_name, bool ondemand);
Venkat Sudhira41630a2012-10-27 00:57:31 -070066static int wcd9xxx_dt_parse_micbias_info(struct device *dev,
67 struct wcd9xxx_micbias_setting *micbias);
68static struct wcd9xxx_pdata *wcd9xxx_populate_dt_pdata(struct device *dev);
69
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053070struct wcd9xxx_i2c wcd9xxx_modules[MAX_WCD9XXX_DEVICE];
Venkat Sudhirdb7aa2b2012-05-15 15:06:14 -070071static int wcd9xxx_intf = -1;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053072
73static int wcd9xxx_read(struct wcd9xxx *wcd9xxx, unsigned short reg,
74 int bytes, void *dest, bool interface_reg)
75{
Joonwoo Parkf35b6072013-04-25 13:26:38 -070076 int i, ret;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053077
78 if (bytes <= 0) {
79 dev_err(wcd9xxx->dev, "Invalid byte read length %d\n", bytes);
80 return -EINVAL;
81 }
82
83 ret = wcd9xxx->read_dev(wcd9xxx, reg, bytes, dest, interface_reg);
84 if (ret < 0) {
85 dev_err(wcd9xxx->dev, "Codec read failed\n");
86 return ret;
Joonwoo Parkf35b6072013-04-25 13:26:38 -070087 } else {
88 for (i = 0; i < bytes; i++)
89 dev_dbg(wcd9xxx->dev, "Read 0x%02x from 0x%x\n",
90 ((u8 *)dest)[i], reg + i);
91 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +053092
93 return 0;
94}
95int wcd9xxx_reg_read(struct wcd9xxx *wcd9xxx, unsigned short reg)
96{
97 u8 val;
98 int ret;
99
100 mutex_lock(&wcd9xxx->io_lock);
101 ret = wcd9xxx_read(wcd9xxx, reg, 1, &val, false);
102 mutex_unlock(&wcd9xxx->io_lock);
103
104 if (ret < 0)
105 return ret;
106 else
107 return val;
108}
109EXPORT_SYMBOL_GPL(wcd9xxx_reg_read);
110
111static int wcd9xxx_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
112 int bytes, void *src, bool interface_reg)
113{
Joonwoo Parkf35b6072013-04-25 13:26:38 -0700114 int i;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530115
116 if (bytes <= 0) {
117 pr_err("%s: Error, invalid write length\n", __func__);
118 return -EINVAL;
119 }
120
Joonwoo Parkf35b6072013-04-25 13:26:38 -0700121 for (i = 0; i < bytes; i++)
122 dev_dbg(wcd9xxx->dev, "Write %02x to 0x%x\n", ((u8 *)src)[i],
123 reg + i);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530124
125 return wcd9xxx->write_dev(wcd9xxx, reg, bytes, src, interface_reg);
126}
127
128int wcd9xxx_reg_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
129 u8 val)
130{
131 int ret;
132
133 mutex_lock(&wcd9xxx->io_lock);
134 ret = wcd9xxx_write(wcd9xxx, reg, 1, &val, false);
135 mutex_unlock(&wcd9xxx->io_lock);
136
137 return ret;
138}
139EXPORT_SYMBOL_GPL(wcd9xxx_reg_write);
140
141static u8 wcd9xxx_pgd_la;
142static u8 wcd9xxx_inf_la;
143
144int wcd9xxx_interface_reg_read(struct wcd9xxx *wcd9xxx, unsigned short reg)
145{
146 u8 val;
147 int ret;
148
149 mutex_lock(&wcd9xxx->io_lock);
150 ret = wcd9xxx_read(wcd9xxx, reg, 1, &val, true);
151 mutex_unlock(&wcd9xxx->io_lock);
152
153 if (ret < 0)
154 return ret;
155 else
156 return val;
157}
158EXPORT_SYMBOL_GPL(wcd9xxx_interface_reg_read);
159
160int wcd9xxx_interface_reg_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
161 u8 val)
162{
163 int ret;
164
165 mutex_lock(&wcd9xxx->io_lock);
166 ret = wcd9xxx_write(wcd9xxx, reg, 1, &val, true);
167 mutex_unlock(&wcd9xxx->io_lock);
168
169 return ret;
170}
171EXPORT_SYMBOL_GPL(wcd9xxx_interface_reg_write);
172
173int wcd9xxx_bulk_read(struct wcd9xxx *wcd9xxx, unsigned short reg,
174 int count, u8 *buf)
175{
176 int ret;
177
178 mutex_lock(&wcd9xxx->io_lock);
179
180 ret = wcd9xxx_read(wcd9xxx, reg, count, buf, false);
181
182 mutex_unlock(&wcd9xxx->io_lock);
183
184 return ret;
185}
186EXPORT_SYMBOL_GPL(wcd9xxx_bulk_read);
187
188int wcd9xxx_bulk_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
189 int count, u8 *buf)
190{
191 int ret;
192
193 mutex_lock(&wcd9xxx->io_lock);
194
195 ret = wcd9xxx_write(wcd9xxx, reg, count, buf, false);
196
197 mutex_unlock(&wcd9xxx->io_lock);
198
199 return ret;
200}
201EXPORT_SYMBOL_GPL(wcd9xxx_bulk_write);
202
203static int wcd9xxx_slim_read_device(struct wcd9xxx *wcd9xxx, unsigned short reg,
204 int bytes, void *dest, bool interface)
205{
206 int ret;
207 struct slim_ele_access msg;
208 int slim_read_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_request_val_element(interface ?
216 wcd9xxx->slim_slave : wcd9xxx->slim,
217 &msg, dest, bytes);
218 mutex_unlock(&wcd9xxx->xfer_lock);
219 if (likely(ret == 0) || (--slim_read_tries == 0))
220 break;
221 usleep_range(5000, 5000);
222 }
223
224 if (ret)
225 pr_err("%s: Error, Codec read failed (%d)\n", __func__, ret);
226
227 return ret;
228}
229/* Interface specifies whether the write is to the interface or general
230 * registers.
231 */
232static int wcd9xxx_slim_write_device(struct wcd9xxx *wcd9xxx,
233 unsigned short reg, int bytes, void *src, bool interface)
234{
235 int ret;
236 struct slim_ele_access msg;
237 int slim_write_tries = WCD9XXX_SLIM_RW_MAX_TRIES;
238 msg.start_offset = WCD9XXX_REGISTER_START_OFFSET + reg;
239 msg.num_bytes = bytes;
240 msg.comp = NULL;
241
242 while (1) {
243 mutex_lock(&wcd9xxx->xfer_lock);
244 ret = slim_change_val_element(interface ?
245 wcd9xxx->slim_slave : wcd9xxx->slim,
246 &msg, src, bytes);
247 mutex_unlock(&wcd9xxx->xfer_lock);
248 if (likely(ret == 0) || (--slim_write_tries == 0))
249 break;
250 usleep_range(5000, 5000);
251 }
252
253 if (ret)
254 pr_err("%s: Error, Codec write failed (%d)\n", __func__, ret);
255
256 return ret;
257}
258
259static struct mfd_cell tabla1x_devs[] = {
260 {
261 .name = "tabla1x_codec",
262 },
263};
264
265static struct mfd_cell tabla_devs[] = {
266 {
267 .name = "tabla_codec",
268 },
269};
270
Asish Bhattacharyab86c3472012-02-15 08:31:52 +0530271static struct mfd_cell sitar_devs[] = {
272 {
273 .name = "sitar_codec",
274 },
275};
276
Joonwoo Parka7172112012-07-23 16:03:49 -0700277static struct mfd_cell taiko_devs[] = {
278 {
279 .name = "taiko_codec",
280 },
281};
282
Banajit Goswami4d6d891b2012-12-12 23:59:07 -0800283static struct mfd_cell tapan_devs[] = {
284 {
285 .name = "tapan_codec",
286 },
287};
288
Joonwoo Park1277cb62013-03-19 14:16:51 -0700289static const struct wcd9xxx_codec_type wcd9xxx_codecs[] = {
Joonwoo Park3cf3a942013-02-13 14:18:22 -0800290 {
Joonwoo Park1277cb62013-03-19 14:16:51 -0700291 TABLA_MAJOR, cpu_to_le16(0x1), tabla1x_devs,
292 ARRAY_SIZE(tabla1x_devs), TABLA_NUM_IRQS, -1,
293 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x03,
Joonwoo Park3cf3a942013-02-13 14:18:22 -0800294 },
295 {
Joonwoo Park1277cb62013-03-19 14:16:51 -0700296 TABLA_MAJOR, cpu_to_le16(0x2), tabla_devs,
297 ARRAY_SIZE(tabla_devs), TABLA_NUM_IRQS, -1,
298 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x03
Joonwoo Park559a5bf2013-02-15 14:46:36 -0800299 },
Joonwoo Park3cf3a942013-02-13 14:18:22 -0800300 {
Joonwoo Park1277cb62013-03-19 14:16:51 -0700301 /* Siter version 1 has same major chip id with Tabla */
302 TABLA_MAJOR, cpu_to_le16(0x0), sitar_devs,
303 ARRAY_SIZE(sitar_devs), SITAR_NUM_IRQS, -1,
304 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x01
Joonwoo Park3cf3a942013-02-13 14:18:22 -0800305 },
306 {
Joonwoo Park1277cb62013-03-19 14:16:51 -0700307 SITAR_MAJOR, cpu_to_le16(0x1), sitar_devs,
308 ARRAY_SIZE(sitar_devs), SITAR_NUM_IRQS, -1,
309 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x01
Bhalchandra Gajare750d12d2013-03-01 20:56:36 -0800310 },
311 {
Joonwoo Park1277cb62013-03-19 14:16:51 -0700312 SITAR_MAJOR, cpu_to_le16(0x2), sitar_devs,
313 ARRAY_SIZE(sitar_devs), SITAR_NUM_IRQS, -1,
314 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x01
Joonwoo Park3cf3a942013-02-13 14:18:22 -0800315 },
316 {
Joonwoo Park1277cb62013-03-19 14:16:51 -0700317 TAIKO_MAJOR, cpu_to_le16(0x0), taiko_devs,
318 ARRAY_SIZE(taiko_devs), TAIKO_NUM_IRQS, 1,
319 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TAIKO, 0x01
Joonwoo Park3cf3a942013-02-13 14:18:22 -0800320 },
321 {
Joonwoo Park1277cb62013-03-19 14:16:51 -0700322 TAIKO_MAJOR, cpu_to_le16(0x1), taiko_devs,
323 ARRAY_SIZE(taiko_devs), TAIKO_NUM_IRQS, 2,
324 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TAIKO, 0x01
325 },
326 {
327 TAPAN_MAJOR, cpu_to_le16(0x0), tapan_devs,
328 ARRAY_SIZE(tapan_devs), TAPAN_NUM_IRQS, -1,
329 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TAIKO, 0x03
330 },
331 {
332 TAPAN_MAJOR, cpu_to_le16(0x1), tapan_devs,
333 ARRAY_SIZE(tapan_devs), TAPAN_NUM_IRQS, -1,
334 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TAIKO, 0x03
Joonwoo Park3cf3a942013-02-13 14:18:22 -0800335 },
Venkat Sudhiree6c5002012-09-06 11:27:32 -0700336};
337
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530338static void wcd9xxx_bring_up(struct wcd9xxx *wcd9xxx)
339{
340 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x4);
341 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_CDC_CTL, 0);
342 usleep_range(5000, 5000);
343 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_CDC_CTL, 3);
344 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 3);
345}
346
347static void wcd9xxx_bring_down(struct wcd9xxx *wcd9xxx)
348{
349 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x7);
350 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x6);
351 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0xe);
352 wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x8);
353}
354
355static int wcd9xxx_reset(struct wcd9xxx *wcd9xxx)
356{
357 int ret;
358
Ravishankar Sarawadi839fcf32012-11-14 12:13:00 -0800359 if (wcd9xxx->reset_gpio && wcd9xxx->slim_device_bootup) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530360 ret = gpio_request(wcd9xxx->reset_gpio, "CDC_RESET");
361 if (ret) {
362 pr_err("%s: Failed to request gpio %d\n", __func__,
363 wcd9xxx->reset_gpio);
364 wcd9xxx->reset_gpio = 0;
365 return ret;
366 }
Ravishankar Sarawadi839fcf32012-11-14 12:13:00 -0800367 }
368 if (wcd9xxx->reset_gpio) {
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530369 gpio_direction_output(wcd9xxx->reset_gpio, 0);
370 msleep(20);
371 gpio_direction_output(wcd9xxx->reset_gpio, 1);
372 msleep(20);
373 }
374 return 0;
375}
376
377static void wcd9xxx_free_reset(struct wcd9xxx *wcd9xxx)
378{
379 if (wcd9xxx->reset_gpio) {
380 gpio_free(wcd9xxx->reset_gpio);
381 wcd9xxx->reset_gpio = 0;
382 }
383}
Joonwoo Park1277cb62013-03-19 14:16:51 -0700384
385static const struct wcd9xxx_codec_type
386*wcd9xxx_check_codec_type(struct wcd9xxx *wcd9xxx, u8 *version)
Venkat Sudhiree6c5002012-09-06 11:27:32 -0700387{
Joonwoo Park1277cb62013-03-19 14:16:51 -0700388 int i, rc;
389 const struct wcd9xxx_codec_type *c, *d = NULL;
390
391 rc = wcd9xxx_bulk_read(wcd9xxx, WCD9XXX_A_CHIP_ID_BYTE_0,
392 sizeof(wcd9xxx->id_minor),
393 (u8 *)&wcd9xxx->id_minor);
394 if (rc < 0)
395 goto exit;
396
397 rc = wcd9xxx_bulk_read(wcd9xxx, WCD9XXX_A_CHIP_ID_BYTE_2,
398 sizeof(wcd9xxx->id_major),
399 (u8 *)&wcd9xxx->id_major);
400 if (rc < 0)
401 goto exit;
402 dev_dbg(wcd9xxx->dev, "%s: wcd9xxx chip id major 0x%x, minor 0x%x\n",
403 __func__, wcd9xxx->id_major, wcd9xxx->id_minor);
404
405 for (i = 0, c = &wcd9xxx_codecs[0]; i < ARRAY_SIZE(wcd9xxx_codecs);
406 i++, c++) {
407 if (c->id_major == wcd9xxx->id_major) {
408 if (c->id_minor == wcd9xxx->id_minor) {
409 d = c;
410 dev_dbg(wcd9xxx->dev,
411 "%s: exact match %s\n", __func__,
412 d->dev->name);
413 break;
414 } else if (!d) {
415 d = c;
416 } else {
417 if ((d->id_minor < c->id_minor) ||
418 (d->id_minor == c->id_minor &&
419 d->version < c->version))
420 d = c;
421 }
422 dev_dbg(wcd9xxx->dev,
423 "%s: best match %s, major 0x%x, minor 0x%x\n",
424 __func__, d->dev->name, d->id_major,
425 d->id_minor);
426 }
Venkat Sudhiree6c5002012-09-06 11:27:32 -0700427 }
428
Joonwoo Park1277cb62013-03-19 14:16:51 -0700429 if (!d) {
430 dev_warn(wcd9xxx->dev,
431 "%s: driver for id major 0x%x, minor 0x%x not found\n",
432 __func__, wcd9xxx->id_major, wcd9xxx->id_minor);
433 } else {
434 if (d->version > -1) {
435 *version = d->version;
436 } else {
437 rc = wcd9xxx_reg_read(wcd9xxx, WCD9XXX_A_CHIP_VERSION);
438 if (rc < 0) {
439 d = NULL;
440 goto exit;
441 }
442 *version = (u8)rc & 0x1F;
Venkat Sudhiree6c5002012-09-06 11:27:32 -0700443 }
Joonwoo Park1277cb62013-03-19 14:16:51 -0700444 dev_info(wcd9xxx->dev,
445 "%s: detected %s, major 0x%x, minor 0x%x, ver 0x%x\n",
446 __func__, d->dev->name, d->id_major, d->id_minor,
447 *version);
Venkat Sudhiree6c5002012-09-06 11:27:32 -0700448 }
Venkat Sudhiree6c5002012-09-06 11:27:32 -0700449exit:
Joonwoo Park1277cb62013-03-19 14:16:51 -0700450 return d;
Venkat Sudhiree6c5002012-09-06 11:27:32 -0700451}
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530452
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700453static int wcd9xxx_device_init(struct wcd9xxx *wcd9xxx)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530454{
455 int ret;
Joonwoo Park1277cb62013-03-19 14:16:51 -0700456 u8 version;
457 const struct wcd9xxx_codec_type *found;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530458
459 mutex_init(&wcd9xxx->io_lock);
460 mutex_init(&wcd9xxx->xfer_lock);
461
462 mutex_init(&wcd9xxx->pm_lock);
463 wcd9xxx->wlock_holders = 0;
464 wcd9xxx->pm_state = WCD9XXX_PM_SLEEPABLE;
465 init_waitqueue_head(&wcd9xxx->pm_wq);
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700466 pm_qos_add_request(&wcd9xxx->pm_qos_req, PM_QOS_CPU_DMA_LATENCY,
467 PM_QOS_DEFAULT_VALUE);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530468
469 dev_set_drvdata(wcd9xxx->dev, wcd9xxx);
470
471 wcd9xxx_bring_up(wcd9xxx);
472
Joonwoo Park1277cb62013-03-19 14:16:51 -0700473 found = wcd9xxx_check_codec_type(wcd9xxx, &version);
474 if (!found) {
475 ret = -ENODEV;
Gopikrishnaiah Anandan7d62cfd2013-07-25 15:46:50 -0400476 goto err;
Joonwoo Park1277cb62013-03-19 14:16:51 -0700477 } else {
478 wcd9xxx->codec_type = found;
479 wcd9xxx->version = version;
480 }
Joonwoo Parkf6574c72012-10-10 17:29:57 -0700481
Kiran Kandi725f8492012-08-06 13:45:16 -0700482 if (wcd9xxx->irq != -1) {
483 ret = wcd9xxx_irq_init(wcd9xxx);
484 if (ret) {
485 pr_err("IRQ initialization failed\n");
486 goto err;
487 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530488 }
489
Joonwoo Park1277cb62013-03-19 14:16:51 -0700490 ret = mfd_add_devices(wcd9xxx->dev, -1, found->dev, found->size,
Joonwoo Parka7172112012-07-23 16:03:49 -0700491 NULL, 0);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530492 if (ret != 0) {
493 dev_err(wcd9xxx->dev, "Failed to add children: %d\n", ret);
494 goto err_irq;
495 }
496 return ret;
497err_irq:
498 wcd9xxx_irq_exit(wcd9xxx);
499err:
500 wcd9xxx_bring_down(wcd9xxx);
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700501 pm_qos_remove_request(&wcd9xxx->pm_qos_req);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530502 mutex_destroy(&wcd9xxx->pm_lock);
503 mutex_destroy(&wcd9xxx->io_lock);
504 mutex_destroy(&wcd9xxx->xfer_lock);
505 return ret;
506}
507
508static void wcd9xxx_device_exit(struct wcd9xxx *wcd9xxx)
509{
510 wcd9xxx_irq_exit(wcd9xxx);
511 wcd9xxx_bring_down(wcd9xxx);
512 wcd9xxx_free_reset(wcd9xxx);
513 mutex_destroy(&wcd9xxx->pm_lock);
Stephen Boyd2fcabf92012-05-30 10:41:11 -0700514 pm_qos_remove_request(&wcd9xxx->pm_qos_req);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530515 mutex_destroy(&wcd9xxx->io_lock);
516 mutex_destroy(&wcd9xxx->xfer_lock);
Venkat Sudhirdb7aa2b2012-05-15 15:06:14 -0700517 if (wcd9xxx_intf == WCD9XXX_INTERFACE_TYPE_SLIMBUS)
518 slim_remove_device(wcd9xxx->slim_slave);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530519 kfree(wcd9xxx);
520}
521
522
523#ifdef CONFIG_DEBUG_FS
524struct wcd9xxx *debugCodec;
525
526static struct dentry *debugfs_wcd9xxx_dent;
527static struct dentry *debugfs_peek;
528static struct dentry *debugfs_poke;
529
530static unsigned char read_data;
531
532static int codec_debug_open(struct inode *inode, struct file *file)
533{
534 file->private_data = inode->i_private;
535 return 0;
536}
537
538static int get_parameters(char *buf, long int *param1, int num_of_par)
539{
540 char *token;
541 int base, cnt;
542
543 token = strsep(&buf, " ");
544
545 for (cnt = 0; cnt < num_of_par; cnt++) {
546 if (token != NULL) {
547 if ((token[1] == 'x') || (token[1] == 'X'))
548 base = 16;
549 else
550 base = 10;
551
552 if (strict_strtoul(token, base, &param1[cnt]) != 0)
553 return -EINVAL;
554
555 token = strsep(&buf, " ");
556 } else
557 return -EINVAL;
558 }
559 return 0;
560}
561
562static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
563 size_t count, loff_t *ppos)
564{
565 char lbuf[8];
566
567 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
568 return simple_read_from_buffer(ubuf, count, ppos, lbuf,
569 strnlen(lbuf, 7));
570}
571
572
573static ssize_t codec_debug_write(struct file *filp,
574 const char __user *ubuf, size_t cnt, loff_t *ppos)
575{
576 char *access_str = filp->private_data;
577 char lbuf[32];
578 int rc;
579 long int param[5];
580
581 if (cnt > sizeof(lbuf) - 1)
582 return -EINVAL;
583
584 rc = copy_from_user(lbuf, ubuf, cnt);
585 if (rc)
586 return -EFAULT;
587
588 lbuf[cnt] = '\0';
589
590 if (!strncmp(access_str, "poke", 6)) {
591 /* write */
592 rc = get_parameters(lbuf, param, 2);
593 if ((param[0] <= 0x3FF) && (param[1] <= 0xFF) &&
594 (rc == 0))
595 wcd9xxx_interface_reg_write(debugCodec, param[0],
596 param[1]);
597 else
598 rc = -EINVAL;
599 } else if (!strncmp(access_str, "peek", 6)) {
600 /* read */
601 rc = get_parameters(lbuf, param, 1);
602 if ((param[0] <= 0x3FF) && (rc == 0))
603 read_data = wcd9xxx_interface_reg_read(debugCodec,
604 param[0]);
605 else
606 rc = -EINVAL;
607 }
608
609 if (rc == 0)
610 rc = cnt;
611 else
612 pr_err("%s: rc = %d\n", __func__, rc);
613
614 return rc;
615}
616
617static const struct file_operations codec_debug_ops = {
618 .open = codec_debug_open,
619 .write = codec_debug_write,
620 .read = codec_debug_read
621};
622#endif
623
Joonwoo Park5d170a42013-04-10 15:16:36 -0700624static int wcd9xxx_init_supplies(struct wcd9xxx *wcd9xxx,
625 struct wcd9xxx_pdata *pdata)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530626{
627 int ret;
628 int i;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530629 wcd9xxx->supplies = kzalloc(sizeof(struct regulator_bulk_data) *
630 ARRAY_SIZE(pdata->regulator),
631 GFP_KERNEL);
632 if (!wcd9xxx->supplies) {
633 ret = -ENOMEM;
634 goto err;
635 }
636
Kiran Kandi725f8492012-08-06 13:45:16 -0700637 wcd9xxx->num_of_supplies = 0;
Simmi Pateriya4fd69932012-10-26 00:57:06 +0530638
Joonwoo Park5d170a42013-04-10 15:16:36 -0700639 if (ARRAY_SIZE(pdata->regulator) > WCD9XXX_MAX_REGULATOR) {
Simmi Pateriya4fd69932012-10-26 00:57:06 +0530640 pr_err("%s: Array Size out of bound\n", __func__);
641 ret = -EINVAL;
642 goto err;
643 }
644
Kiran Kandi725f8492012-08-06 13:45:16 -0700645 for (i = 0; i < ARRAY_SIZE(pdata->regulator); i++) {
646 if (pdata->regulator[i].name) {
647 wcd9xxx->supplies[i].supply = pdata->regulator[i].name;
648 wcd9xxx->num_of_supplies++;
649 }
650 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530651
Kiran Kandi725f8492012-08-06 13:45:16 -0700652 ret = regulator_bulk_get(wcd9xxx->dev, wcd9xxx->num_of_supplies,
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530653 wcd9xxx->supplies);
654 if (ret != 0) {
655 dev_err(wcd9xxx->dev, "Failed to get supplies: err = %d\n",
656 ret);
657 goto err_supplies;
658 }
659
Kiran Kandi725f8492012-08-06 13:45:16 -0700660 for (i = 0; i < wcd9xxx->num_of_supplies; i++) {
Joonwoo Park5d170a42013-04-10 15:16:36 -0700661 if (regulator_count_voltages(wcd9xxx->supplies[i].consumer) <=
662 0)
663 continue;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530664 ret = regulator_set_voltage(wcd9xxx->supplies[i].consumer,
Joonwoo Park5d170a42013-04-10 15:16:36 -0700665 pdata->regulator[i].min_uV,
666 pdata->regulator[i].max_uV);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530667 if (ret) {
668 pr_err("%s: Setting regulator voltage failed for "
669 "regulator %s err = %d\n", __func__,
670 wcd9xxx->supplies[i].supply, ret);
671 goto err_get;
672 }
673
674 ret = regulator_set_optimum_mode(wcd9xxx->supplies[i].consumer,
Joonwoo Park5d170a42013-04-10 15:16:36 -0700675 pdata->regulator[i].optimum_uA);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530676 if (ret < 0) {
677 pr_err("%s: Setting regulator optimum mode failed for "
678 "regulator %s err = %d\n", __func__,
679 wcd9xxx->supplies[i].supply, ret);
680 goto err_get;
Joonwoo Park5d170a42013-04-10 15:16:36 -0700681 } else {
682 ret = 0;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530683 }
684 }
685
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530686 return ret;
687
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530688err_get:
Kiran Kandi725f8492012-08-06 13:45:16 -0700689 regulator_bulk_free(wcd9xxx->num_of_supplies, wcd9xxx->supplies);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530690err_supplies:
691 kfree(wcd9xxx->supplies);
692err:
693 return ret;
694}
695
Joonwoo Park5d170a42013-04-10 15:16:36 -0700696static int wcd9xxx_enable_static_supplies(struct wcd9xxx *wcd9xxx,
697 struct wcd9xxx_pdata *pdata)
698{
699 int i;
700 int ret = 0;
701
702 for (i = 0; i < wcd9xxx->num_of_supplies; i++) {
703 if (pdata->regulator[i].ondemand)
704 continue;
705 ret = regulator_enable(wcd9xxx->supplies[i].consumer);
706 if (ret) {
707 pr_err("%s: Failed to enable %s\n", __func__,
708 wcd9xxx->supplies[i].supply);
709 break;
710 } else {
711 pr_debug("%s: Enabled regulator %s\n", __func__,
712 wcd9xxx->supplies[i].supply);
713 }
714 }
715
716 while (ret && --i)
717 if (!pdata->regulator[i].ondemand)
718 regulator_disable(wcd9xxx->supplies[i].consumer);
719
720 return ret;
721}
722
Venkat Sudhir49203862012-05-21 14:29:13 -0700723static void wcd9xxx_disable_supplies(struct wcd9xxx *wcd9xxx,
724 struct wcd9xxx_pdata *pdata)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530725{
726 int i;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530727
Kiran Kandi725f8492012-08-06 13:45:16 -0700728 regulator_bulk_disable(wcd9xxx->num_of_supplies,
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530729 wcd9xxx->supplies);
Kiran Kandi725f8492012-08-06 13:45:16 -0700730 for (i = 0; i < wcd9xxx->num_of_supplies; i++) {
Joonwoo Park5d170a42013-04-10 15:16:36 -0700731 if (regulator_count_voltages(wcd9xxx->supplies[i].consumer) <=
732 0)
733 continue;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530734 regulator_set_voltage(wcd9xxx->supplies[i].consumer, 0,
Joonwoo Park5d170a42013-04-10 15:16:36 -0700735 pdata->regulator[i].max_uV);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530736 regulator_set_optimum_mode(wcd9xxx->supplies[i].consumer, 0);
737 }
Kiran Kandi725f8492012-08-06 13:45:16 -0700738 regulator_bulk_free(wcd9xxx->num_of_supplies, wcd9xxx->supplies);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530739 kfree(wcd9xxx->supplies);
740}
741
Venkat Sudhira50a3762012-11-26 12:12:15 -0800742enum wcd9xxx_intf_status wcd9xxx_get_intf_type(void)
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530743{
744 return wcd9xxx_intf;
745}
Venkat Sudhira50a3762012-11-26 12:12:15 -0800746
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530747EXPORT_SYMBOL_GPL(wcd9xxx_get_intf_type);
748
749struct wcd9xxx_i2c *get_i2c_wcd9xxx_device_info(u16 reg)
750{
751 u16 mask = 0x0f00;
752 int value = 0;
753 struct wcd9xxx_i2c *wcd9xxx = NULL;
754 value = ((reg & mask) >> 8) & 0x000f;
755 switch (value) {
756 case 0:
757 wcd9xxx = &wcd9xxx_modules[0];
758 break;
759 case 1:
760 wcd9xxx = &wcd9xxx_modules[1];
761 break;
762 case 2:
763 wcd9xxx = &wcd9xxx_modules[2];
764 break;
765 case 3:
766 wcd9xxx = &wcd9xxx_modules[3];
767 break;
768 default:
769 break;
770 }
771 return wcd9xxx;
772}
773
774int wcd9xxx_i2c_write_device(u16 reg, u8 *value,
775 u32 bytes)
776{
777
778 struct i2c_msg *msg;
779 int ret = 0;
780 u8 reg_addr = 0;
781 u8 data[bytes + 1];
782 struct wcd9xxx_i2c *wcd9xxx;
783
784 wcd9xxx = get_i2c_wcd9xxx_device_info(reg);
785 if (wcd9xxx == NULL || wcd9xxx->client == NULL) {
786 pr_err("failed to get device info\n");
787 return -ENODEV;
788 }
789 reg_addr = (u8)reg;
790 msg = &wcd9xxx->xfer_msg[0];
791 msg->addr = wcd9xxx->client->addr;
792 msg->len = bytes + 1;
793 msg->flags = 0;
794 data[0] = reg;
795 data[1] = *value;
796 msg->buf = data;
797 ret = i2c_transfer(wcd9xxx->client->adapter, wcd9xxx->xfer_msg, 1);
798 /* Try again if the write fails */
799 if (ret != 1) {
800 ret = i2c_transfer(wcd9xxx->client->adapter,
801 wcd9xxx->xfer_msg, 1);
802 if (ret != 1) {
803 pr_err("failed to write the device\n");
804 return ret;
805 }
806 }
807 pr_debug("write sucess register = %x val = %x\n", reg, data[1]);
808 return 0;
809}
810
811
812int wcd9xxx_i2c_read_device(unsigned short reg,
813 int bytes, unsigned char *dest)
814{
815 struct i2c_msg *msg;
816 int ret = 0;
817 u8 reg_addr = 0;
818 struct wcd9xxx_i2c *wcd9xxx;
819 u8 i = 0;
820
821 wcd9xxx = get_i2c_wcd9xxx_device_info(reg);
822 if (wcd9xxx == NULL || wcd9xxx->client == NULL) {
823 pr_err("failed to get device info\n");
824 return -ENODEV;
825 }
826 for (i = 0; i < bytes; i++) {
827 reg_addr = (u8)reg++;
828 msg = &wcd9xxx->xfer_msg[0];
829 msg->addr = wcd9xxx->client->addr;
830 msg->len = 1;
831 msg->flags = 0;
832 msg->buf = &reg_addr;
833
834 msg = &wcd9xxx->xfer_msg[1];
835 msg->addr = wcd9xxx->client->addr;
836 msg->len = 1;
837 msg->flags = I2C_M_RD;
838 msg->buf = dest++;
839 ret = i2c_transfer(wcd9xxx->client->adapter,
840 wcd9xxx->xfer_msg, 2);
841
842 /* Try again if read fails first time */
843 if (ret != 2) {
844 ret = i2c_transfer(wcd9xxx->client->adapter,
845 wcd9xxx->xfer_msg, 2);
846 if (ret != 2) {
847 pr_err("failed to read wcd9xxx register\n");
848 return ret;
849 }
850 }
851 }
852 return 0;
853}
854
855int wcd9xxx_i2c_read(struct wcd9xxx *wcd9xxx, unsigned short reg,
856 int bytes, void *dest, bool interface_reg)
857{
858 return wcd9xxx_i2c_read_device(reg, bytes, dest);
859}
860
861int wcd9xxx_i2c_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
862 int bytes, void *src, bool interface_reg)
863{
864 return wcd9xxx_i2c_write_device(reg, src, bytes);
865}
866
Venkat Sudhira50a3762012-11-26 12:12:15 -0800867static int wcd9xxx_i2c_get_client_index(struct i2c_client *client,
868 int *wcd9xx_index)
869{
870 int ret = 0;
871 switch (client->addr) {
872 case WCD9XXX_I2C_TOP_SLAVE_ADDR:
873 *wcd9xx_index = WCD9XXX_I2C_TOP_LEVEL;
874 break;
875 case WCD9XXX_ANALOG_I2C_SLAVE_ADDR:
876 *wcd9xx_index = WCD9XXX_I2C_ANALOG;
877 break;
878 case WCD9XXX_DIGITAL1_I2C_SLAVE_ADDR:
879 *wcd9xx_index = WCD9XXX_I2C_DIGITAL_1;
880 break;
881 case WCD9XXX_DIGITAL2_I2C_SLAVE_ADDR:
882 *wcd9xx_index = WCD9XXX_I2C_DIGITAL_2;
883 break;
884 default:
885 ret = -EINVAL;
886 break;
887 }
888 return ret;
889}
890
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530891static int __devinit wcd9xxx_i2c_probe(struct i2c_client *client,
892 const struct i2c_device_id *id)
893{
Venkat Sudhira50a3762012-11-26 12:12:15 -0800894 struct wcd9xxx *wcd9xxx = NULL;
895 struct wcd9xxx_pdata *pdata = NULL;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530896 int val = 0;
897 int ret = 0;
Venkat Sudhira50a3762012-11-26 12:12:15 -0800898 int wcd9xx_index = 0;
Venkat Sudhira41630a2012-10-27 00:57:31 -0700899 struct device *dev;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530900
Venkat Sudhira50a3762012-11-26 12:12:15 -0800901 pr_debug("%s: interface status %d\n", __func__, wcd9xxx_intf);
Venkat Sudhirdb7aa2b2012-05-15 15:06:14 -0700902 if (wcd9xxx_intf == WCD9XXX_INTERFACE_TYPE_SLIMBUS) {
Venkat Sudhira41630a2012-10-27 00:57:31 -0700903 dev_dbg(&client->dev, "%s:Codec is detected in slimbus mode\n",
Venkat Sudhira50a3762012-11-26 12:12:15 -0800904 __func__);
Venkat Sudhirdb7aa2b2012-05-15 15:06:14 -0700905 return -ENODEV;
Venkat Sudhira50a3762012-11-26 12:12:15 -0800906 } else if (wcd9xxx_intf == WCD9XXX_INTERFACE_TYPE_I2C) {
907 ret = wcd9xxx_i2c_get_client_index(client, &wcd9xx_index);
908 if (ret != 0)
909 dev_err(&client->dev, "%s: I2C set codec I2C\n"
910 "client failed\n", __func__);
911 else {
912 dev_err(&client->dev, "%s:probe for other slaves\n"
913 "devices of codec I2C slave Addr = %x\n",
914 __func__, client->addr);
915 wcd9xxx_modules[wcd9xx_index].client = client;
916 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530917 return ret;
Venkat Sudhira50a3762012-11-26 12:12:15 -0800918 } else if (wcd9xxx_intf == WCD9XXX_INTERFACE_TYPE_PROBING) {
919 dev = &client->dev;
920 if (client->dev.of_node) {
921 dev_dbg(&client->dev, "%s:Platform data\n"
922 "from device tree\n", __func__);
923 pdata = wcd9xxx_populate_dt_pdata(&client->dev);
924 client->dev.platform_data = pdata;
925 } else {
926 dev_dbg(&client->dev, "%s:Platform data from\n"
927 "board file\n", __func__);
928 pdata = client->dev.platform_data;
929 }
930 wcd9xxx = kzalloc(sizeof(struct wcd9xxx), GFP_KERNEL);
931 if (wcd9xxx == NULL) {
932 pr_err("%s: error, allocation failed\n", __func__);
933 ret = -ENOMEM;
934 goto fail;
935 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530936
Venkat Sudhira50a3762012-11-26 12:12:15 -0800937 if (!pdata) {
938 dev_dbg(&client->dev, "no platform data?\n");
939 ret = -EINVAL;
940 goto fail;
941 }
942 if (i2c_check_functionality(client->adapter,
943 I2C_FUNC_I2C) == 0) {
944 dev_dbg(&client->dev, "can't talk I2C?\n");
945 ret = -EIO;
946 goto fail;
947 }
948 dev_set_drvdata(&client->dev, wcd9xxx);
949 wcd9xxx->dev = &client->dev;
950 wcd9xxx->reset_gpio = pdata->reset_gpio;
Ravishankar Sarawadi2293efe2013-01-11 16:37:23 -0800951 wcd9xxx->slim_device_bootup = true;
Venkat Sudhira50a3762012-11-26 12:12:15 -0800952 if (client->dev.of_node)
953 wcd9xxx->mclk_rate = pdata->mclk_rate;
Joonwoo Park5d170a42013-04-10 15:16:36 -0700954
955 ret = wcd9xxx_init_supplies(wcd9xxx, pdata);
Venkat Sudhira50a3762012-11-26 12:12:15 -0800956 if (ret) {
957 pr_err("%s: Fail to enable Codec supplies\n",
958 __func__);
959 goto err_codec;
960 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530961
Joonwoo Park5d170a42013-04-10 15:16:36 -0700962 ret = wcd9xxx_enable_static_supplies(wcd9xxx, pdata);
963 if (ret) {
964 pr_err("%s: Fail to enable Codec pre-reset supplies\n",
965 __func__);
966 goto err_codec;
967 }
Venkat Sudhira50a3762012-11-26 12:12:15 -0800968 usleep_range(5, 5);
Joonwoo Park5d170a42013-04-10 15:16:36 -0700969
Venkat Sudhira50a3762012-11-26 12:12:15 -0800970 ret = wcd9xxx_reset(wcd9xxx);
971 if (ret) {
972 pr_err("%s: Resetting Codec failed\n", __func__);
Joonwoo Park5d170a42013-04-10 15:16:36 -0700973 goto err_supplies;
Venkat Sudhira50a3762012-11-26 12:12:15 -0800974 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530975
Venkat Sudhira50a3762012-11-26 12:12:15 -0800976 ret = wcd9xxx_i2c_get_client_index(client, &wcd9xx_index);
977 if (ret != 0) {
978 pr_err("%s:Set codec I2C client failed\n", __func__);
979 goto err_supplies;
980 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +0530981
Venkat Sudhira50a3762012-11-26 12:12:15 -0800982 wcd9xxx_modules[wcd9xx_index].client = client;
983 wcd9xxx->read_dev = wcd9xxx_i2c_read;
984 wcd9xxx->write_dev = wcd9xxx_i2c_write;
985 if (!wcd9xxx->dev->of_node) {
986 wcd9xxx->irq = pdata->irq;
987 wcd9xxx->irq_base = pdata->irq_base;
988 }
Asish Bhattacharya6dd4cb52012-07-05 19:47:42 +0530989
Venkat Sudhira50a3762012-11-26 12:12:15 -0800990 ret = wcd9xxx_device_init(wcd9xxx);
991 if (ret) {
992 pr_err("%s: error, initializing device failed\n",
993 __func__);
994 goto err_device_init;
995 }
Asish Bhattacharya6dd4cb52012-07-05 19:47:42 +0530996
Venkat Sudhira50a3762012-11-26 12:12:15 -0800997 ret = wcd9xxx_read(wcd9xxx, WCD9XXX_A_CHIP_STATUS, 1, &val, 0);
Joonwoo Park1277cb62013-03-19 14:16:51 -0700998 if (ret < 0)
999 pr_err("%s: failed to read the wcd9xxx status (%d)\n",
1000 __func__, ret);
1001 if (val != wcd9xxx->codec_type->i2c_chip_status)
1002 pr_err("%s: unknown chip status 0x%x\n", __func__, val);
Venkat Sudhira50a3762012-11-26 12:12:15 -08001003
Joonwoo Park1277cb62013-03-19 14:16:51 -07001004 wcd9xxx_intf = WCD9XXX_INTERFACE_TYPE_I2C;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301005
Venkat Sudhira50a3762012-11-26 12:12:15 -08001006 return ret;
1007 } else
1008 pr_err("%s: I2C probe in wrong state\n", __func__);
1009
1010
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301011err_device_init:
1012 wcd9xxx_free_reset(wcd9xxx);
1013err_supplies:
Venkat Sudhir49203862012-05-21 14:29:13 -07001014 wcd9xxx_disable_supplies(wcd9xxx, pdata);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301015err_codec:
1016 kfree(wcd9xxx);
1017fail:
1018 return ret;
1019}
1020
1021static int __devexit wcd9xxx_i2c_remove(struct i2c_client *client)
1022{
1023 struct wcd9xxx *wcd9xxx;
Venkat Sudhir49203862012-05-21 14:29:13 -07001024 struct wcd9xxx_pdata *pdata = client->dev.platform_data;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301025 pr_debug("exit\n");
1026 wcd9xxx = dev_get_drvdata(&client->dev);
Venkat Sudhir49203862012-05-21 14:29:13 -07001027 wcd9xxx_disable_supplies(wcd9xxx, pdata);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301028 wcd9xxx_device_exit(wcd9xxx);
1029 return 0;
1030}
1031
Kiran Kandi725f8492012-08-06 13:45:16 -07001032static int wcd9xxx_dt_parse_vreg_info(struct device *dev,
Joonwoo Park5d170a42013-04-10 15:16:36 -07001033 struct wcd9xxx_regulator *vreg,
1034 const char *vreg_name,
1035 bool ondemand)
Kiran Kandi725f8492012-08-06 13:45:16 -07001036{
1037 int len, ret = 0;
1038 const __be32 *prop;
1039 char prop_name[CODEC_DT_MAX_PROP_SIZE];
1040 struct device_node *regnode = NULL;
1041 u32 prop_val;
1042
1043 snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "%s-supply",
1044 vreg_name);
1045 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
1046
1047 if (!regnode) {
1048 dev_err(dev, "Looking up %s property in node %s failed",
1049 prop_name, dev->of_node->full_name);
1050 return -ENODEV;
1051 }
1052 vreg->name = vreg_name;
Joonwoo Park5d170a42013-04-10 15:16:36 -07001053 vreg->ondemand = ondemand;
Kiran Kandi725f8492012-08-06 13:45:16 -07001054
1055 snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE,
1056 "qcom,%s-voltage", vreg_name);
1057 prop = of_get_property(dev->of_node, prop_name, &len);
1058
1059 if (!prop || (len != (2 * sizeof(__be32)))) {
1060 dev_err(dev, "%s %s property\n",
1061 prop ? "invalid format" : "no", prop_name);
Joonwoo Park5d170a42013-04-10 15:16:36 -07001062 return -EINVAL;
Kiran Kandi725f8492012-08-06 13:45:16 -07001063 } else {
1064 vreg->min_uV = be32_to_cpup(&prop[0]);
1065 vreg->max_uV = be32_to_cpup(&prop[1]);
1066 }
1067
1068 snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE,
1069 "qcom,%s-current", vreg_name);
1070
1071 ret = of_property_read_u32(dev->of_node, prop_name, &prop_val);
1072 if (ret) {
1073 dev_err(dev, "Looking up %s property in node %s failed",
1074 prop_name, dev->of_node->full_name);
Joonwoo Park5d170a42013-04-10 15:16:36 -07001075 return -EFAULT;
Kiran Kandi725f8492012-08-06 13:45:16 -07001076 }
1077 vreg->optimum_uA = prop_val;
1078
Joonwoo Park5d170a42013-04-10 15:16:36 -07001079 dev_info(dev, "%s: vol=[%d %d]uV, curr=[%d]uA, ond %d\n", vreg->name,
1080 vreg->min_uV, vreg->max_uV, vreg->optimum_uA, vreg->ondemand);
Kiran Kandi725f8492012-08-06 13:45:16 -07001081 return 0;
1082}
1083
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001084static int wcd9xxx_read_of_property_u32(struct device *dev,
1085 const char *name, u32 *val)
1086{
1087 int ret = 0;
1088 ret = of_property_read_u32(dev->of_node, name, val);
1089 if (ret)
1090 dev_err(dev, "Looking up %s property in node %s failed",
1091 name, dev->of_node->full_name);
1092 return ret;
1093}
1094
Kiran Kandi725f8492012-08-06 13:45:16 -07001095static int wcd9xxx_dt_parse_micbias_info(struct device *dev,
1096 struct wcd9xxx_micbias_setting *micbias)
1097{
Kiran Kandi725f8492012-08-06 13:45:16 -07001098 u32 prop_val;
1099
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001100 if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias-ldoh-v",
1101 &prop_val)))
1102 micbias->ldoh_v = (u8)prop_val;
Kiran Kandi725f8492012-08-06 13:45:16 -07001103
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001104 wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias-cfilt1-mv",
1105 &micbias->cfilt1_mv);
Kiran Kandi725f8492012-08-06 13:45:16 -07001106
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001107 wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias-cfilt2-mv",
1108 &micbias->cfilt2_mv);
Kiran Kandi725f8492012-08-06 13:45:16 -07001109
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001110 wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias-cfilt3-mv",
1111 &micbias->cfilt3_mv);
Kiran Kandi725f8492012-08-06 13:45:16 -07001112
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001113 /* Read micbias values for codec. Does not matter even if a few
1114 * micbias values are not defined in the Device Tree. Codec will
1115 * anyway not use those values
1116 */
1117 if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias1-cfilt-sel",
1118 &prop_val)))
1119 micbias->bias1_cfilt_sel = (u8)prop_val;
Kiran Kandi725f8492012-08-06 13:45:16 -07001120
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001121 if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias2-cfilt-sel",
1122 &prop_val)))
1123 micbias->bias2_cfilt_sel = (u8)prop_val;
Kiran Kandi725f8492012-08-06 13:45:16 -07001124
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001125 if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias3-cfilt-sel",
1126 &prop_val)))
1127 micbias->bias3_cfilt_sel = (u8)prop_val;
Kiran Kandi725f8492012-08-06 13:45:16 -07001128
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001129 if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias4-cfilt-sel",
1130 &prop_val)))
1131 micbias->bias4_cfilt_sel = (u8)prop_val;
Kiran Kandi725f8492012-08-06 13:45:16 -07001132
Joonwoo Parkadf25972012-10-18 13:18:08 -07001133 /* micbias external cap */
1134 micbias->bias1_cap_mode =
1135 (of_property_read_bool(dev->of_node, "qcom,cdc-micbias1-ext-cap") ?
1136 MICBIAS_EXT_BYP_CAP : MICBIAS_NO_EXT_BYP_CAP);
1137 micbias->bias2_cap_mode =
1138 (of_property_read_bool(dev->of_node, "qcom,cdc-micbias2-ext-cap") ?
1139 MICBIAS_EXT_BYP_CAP : MICBIAS_NO_EXT_BYP_CAP);
1140 micbias->bias3_cap_mode =
1141 (of_property_read_bool(dev->of_node, "qcom,cdc-micbias3-ext-cap") ?
1142 MICBIAS_EXT_BYP_CAP : MICBIAS_NO_EXT_BYP_CAP);
1143 micbias->bias4_cap_mode =
1144 (of_property_read_bool(dev->of_node, "qcom,cdc-micbias4-ext-cap") ?
1145 MICBIAS_EXT_BYP_CAP : MICBIAS_NO_EXT_BYP_CAP);
1146
Joonwoo Park88bfa842013-04-15 16:59:21 -07001147 micbias->bias2_is_headset_only =
1148 of_property_read_bool(dev->of_node,
1149 "qcom,cdc-micbias2-headset-only");
1150
Kiran Kandi725f8492012-08-06 13:45:16 -07001151 dev_dbg(dev, "ldoh_v %u cfilt1_mv %u cfilt2_mv %u cfilt3_mv %u",
1152 (u32)micbias->ldoh_v, (u32)micbias->cfilt1_mv,
1153 (u32)micbias->cfilt2_mv, (u32)micbias->cfilt3_mv);
1154
1155 dev_dbg(dev, "bias1_cfilt_sel %u bias2_cfilt_sel %u\n",
1156 (u32)micbias->bias1_cfilt_sel, (u32)micbias->bias2_cfilt_sel);
1157
1158 dev_dbg(dev, "bias3_cfilt_sel %u bias4_cfilt_sel %u\n",
1159 (u32)micbias->bias3_cfilt_sel, (u32)micbias->bias4_cfilt_sel);
1160
Joonwoo Parkadf25972012-10-18 13:18:08 -07001161 dev_dbg(dev, "bias1_ext_cap %d bias2_ext_cap %d\n",
1162 micbias->bias1_cap_mode, micbias->bias2_cap_mode);
1163 dev_dbg(dev, "bias3_ext_cap %d bias4_ext_cap %d\n",
1164 micbias->bias3_cap_mode, micbias->bias4_cap_mode);
1165
Joonwoo Park88bfa842013-04-15 16:59:21 -07001166 dev_dbg(dev, "bias2_is_headset_only %d\n",
1167 micbias->bias2_is_headset_only);
Kiran Kandi725f8492012-08-06 13:45:16 -07001168 return 0;
1169}
1170
1171static int wcd9xxx_dt_parse_slim_interface_dev_info(struct device *dev,
1172 struct slim_device *slim_ifd)
1173{
1174 int ret = 0;
1175 struct property *prop;
1176
1177 ret = of_property_read_string(dev->of_node, "qcom,cdc-slim-ifd",
1178 &slim_ifd->name);
1179 if (ret) {
1180 dev_err(dev, "Looking up %s property in node %s failed",
1181 "qcom,cdc-slim-ifd-dev", dev->of_node->full_name);
1182 return -ENODEV;
1183 }
1184 prop = of_find_property(dev->of_node,
1185 "qcom,cdc-slim-ifd-elemental-addr", NULL);
1186 if (!prop) {
1187 dev_err(dev, "Looking up %s property in node %s failed",
1188 "qcom,cdc-slim-ifd-elemental-addr",
1189 dev->of_node->full_name);
1190 return -ENODEV;
1191 } else if (prop->length != 6) {
1192 dev_err(dev, "invalid codec slim ifd addr. addr length = %d\n",
1193 prop->length);
1194 return -ENODEV;
1195 }
1196 memcpy(slim_ifd->e_addr, prop->value, 6);
1197
1198 return 0;
1199}
1200
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001201static int wcd9xxx_process_supplies(struct device *dev,
1202 struct wcd9xxx_pdata *pdata, const char *supply_list,
1203 int supply_cnt, bool is_ondemand, int index)
1204{
1205 int idx, ret = 0;
1206 const char *name;
1207
1208 if (supply_cnt == 0) {
1209 dev_dbg(dev, "%s: no supplies defined for %s\n", __func__,
1210 supply_list);
1211 return 0;
1212 }
1213
1214 for (idx = 0; idx < supply_cnt; idx++) {
1215 ret = of_property_read_string_index(dev->of_node,
1216 supply_list, idx,
1217 &name);
1218 if (ret) {
1219 dev_err(dev, "%s: of read string %s idx %d error %d\n",
1220 __func__, supply_list, idx, ret);
1221 goto err;
1222 }
1223
1224 dev_dbg(dev, "%s: Found cdc supply %s as part of %s\n",
1225 __func__, name, supply_list);
1226 ret = wcd9xxx_dt_parse_vreg_info(dev,
1227 &pdata->regulator[index + idx],
1228 name, is_ondemand);
1229 if (ret)
1230 goto err;
1231 }
1232
1233 return 0;
1234
1235err:
1236 return ret;
1237
1238}
1239
Kiran Kandi725f8492012-08-06 13:45:16 -07001240static struct wcd9xxx_pdata *wcd9xxx_populate_dt_pdata(struct device *dev)
1241{
1242 struct wcd9xxx_pdata *pdata;
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001243 int ret, static_cnt, ond_cnt, cp_supplies_cnt;
Venkat Sudhira50a3762012-11-26 12:12:15 -08001244 u32 mclk_rate = 0;
Damir Didjusto1a353ce2013-04-02 11:45:47 -07001245 u32 dmic_sample_rate = 0;
Joonwoo Park73239212013-04-10 15:11:06 -07001246 const char *static_prop_name = "qcom,cdc-static-supplies";
Joonwoo Park5d170a42013-04-10 15:16:36 -07001247 const char *ond_prop_name = "qcom,cdc-on-demand-supplies";
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001248 const char *cp_supplies_name = "qcom,cdc-cp-supplies";
Kiran Kandi725f8492012-08-06 13:45:16 -07001249
1250 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1251 if (!pdata) {
Venkat Sudhira41630a2012-10-27 00:57:31 -07001252 dev_err(dev, "could not allocate memory for platform data\n");
Kiran Kandi725f8492012-08-06 13:45:16 -07001253 return NULL;
1254 }
Joonwoo Park73239212013-04-10 15:11:06 -07001255
1256 static_cnt = of_property_count_strings(dev->of_node, static_prop_name);
1257 if (IS_ERR_VALUE(static_cnt)) {
1258 dev_err(dev, "%s: Failed to get static supplies %d\n", __func__,
1259 static_cnt);
Kiran Kandi725f8492012-08-06 13:45:16 -07001260 goto err;
1261 }
1262
Joonwoo Park5d170a42013-04-10 15:16:36 -07001263 /* On-demand supply list is an optional property */
1264 ond_cnt = of_property_count_strings(dev->of_node, ond_prop_name);
1265 if (IS_ERR_VALUE(ond_cnt))
1266 ond_cnt = 0;
1267
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001268 /* cp-supplies list is an optional property */
1269 cp_supplies_cnt = of_property_count_strings(dev->of_node,
1270 cp_supplies_name);
1271 if (IS_ERR_VALUE(cp_supplies_cnt))
1272 cp_supplies_cnt = 0;
1273
1274 BUG_ON(static_cnt <= 0 || ond_cnt < 0 || cp_supplies_cnt < 0);
1275 if ((static_cnt + ond_cnt + cp_supplies_cnt)
1276 > ARRAY_SIZE(pdata->regulator)) {
Kiran Kandi725f8492012-08-06 13:45:16 -07001277 dev_err(dev, "%s: Num of supplies %u > max supported %u\n",
Joonwoo Park73239212013-04-10 15:11:06 -07001278 __func__, static_cnt, ARRAY_SIZE(pdata->regulator));
Kiran Kandi725f8492012-08-06 13:45:16 -07001279 goto err;
1280 }
1281
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001282 ret = wcd9xxx_process_supplies(dev, pdata, static_prop_name,
Bhalchandra Gajare8e3c09f2013-08-23 11:58:35 -07001283 static_cnt, STATIC_REGULATOR, 0);
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001284 if (ret)
1285 goto err;
Joonwoo Park73239212013-04-10 15:11:06 -07001286
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001287 ret = wcd9xxx_process_supplies(dev, pdata, ond_prop_name,
Bhalchandra Gajare8e3c09f2013-08-23 11:58:35 -07001288 ond_cnt, ONDEMAND_REGULATOR, static_cnt);
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001289 if (ret)
1290 goto err;
Joonwoo Park5d170a42013-04-10 15:16:36 -07001291
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001292 ret = wcd9xxx_process_supplies(dev, pdata, cp_supplies_name,
Bhalchandra Gajare8e3c09f2013-08-23 11:58:35 -07001293 cp_supplies_cnt, ONDEMAND_REGULATOR,
1294 static_cnt + ond_cnt);
Bhalchandra Gajareadd903b2013-07-03 14:32:38 -07001295 if (ret)
1296 goto err;
Kiran Kandi725f8492012-08-06 13:45:16 -07001297
1298 ret = wcd9xxx_dt_parse_micbias_info(dev, &pdata->micbias);
1299 if (ret)
1300 goto err;
1301
1302 pdata->reset_gpio = of_get_named_gpio(dev->of_node,
1303 "qcom,cdc-reset-gpio", 0);
1304 if (pdata->reset_gpio < 0) {
1305 dev_err(dev, "Looking up %s property in node %s failed %d\n",
1306 "qcom, cdc-reset-gpio", dev->of_node->full_name,
1307 pdata->reset_gpio);
1308 goto err;
1309 }
Venkat Sudhira41630a2012-10-27 00:57:31 -07001310 dev_dbg(dev, "%s: reset gpio %d", __func__, pdata->reset_gpio);
Venkat Sudhira50a3762012-11-26 12:12:15 -08001311 ret = of_property_read_u32(dev->of_node,
1312 "qcom,cdc-mclk-clk-rate",
1313 &mclk_rate);
1314 if (ret) {
1315 dev_err(dev, "Looking up %s property in\n"
1316 "node %s failed",
1317 "qcom,cdc-mclk-clk-rate",
1318 dev->of_node->full_name);
1319 devm_kfree(dev, pdata);
1320 ret = -EINVAL;
1321 goto err;
1322 }
1323 pdata->mclk_rate = mclk_rate;
Damir Didjusto1a353ce2013-04-02 11:45:47 -07001324
1325 ret = of_property_read_u32(dev->of_node,
1326 "qcom,cdc-dmic-sample-rate",
1327 &dmic_sample_rate);
1328 if (ret) {
1329 dev_err(dev, "Looking up %s property in node %s failed",
1330 "qcom,cdc-dmic-sample-rate",
1331 dev->of_node->full_name);
1332 dmic_sample_rate = TAIKO_DMIC_SAMPLE_RATE_UNDEFINED;
1333 }
1334 if (pdata->mclk_rate == TAIKO_MCLK_CLK_9P6HZ) {
1335 if ((dmic_sample_rate != TAIKO_DMIC_SAMPLE_RATE_2P4MHZ) &&
1336 (dmic_sample_rate != TAIKO_DMIC_SAMPLE_RATE_3P2MHZ) &&
1337 (dmic_sample_rate != TAIKO_DMIC_SAMPLE_RATE_4P8MHZ) &&
1338 (dmic_sample_rate != TAIKO_DMIC_SAMPLE_RATE_UNDEFINED)) {
1339 dev_err(dev, "Invalid dmic rate %d for mclk %d\n",
1340 dmic_sample_rate, pdata->mclk_rate);
1341 ret = -EINVAL;
1342 goto err;
1343 }
1344 } else if (pdata->mclk_rate == TAIKO_MCLK_CLK_12P288MHZ) {
1345 if ((dmic_sample_rate != TAIKO_DMIC_SAMPLE_RATE_3P072MHZ) &&
1346 (dmic_sample_rate != TAIKO_DMIC_SAMPLE_RATE_4P096MHZ) &&
1347 (dmic_sample_rate != TAIKO_DMIC_SAMPLE_RATE_6P144MHZ) &&
1348 (dmic_sample_rate != TAIKO_DMIC_SAMPLE_RATE_UNDEFINED)) {
1349 dev_err(dev, "Invalid dmic rate %d for mclk %d\n",
1350 dmic_sample_rate, pdata->mclk_rate);
1351 ret = -EINVAL;
1352 goto err;
1353 }
1354 }
1355 pdata->dmic_sample_rate = dmic_sample_rate;
Kiran Kandi725f8492012-08-06 13:45:16 -07001356 return pdata;
1357err:
1358 devm_kfree(dev, pdata);
1359 return NULL;
1360}
1361
Joonwoo Park3c5b2df2012-08-28 15:36:55 -07001362static int wcd9xxx_slim_get_laddr(struct slim_device *sb,
1363 const u8 *e_addr, u8 e_len, u8 *laddr)
1364{
1365 int ret;
1366 const unsigned long timeout = jiffies +
1367 msecs_to_jiffies(SLIMBUS_PRESENT_TIMEOUT);
1368
1369 do {
1370 ret = slim_get_logical_addr(sb, e_addr, e_len, laddr);
1371 if (!ret)
1372 break;
1373 /* Give SLIMBUS time to report present and be ready. */
1374 usleep_range(1000, 1000);
1375 pr_debug_ratelimited("%s: retyring get logical addr\n",
1376 __func__);
1377 } while time_before(jiffies, timeout);
1378
1379 return ret;
1380}
1381
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301382static int wcd9xxx_slim_probe(struct slim_device *slim)
1383{
1384 struct wcd9xxx *wcd9xxx;
1385 struct wcd9xxx_pdata *pdata;
1386 int ret = 0;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301387
Venkat Sudhira50a3762012-11-26 12:12:15 -08001388 if (wcd9xxx_intf == WCD9XXX_INTERFACE_TYPE_I2C) {
1389 dev_dbg(&slim->dev, "%s:Codec is detected in I2C mode\n",
1390 __func__);
1391 return -ENODEV;
1392 }
Kiran Kandi725f8492012-08-06 13:45:16 -07001393 if (slim->dev.of_node) {
1394 dev_info(&slim->dev, "Platform data from device tree\n");
1395 pdata = wcd9xxx_populate_dt_pdata(&slim->dev);
Venkat Sudhira41630a2012-10-27 00:57:31 -07001396 ret = wcd9xxx_dt_parse_slim_interface_dev_info(&slim->dev,
1397 &pdata->slimbus_slave_device);
1398 if (ret) {
1399 dev_err(&slim->dev, "Error, parsing slim interface\n");
1400 devm_kfree(&slim->dev, pdata);
1401 ret = -EINVAL;
1402 goto err;
1403 }
Kiran Kandi725f8492012-08-06 13:45:16 -07001404 slim->dev.platform_data = pdata;
1405
1406 } else {
1407 dev_info(&slim->dev, "Platform data from board file\n");
1408 pdata = slim->dev.platform_data;
1409 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301410
1411 if (!pdata) {
1412 dev_err(&slim->dev, "Error, no platform data\n");
1413 ret = -EINVAL;
1414 goto err;
1415 }
1416
1417 wcd9xxx = kzalloc(sizeof(struct wcd9xxx), GFP_KERNEL);
1418 if (wcd9xxx == NULL) {
1419 pr_err("%s: error, allocation failed\n", __func__);
1420 ret = -ENOMEM;
1421 goto err;
1422 }
1423 if (!slim->ctrl) {
1424 pr_err("Error, no SLIMBUS control data\n");
1425 ret = -EINVAL;
1426 goto err_codec;
1427 }
1428 wcd9xxx->slim = slim;
1429 slim_set_clientdata(slim, wcd9xxx);
1430 wcd9xxx->reset_gpio = pdata->reset_gpio;
1431 wcd9xxx->dev = &slim->dev;
Venkat Sudhira50a3762012-11-26 12:12:15 -08001432 wcd9xxx->mclk_rate = pdata->mclk_rate;
Ravishankar Sarawadi839fcf32012-11-14 12:13:00 -08001433 wcd9xxx->slim_device_bootup = true;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301434
Joonwoo Park5d170a42013-04-10 15:16:36 -07001435 ret = wcd9xxx_init_supplies(wcd9xxx, pdata);
1436 if (ret) {
1437 pr_err("%s: Fail to init Codec supplies %d\n", __func__, ret);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301438 goto err_codec;
Joonwoo Park5d170a42013-04-10 15:16:36 -07001439 }
1440 ret = wcd9xxx_enable_static_supplies(wcd9xxx, pdata);
1441 if (ret) {
1442 pr_err("%s: Fail to enable Codec pre-reset supplies\n",
1443 __func__);
1444 goto err_codec;
1445 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301446 usleep_range(5, 5);
1447
1448 ret = wcd9xxx_reset(wcd9xxx);
1449 if (ret) {
1450 pr_err("%s: Resetting Codec failed\n", __func__);
1451 goto err_supplies;
1452 }
1453
Joonwoo Park3c5b2df2012-08-28 15:36:55 -07001454 ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim, wcd9xxx->slim->e_addr,
1455 ARRAY_SIZE(wcd9xxx->slim->e_addr),
1456 &wcd9xxx->slim->laddr);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301457 if (ret) {
Joonwoo Park3c5b2df2012-08-28 15:36:55 -07001458 pr_err("%s: failed to get slimbus %s logical address: %d\n",
1459 __func__, wcd9xxx->slim->name, ret);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301460 goto err_reset;
1461 }
1462 wcd9xxx->read_dev = wcd9xxx_slim_read_device;
1463 wcd9xxx->write_dev = wcd9xxx_slim_write_device;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301464 wcd9xxx_pgd_la = wcd9xxx->slim->laddr;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301465 wcd9xxx->slim_slave = &pdata->slimbus_slave_device;
Joonwoo Parkf6574c72012-10-10 17:29:57 -07001466 if (!wcd9xxx->dev->of_node) {
1467 wcd9xxx->irq = pdata->irq;
1468 wcd9xxx->irq_base = pdata->irq_base;
1469 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301470
1471 ret = slim_add_device(slim->ctrl, wcd9xxx->slim_slave);
1472 if (ret) {
1473 pr_err("%s: error, adding SLIMBUS device failed\n", __func__);
1474 goto err_reset;
1475 }
1476
Joonwoo Park3c5b2df2012-08-28 15:36:55 -07001477 ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim_slave,
1478 wcd9xxx->slim_slave->e_addr,
1479 ARRAY_SIZE(wcd9xxx->slim_slave->e_addr),
1480 &wcd9xxx->slim_slave->laddr);
1481 if (ret) {
1482 pr_err("%s: failed to get slimbus %s logical address: %d\n",
1483 __func__, wcd9xxx->slim->name, ret);
1484 goto err_slim_add;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301485 }
1486 wcd9xxx_inf_la = wcd9xxx->slim_slave->laddr;
1487 wcd9xxx_intf = WCD9XXX_INTERFACE_TYPE_SLIMBUS;
1488
Joonwoo Parkf6574c72012-10-10 17:29:57 -07001489 ret = wcd9xxx_device_init(wcd9xxx);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301490 if (ret) {
1491 pr_err("%s: error, initializing device failed\n", __func__);
1492 goto err_slim_add;
1493 }
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301494#ifdef CONFIG_DEBUG_FS
1495 debugCodec = wcd9xxx;
1496
1497 debugfs_wcd9xxx_dent = debugfs_create_dir
1498 ("wcd9310_slimbus_interface_device", 0);
1499 if (!IS_ERR(debugfs_wcd9xxx_dent)) {
1500 debugfs_peek = debugfs_create_file("peek",
1501 S_IFREG | S_IRUGO, debugfs_wcd9xxx_dent,
1502 (void *) "peek", &codec_debug_ops);
1503
1504 debugfs_poke = debugfs_create_file("poke",
1505 S_IFREG | S_IRUGO, debugfs_wcd9xxx_dent,
1506 (void *) "poke", &codec_debug_ops);
1507 }
1508#endif
1509
1510 return ret;
1511
1512err_slim_add:
1513 slim_remove_device(wcd9xxx->slim_slave);
1514err_reset:
1515 wcd9xxx_free_reset(wcd9xxx);
1516err_supplies:
Venkat Sudhir49203862012-05-21 14:29:13 -07001517 wcd9xxx_disable_supplies(wcd9xxx, pdata);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301518err_codec:
1519 kfree(wcd9xxx);
1520err:
1521 return ret;
1522}
1523static int wcd9xxx_slim_remove(struct slim_device *pdev)
1524{
1525 struct wcd9xxx *wcd9xxx;
Venkat Sudhir49203862012-05-21 14:29:13 -07001526 struct wcd9xxx_pdata *pdata = pdev->dev.platform_data;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301527
1528#ifdef CONFIG_DEBUG_FS
1529 debugfs_remove(debugfs_peek);
1530 debugfs_remove(debugfs_poke);
1531 debugfs_remove(debugfs_wcd9xxx_dent);
1532#endif
1533 wcd9xxx = slim_get_devicedata(pdev);
1534 wcd9xxx_deinit_slimslave(wcd9xxx);
1535 slim_remove_device(wcd9xxx->slim_slave);
Venkat Sudhir49203862012-05-21 14:29:13 -07001536 wcd9xxx_disable_supplies(wcd9xxx, pdata);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301537 wcd9xxx_device_exit(wcd9xxx);
1538 return 0;
1539}
1540
1541static int wcd9xxx_resume(struct wcd9xxx *wcd9xxx)
1542{
1543 int ret = 0;
1544
1545 pr_debug("%s: enter\n", __func__);
1546 mutex_lock(&wcd9xxx->pm_lock);
1547 if (wcd9xxx->pm_state == WCD9XXX_PM_ASLEEP) {
1548 pr_debug("%s: resuming system, state %d, wlock %d\n", __func__,
1549 wcd9xxx->pm_state, wcd9xxx->wlock_holders);
1550 wcd9xxx->pm_state = WCD9XXX_PM_SLEEPABLE;
1551 } else {
1552 pr_warn("%s: system is already awake, state %d wlock %d\n",
1553 __func__, wcd9xxx->pm_state, wcd9xxx->wlock_holders);
1554 }
1555 mutex_unlock(&wcd9xxx->pm_lock);
1556 wake_up_all(&wcd9xxx->pm_wq);
1557
1558 return ret;
1559}
1560
Ravishankar Sarawadi839fcf32012-11-14 12:13:00 -08001561static int wcd9xxx_device_up(struct wcd9xxx *wcd9xxx)
1562{
1563 int ret = 0;
1564
1565 if (wcd9xxx->slim_device_bootup) {
1566 wcd9xxx->slim_device_bootup = false;
1567 return 0;
1568 }
1569 ret = wcd9xxx_reset(wcd9xxx);
1570 if (ret)
1571 pr_err("%s: Resetting Codec failed\n", __func__);
1572
1573 wcd9xxx_bring_up(wcd9xxx);
1574 wcd9xxx->post_reset(wcd9xxx);
1575 return ret;
1576}
1577
1578static int wcd9xxx_slim_device_up(struct slim_device *sldev)
1579{
1580 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1581 return wcd9xxx_device_up(wcd9xxx);
1582}
1583
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301584static int wcd9xxx_slim_resume(struct slim_device *sldev)
1585{
1586 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1587 return wcd9xxx_resume(wcd9xxx);
1588}
1589
1590static int wcd9xxx_i2c_resume(struct i2c_client *i2cdev)
1591{
1592 struct wcd9xxx *wcd9xxx = dev_get_drvdata(&i2cdev->dev);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301593 if (wcd9xxx)
1594 return wcd9xxx_resume(wcd9xxx);
1595 else
1596 return 0;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301597}
1598
1599static int wcd9xxx_suspend(struct wcd9xxx *wcd9xxx, pm_message_t pmesg)
1600{
1601 int ret = 0;
1602
1603 pr_debug("%s: enter\n", __func__);
Stephen Boyd2fcabf92012-05-30 10:41:11 -07001604 /*
1605 * pm_qos_update_request() can be called after this suspend chain call
1606 * started. thus suspend can be called while lock is being held
1607 */
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301608 mutex_lock(&wcd9xxx->pm_lock);
1609 if (wcd9xxx->pm_state == WCD9XXX_PM_SLEEPABLE) {
1610 pr_debug("%s: suspending system, state %d, wlock %d\n",
1611 __func__, wcd9xxx->pm_state, wcd9xxx->wlock_holders);
1612 wcd9xxx->pm_state = WCD9XXX_PM_ASLEEP;
1613 } else if (wcd9xxx->pm_state == WCD9XXX_PM_AWAKE) {
1614 /* unlock to wait for pm_state == WCD9XXX_PM_SLEEPABLE
1615 * then set to WCD9XXX_PM_ASLEEP */
1616 pr_debug("%s: waiting to suspend system, state %d, wlock %d\n",
1617 __func__, wcd9xxx->pm_state, wcd9xxx->wlock_holders);
1618 mutex_unlock(&wcd9xxx->pm_lock);
1619 if (!(wait_event_timeout(wcd9xxx->pm_wq,
1620 wcd9xxx_pm_cmpxchg(wcd9xxx,
1621 WCD9XXX_PM_SLEEPABLE,
1622 WCD9XXX_PM_ASLEEP) ==
1623 WCD9XXX_PM_SLEEPABLE,
1624 HZ))) {
1625 pr_debug("%s: suspend failed state %d, wlock %d\n",
1626 __func__, wcd9xxx->pm_state,
1627 wcd9xxx->wlock_holders);
1628 ret = -EBUSY;
1629 } else {
1630 pr_debug("%s: done, state %d, wlock %d\n", __func__,
1631 wcd9xxx->pm_state, wcd9xxx->wlock_holders);
1632 }
1633 mutex_lock(&wcd9xxx->pm_lock);
1634 } else if (wcd9xxx->pm_state == WCD9XXX_PM_ASLEEP) {
1635 pr_warn("%s: system is already suspended, state %d, wlock %dn",
1636 __func__, wcd9xxx->pm_state, wcd9xxx->wlock_holders);
1637 }
1638 mutex_unlock(&wcd9xxx->pm_lock);
1639
1640 return ret;
1641}
1642
1643static int wcd9xxx_slim_suspend(struct slim_device *sldev, pm_message_t pmesg)
1644{
1645 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1646 return wcd9xxx_suspend(wcd9xxx, pmesg);
1647}
1648
1649static int wcd9xxx_i2c_suspend(struct i2c_client *i2cdev, pm_message_t pmesg)
1650{
1651 struct wcd9xxx *wcd9xxx = dev_get_drvdata(&i2cdev->dev);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301652 if (wcd9xxx)
1653 return wcd9xxx_suspend(wcd9xxx, pmesg);
1654 else
1655 return 0;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301656}
1657
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301658static const struct slim_device_id sitar_slimtest_id[] = {
1659 {"sitar-slim", 0},
1660 {}
1661};
1662static struct slim_driver sitar_slim_driver = {
1663 .driver = {
1664 .name = "sitar-slim",
1665 .owner = THIS_MODULE,
1666 },
1667 .probe = wcd9xxx_slim_probe,
1668 .remove = wcd9xxx_slim_remove,
1669 .id_table = sitar_slimtest_id,
1670 .resume = wcd9xxx_slim_resume,
1671 .suspend = wcd9xxx_slim_suspend,
1672};
1673
Bhalchandra Gajare83c81f62012-05-18 16:09:05 -07001674static const struct slim_device_id sitar1p1_slimtest_id[] = {
1675 {"sitar1p1-slim", 0},
1676 {}
1677};
1678static struct slim_driver sitar1p1_slim_driver = {
1679 .driver = {
1680 .name = "sitar1p1-slim",
1681 .owner = THIS_MODULE,
1682 },
1683 .probe = wcd9xxx_slim_probe,
1684 .remove = wcd9xxx_slim_remove,
1685 .id_table = sitar1p1_slimtest_id,
1686 .resume = wcd9xxx_slim_resume,
1687 .suspend = wcd9xxx_slim_suspend,
1688};
1689
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301690static const struct slim_device_id slimtest_id[] = {
1691 {"tabla-slim", 0},
1692 {}
1693};
1694
1695static struct slim_driver tabla_slim_driver = {
1696 .driver = {
1697 .name = "tabla-slim",
1698 .owner = THIS_MODULE,
1699 },
1700 .probe = wcd9xxx_slim_probe,
1701 .remove = wcd9xxx_slim_remove,
1702 .id_table = slimtest_id,
1703 .resume = wcd9xxx_slim_resume,
1704 .suspend = wcd9xxx_slim_suspend,
1705};
1706
1707static const struct slim_device_id slimtest2x_id[] = {
1708 {"tabla2x-slim", 0},
1709 {}
1710};
1711
1712static struct slim_driver tabla2x_slim_driver = {
1713 .driver = {
1714 .name = "tabla2x-slim",
1715 .owner = THIS_MODULE,
1716 },
1717 .probe = wcd9xxx_slim_probe,
1718 .remove = wcd9xxx_slim_remove,
1719 .id_table = slimtest2x_id,
1720 .resume = wcd9xxx_slim_resume,
1721 .suspend = wcd9xxx_slim_suspend,
1722};
1723
Joonwoo Parka7172112012-07-23 16:03:49 -07001724static const struct slim_device_id taiko_slimtest_id[] = {
Kiran Kandi725f8492012-08-06 13:45:16 -07001725 {"taiko-slim-pgd", 0},
Joonwoo Parka7172112012-07-23 16:03:49 -07001726 {}
1727};
1728
1729static struct slim_driver taiko_slim_driver = {
1730 .driver = {
1731 .name = "taiko-slim",
1732 .owner = THIS_MODULE,
1733 },
1734 .probe = wcd9xxx_slim_probe,
1735 .remove = wcd9xxx_slim_remove,
1736 .id_table = taiko_slimtest_id,
1737 .resume = wcd9xxx_slim_resume,
1738 .suspend = wcd9xxx_slim_suspend,
Ravishankar Sarawadi839fcf32012-11-14 12:13:00 -08001739 .device_up = wcd9xxx_slim_device_up,
Joonwoo Parka7172112012-07-23 16:03:49 -07001740};
1741
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001742static const struct slim_device_id tapan_slimtest_id[] = {
1743 {"tapan-slim-pgd", 0},
1744 {}
1745};
1746
1747static struct slim_driver tapan_slim_driver = {
1748 .driver = {
1749 .name = "tapan-slim",
1750 .owner = THIS_MODULE,
1751 },
1752 .probe = wcd9xxx_slim_probe,
1753 .remove = wcd9xxx_slim_remove,
1754 .id_table = tapan_slimtest_id,
1755 .resume = wcd9xxx_slim_resume,
1756 .suspend = wcd9xxx_slim_suspend,
Ravishankar Sarawadid6273212013-05-07 15:08:27 -07001757 .device_up = wcd9xxx_slim_device_up,
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001758};
1759
Venkat Sudhira41630a2012-10-27 00:57:31 -07001760static struct i2c_device_id wcd9xxx_id_table[] = {
1761 {"wcd9xxx-i2c", WCD9XXX_I2C_TOP_LEVEL},
1762 {"wcd9xxx-i2c", WCD9XXX_I2C_ANALOG},
1763 {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_1},
1764 {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_2},
1765 {}
1766};
1767
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301768static struct i2c_device_id tabla_id_table[] = {
Asish Bhattacharya2b709d42011-11-15 10:39:23 +05301769 {"tabla top level", WCD9XXX_I2C_TOP_LEVEL},
1770 {"tabla analog", WCD9XXX_I2C_ANALOG},
1771 {"tabla digital1", WCD9XXX_I2C_DIGITAL_1},
1772 {"tabla digital2", WCD9XXX_I2C_DIGITAL_2},
1773 {}
1774};
1775MODULE_DEVICE_TABLE(i2c, tabla_id_table);
1776
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301777static struct i2c_driver tabla_i2c_driver = {
1778 .driver = {
1779 .owner = THIS_MODULE,
1780 .name = "tabla-i2c-core",
1781 },
1782 .id_table = tabla_id_table,
1783 .probe = wcd9xxx_i2c_probe,
1784 .remove = __devexit_p(wcd9xxx_i2c_remove),
1785 .resume = wcd9xxx_i2c_resume,
1786 .suspend = wcd9xxx_i2c_suspend,
1787};
1788
Venkat Sudhira41630a2012-10-27 00:57:31 -07001789static struct i2c_driver wcd9xxx_i2c_driver = {
1790 .driver = {
1791 .owner = THIS_MODULE,
1792 .name = "wcd9xxx-i2c-core",
1793 },
1794 .id_table = wcd9xxx_id_table,
1795 .probe = wcd9xxx_i2c_probe,
1796 .remove = __devexit_p(wcd9xxx_i2c_remove),
1797 .resume = wcd9xxx_i2c_resume,
1798 .suspend = wcd9xxx_i2c_suspend,
1799};
1800
1801
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301802static int __init wcd9xxx_init(void)
1803{
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001804 int ret[NUM_WCD9XXX_REG_RET];
1805 int i = 0;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301806
Venkat Sudhira50a3762012-11-26 12:12:15 -08001807 wcd9xxx_intf = WCD9XXX_INTERFACE_TYPE_PROBING;
1808
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001809 ret[0] = slim_driver_register(&tabla_slim_driver);
1810 if (ret[0])
1811 pr_err("Failed to register tabla SB driver: %d\n", ret[0]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301812
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001813 ret[1] = slim_driver_register(&tabla2x_slim_driver);
1814 if (ret[1])
1815 pr_err("Failed to register tabla2x SB driver: %d\n", ret[1]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301816
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001817 ret[2] = i2c_add_driver(&tabla_i2c_driver);
1818 if (ret[2])
1819 pr_err("failed to add the tabla2x I2C driver: %d\n", ret[2]);
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301820
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001821 ret[3] = slim_driver_register(&sitar_slim_driver);
1822 if (ret[3])
1823 pr_err("Failed to register sitar SB driver: %d\n", ret[3]);
Asish Bhattacharyab86c3472012-02-15 08:31:52 +05301824
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001825 ret[4] = slim_driver_register(&sitar1p1_slim_driver);
1826 if (ret[4])
1827 pr_err("Failed to register sitar SB driver: %d\n", ret[4]);
Bhalchandra Gajare83c81f62012-05-18 16:09:05 -07001828
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001829 ret[5] = slim_driver_register(&taiko_slim_driver);
1830 if (ret[5])
1831 pr_err("Failed to register taiko SB driver: %d\n", ret[5]);
Joonwoo Parka7172112012-07-23 16:03:49 -07001832
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001833 ret[6] = i2c_add_driver(&wcd9xxx_i2c_driver);
1834 if (ret[6])
1835 pr_err("failed to add the wcd9xxx I2C driver: %d\n", ret[6]);
Venkat Sudhira41630a2012-10-27 00:57:31 -07001836
Banajit Goswami4d6d891b2012-12-12 23:59:07 -08001837 ret[7] = slim_driver_register(&tapan_slim_driver);
1838 if (ret[7])
1839 pr_err("Failed to register tapan SB driver: %d\n", ret[7]);
1840
1841 for (i = 0; i < NUM_WCD9XXX_REG_RET; i++) {
1842 if (ret[i])
1843 return ret[i];
1844 }
1845 return 0;
Asish Bhattacharyab1aeae22012-02-15 08:29:28 +05301846}
1847module_init(wcd9xxx_init);
1848
1849static void __exit wcd9xxx_exit(void)
1850{
1851}
1852module_exit(wcd9xxx_exit);
1853
1854MODULE_DESCRIPTION("Codec core driver");
1855MODULE_VERSION("1.0");
1856MODULE_LICENSE("GPL v2");