blob: a12d5cad3b2edff5b94fe7b676e1e4d153cbf80b [file] [log] [blame]
Banajit Goswamide8271c2017-01-18 00:28:59 -08001/* Copyright (c) 2011-2017, The Linux Foundation. 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/of_device.h>
16#include <linux/slab.h>
17#include <linux/ratelimit.h>
18#include <linux/mfd/core.h>
19#include <linux/mfd/wcd9xxx/wcd9xxx-slimslave.h>
20#include <linux/mfd/wcd9xxx/core.h>
21#include <linux/mfd/wcd9xxx/wcd9xxx-irq.h>
22#include <linux/mfd/wcd9xxx/pdata.h>
23#include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
24#include <linux/mfd/msm-cdc-pinctrl.h>
25#include <linux/mfd/wcd9xxx/wcd9xxx-utils.h>
26#include <linux/mfd/msm-cdc-supply.h>
27#include <linux/delay.h>
28#include <linux/gpio.h>
29#include <linux/debugfs.h>
30#include <linux/i2c.h>
31#include <linux/regmap.h>
32#include <sound/soc.h>
33#include "wcd9xxx-regmap.h"
34
35#define WCD9XXX_REGISTER_START_OFFSET 0x800
36#define WCD9XXX_SLIM_RW_MAX_TRIES 3
37#define SLIMBUS_PRESENT_TIMEOUT 100
38
39#define MAX_WCD9XXX_DEVICE 4
40#define WCD9XXX_I2C_GSBI_SLAVE_ID "3-000d"
41#define WCD9XXX_I2C_TOP_SLAVE_ADDR 0x0d
42#define WCD9XXX_ANALOG_I2C_SLAVE_ADDR 0x77
43#define WCD9XXX_DIGITAL1_I2C_SLAVE_ADDR 0x66
44#define WCD9XXX_DIGITAL2_I2C_SLAVE_ADDR 0x55
45#define WCD9XXX_I2C_TOP_LEVEL 0
46#define WCD9XXX_I2C_ANALOG 1
47#define WCD9XXX_I2C_DIGITAL_1 2
48#define WCD9XXX_I2C_DIGITAL_2 3
49
50/*
51 * 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 4
55
56#define SLIM_USR_MC_REPEAT_CHANGE_VALUE 0x0
57#define SLIM_REPEAT_WRITE_MAX_SLICE 16
58#define REG_BYTES 2
59#define VAL_BYTES 1
60#define WCD9XXX_PAGE_NUM(reg) (((reg) >> 8) & 0xff)
61#define WCD9XXX_PAGE_SIZE 256
62
63struct wcd9xxx_i2c {
64 struct i2c_client *client;
65 struct i2c_msg xfer_msg[2];
66 struct mutex xfer_lock;
67 int mod_id;
68};
69
70static struct regmap_config wcd9xxx_base_regmap_config = {
71 .reg_bits = 16,
72 .val_bits = 8,
73 .can_multi_write = true,
74};
75
76static struct regmap_config wcd9xxx_i2c_base_regmap_config = {
77 .reg_bits = 16,
78 .val_bits = 8,
79 .can_multi_write = false,
80 .use_single_rw = true,
81};
82
83static u8 wcd9xxx_pgd_la;
84static u8 wcd9xxx_inf_la;
85
86static const int wcd9xxx_cdc_types[] = {
87 [WCD9XXX] = WCD9XXX,
88 [WCD9330] = WCD9330,
89 [WCD9335] = WCD9335,
90 [WCD934X] = WCD934X,
91};
92
93static const struct of_device_id wcd9xxx_of_match[] = {
94 { .compatible = "qcom,tasha-i2c-pgd",
95 .data = (void *)&wcd9xxx_cdc_types[WCD9335]},
96 { .compatible = "qcom,wcd9xxx-i2c",
97 .data = (void *)&wcd9xxx_cdc_types[WCD9330]},
98 { }
99};
100MODULE_DEVICE_TABLE(of, wcd9xxx_of_match);
101
102static int wcd9xxx_slim_device_up(struct slim_device *sldev);
103static int wcd9xxx_slim_device_down(struct slim_device *sldev);
104
105struct wcd9xxx_i2c wcd9xxx_modules[MAX_WCD9XXX_DEVICE];
106
107static int wcd9xxx_slim_multi_reg_write(struct wcd9xxx *wcd9xxx,
108 const void *data, size_t count)
109{
110 unsigned int reg;
111 struct device *dev;
112 u8 val[WCD9XXX_PAGE_SIZE];
113 int ret = 0;
114 int i = 0;
115 int n = 0;
116 unsigned int page_num;
117 size_t num_regs = (count / (REG_BYTES + VAL_BYTES));
118 struct wcd9xxx_reg_val *bulk_reg;
119 u8 *buf;
120
121 dev = wcd9xxx->dev;
122 if (!data) {
123 dev_err(dev, "%s: data is NULL\n", __func__);
124 return -EINVAL;
125 }
126 if (num_regs == 0)
127 return -EINVAL;
128
129 bulk_reg = kzalloc(num_regs * (sizeof(struct wcd9xxx_reg_val)),
130 GFP_KERNEL);
131 if (!bulk_reg)
132 return -ENOMEM;
133
134 buf = (u8 *)data;
135 reg = *(u16 *)buf;
136 page_num = WCD9XXX_PAGE_NUM(reg);
137 for (i = 0, n = 0; n < num_regs; i++, n++) {
138 reg = *(u16 *)buf;
139 if (page_num != WCD9XXX_PAGE_NUM(reg)) {
140 ret = wcd9xxx_slim_bulk_write(wcd9xxx, bulk_reg,
141 i, false);
142 page_num = WCD9XXX_PAGE_NUM(reg);
143 i = 0;
144 }
145 buf += REG_BYTES;
146 val[i] = *buf;
147 buf += VAL_BYTES;
148 bulk_reg[i].reg = reg;
149 bulk_reg[i].buf = &val[i];
150 bulk_reg[i].bytes = 1;
151 }
152 ret = wcd9xxx_slim_bulk_write(wcd9xxx, bulk_reg,
153 i, false);
154 if (ret)
155 dev_err(dev, "%s: error writing bulk regs\n",
156 __func__);
157
158 kfree(bulk_reg);
159 return ret;
160}
161
162/*
163 * wcd9xxx_interface_reg_read: Read slim interface registers
164 *
165 * @wcd9xxx: Pointer to wcd9xxx structure
166 * @reg: register adderss
167 *
168 * Returns register value in success and negative error code in case of failure
169 */
170int wcd9xxx_interface_reg_read(struct wcd9xxx *wcd9xxx, unsigned short reg)
171{
172 u8 val;
173 int ret;
174
175 mutex_lock(&wcd9xxx->io_lock);
176 ret = wcd9xxx->read_dev(wcd9xxx, reg, 1, (void *)&val,
177 true);
178 if (ret < 0)
179 dev_err(wcd9xxx->dev, "%s: Codec read 0x%x failed\n",
180 __func__, reg);
181 else
182 dev_dbg(wcd9xxx->dev, "%s: Read 0x%02x from 0x%x\n",
183 __func__, val, reg);
184
185 mutex_unlock(&wcd9xxx->io_lock);
186
187 if (ret < 0)
188 return ret;
189 else
190 return val;
191}
192EXPORT_SYMBOL(wcd9xxx_interface_reg_read);
193
194/*
195 * wcd9xxx_interface_reg_write: Write slim interface registers
196 *
197 * @wcd9xxx: Pointer to wcd9xxx structure
198 * @reg: register adderss
199 * @val: value of the register to be written
200 *
201 * Returns 0 for success and negative error code in case of failure
202 */
203int wcd9xxx_interface_reg_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
204 u8 val)
205{
206 int ret;
207
208 mutex_lock(&wcd9xxx->io_lock);
209 ret = wcd9xxx->write_dev(wcd9xxx, reg, 1, (void *)&val, true);
210 dev_dbg(wcd9xxx->dev, "%s: Write %02x to 0x%x ret(%d)\n",
211 __func__, val, reg, ret);
212 mutex_unlock(&wcd9xxx->io_lock);
213
214 return ret;
215}
216EXPORT_SYMBOL(wcd9xxx_interface_reg_write);
217
218static int wcd9xxx_slim_read_device(struct wcd9xxx *wcd9xxx, unsigned short reg,
219 int bytes, void *dest, bool interface)
220{
221 int ret;
222 struct slim_ele_access msg;
223 int slim_read_tries = WCD9XXX_SLIM_RW_MAX_TRIES;
224
225 msg.start_offset = WCD9XXX_REGISTER_START_OFFSET + reg;
226 msg.num_bytes = bytes;
227 msg.comp = NULL;
228
229 if (!wcd9xxx->dev_up) {
230 dev_dbg_ratelimited(
Ramprasad Katkam40bc50d2017-05-19 13:21:28 +0530231 wcd9xxx->dev, "%s: No read allowed. dev_up = %lu\n",
Banajit Goswamide8271c2017-01-18 00:28:59 -0800232 __func__, wcd9xxx->dev_up);
233 return 0;
234 }
235
236 while (1) {
237 mutex_lock(&wcd9xxx->xfer_lock);
238 ret = slim_request_val_element(interface ?
239 wcd9xxx->slim_slave : wcd9xxx->slim,
240 &msg, dest, bytes);
241 mutex_unlock(&wcd9xxx->xfer_lock);
242 if (likely(ret == 0) || (--slim_read_tries == 0))
243 break;
244 usleep_range(5000, 5100);
245 }
246
247 if (ret)
248 dev_err(wcd9xxx->dev, "%s: Error, Codec read failed (%d)\n",
249 __func__, ret);
250
251 return ret;
252}
253
254/*
255 * Interface specifies whether the write is to the interface or general
256 * registers.
257 */
258static int wcd9xxx_slim_write_device(struct wcd9xxx *wcd9xxx,
259 unsigned short reg, int bytes, void *src, bool interface)
260{
261 int ret;
262 struct slim_ele_access msg;
263 int slim_write_tries = WCD9XXX_SLIM_RW_MAX_TRIES;
264
265 msg.start_offset = WCD9XXX_REGISTER_START_OFFSET + reg;
266 msg.num_bytes = bytes;
267 msg.comp = NULL;
268
269 if (!wcd9xxx->dev_up) {
270 dev_dbg_ratelimited(
Ramprasad Katkam40bc50d2017-05-19 13:21:28 +0530271 wcd9xxx->dev, "%s: No write allowed. dev_up = %lu\n",
Banajit Goswamide8271c2017-01-18 00:28:59 -0800272 __func__, wcd9xxx->dev_up);
273 return 0;
274 }
275
276 while (1) {
277 mutex_lock(&wcd9xxx->xfer_lock);
278 ret = slim_change_val_element(interface ?
279 wcd9xxx->slim_slave : wcd9xxx->slim,
280 &msg, src, bytes);
281 mutex_unlock(&wcd9xxx->xfer_lock);
282 if (likely(ret == 0) || (--slim_write_tries == 0))
283 break;
284 usleep_range(5000, 5100);
285 }
286
287 if (ret)
288 pr_err("%s: Error, Codec write failed (%d)\n", __func__, ret);
289
290 return ret;
291}
292
293static int wcd9xxx_slim_get_allowed_slice(struct wcd9xxx *wcd9xxx,
294 int bytes)
295{
296 int allowed_sz = bytes;
297
298 if (likely(bytes == SLIM_REPEAT_WRITE_MAX_SLICE))
299 allowed_sz = 16;
300 else if (bytes >= 12)
301 allowed_sz = 12;
302 else if (bytes >= 8)
303 allowed_sz = 8;
304 else if (bytes >= 6)
305 allowed_sz = 6;
306 else if (bytes >= 4)
307 allowed_sz = 4;
308 else
309 allowed_sz = bytes;
310
311 return allowed_sz;
312}
313
314/*
315 * wcd9xxx_slim_write_repeat: Write the same register with multiple values
316 * @wcd9xxx: handle to wcd core
317 * @reg: register to be written
318 * @bytes: number of bytes to be written to reg
319 * @src: buffer with data content to be written to reg
320 * This API will write reg with bytes from src in a single slimbus
321 * transaction. All values from 1 to 16 are supported by this API.
322 */
323int wcd9xxx_slim_write_repeat(struct wcd9xxx *wcd9xxx, unsigned short reg,
324 int bytes, void *src)
325{
326 int ret = 0, bytes_to_write = bytes, bytes_allowed;
327 struct slim_ele_access slim_msg;
328
329 mutex_lock(&wcd9xxx->io_lock);
330 if (wcd9xxx->type == WCD9335 || wcd9xxx->type == WCD934X) {
331 ret = wcd9xxx_page_write(wcd9xxx, &reg);
332 if (ret)
333 goto done;
334 }
335
336 slim_msg.start_offset = WCD9XXX_REGISTER_START_OFFSET + reg;
337 slim_msg.comp = NULL;
338
339 if (unlikely(bytes > SLIM_REPEAT_WRITE_MAX_SLICE)) {
340 dev_err(wcd9xxx->dev, "%s: size %d not supported\n",
341 __func__, bytes);
342 ret = -EINVAL;
343 goto done;
344 }
345
346 if (!wcd9xxx->dev_up) {
347 dev_dbg_ratelimited(
Ramprasad Katkam40bc50d2017-05-19 13:21:28 +0530348 wcd9xxx->dev, "%s: No write allowed. dev_up = %lu\n",
Banajit Goswamide8271c2017-01-18 00:28:59 -0800349 __func__, wcd9xxx->dev_up);
350 ret = 0;
351 goto done;
352 }
353
354 while (bytes_to_write > 0) {
355 bytes_allowed = wcd9xxx_slim_get_allowed_slice(wcd9xxx,
356 bytes_to_write);
357
358 slim_msg.num_bytes = bytes_allowed;
359 mutex_lock(&wcd9xxx->xfer_lock);
360 ret = slim_user_msg(wcd9xxx->slim, wcd9xxx->slim->laddr,
361 SLIM_MSG_MT_DEST_REFERRED_USER,
362 SLIM_USR_MC_REPEAT_CHANGE_VALUE,
363 &slim_msg, src, bytes_allowed);
364 mutex_unlock(&wcd9xxx->xfer_lock);
365
366 if (ret) {
367 dev_err(wcd9xxx->dev, "%s: failed, ret = %d\n",
368 __func__, ret);
369 break;
370 }
371
372 bytes_to_write = bytes_to_write - bytes_allowed;
373 src = ((u8 *)src) + bytes_allowed;
374 }
375
376done:
377 mutex_unlock(&wcd9xxx->io_lock);
378
379 return ret;
380}
381EXPORT_SYMBOL(wcd9xxx_slim_write_repeat);
382
383/*
384 * wcd9xxx_slim_reserve_bw: API to reserve the slimbus bandwidth
385 * @wcd9xxx: Handle to the wcd9xxx core
386 * @bw_ops: value of the bandwidth that is requested
387 * @commit: Flag to indicate if bandwidth change is to be committed
388 * right away
389 */
390int wcd9xxx_slim_reserve_bw(struct wcd9xxx *wcd9xxx,
391 u32 bw_ops, bool commit)
392{
393 if (!wcd9xxx || !wcd9xxx->slim) {
394 pr_err("%s: Invalid handle to %s\n",
395 __func__,
396 (!wcd9xxx) ? "wcd9xxx" : "slim_device");
397 return -EINVAL;
398 }
399
400 return slim_reservemsg_bw(wcd9xxx->slim, bw_ops, commit);
401}
402EXPORT_SYMBOL(wcd9xxx_slim_reserve_bw);
403
404/*
405 * wcd9xxx_slim_bulk_write: API to write multiple registers with one descriptor
406 * @wcd9xxx: Handle to the wcd9xxx core
407 * @wcd9xxx_reg_val: structure holding register and values to be written
408 * @size: Indicates number of messages to be written with one descriptor
409 * @is_interface: Indicates whether the register is for slim interface or for
410 * general registers.
411 * @return: returns 0 if success or error information to the caller in case
412 * of failure.
413 */
414int wcd9xxx_slim_bulk_write(struct wcd9xxx *wcd9xxx,
415 struct wcd9xxx_reg_val *bulk_reg,
416 unsigned int size, bool is_interface)
417{
418 int ret, i;
419 struct slim_val_inf *msgs;
420 unsigned short reg;
421
422 if (!bulk_reg || !size || !wcd9xxx) {
423 pr_err("%s: Invalid parameters\n", __func__);
424 return -EINVAL;
425 }
426
427 if (!wcd9xxx->dev_up) {
428 dev_dbg_ratelimited(
Ramprasad Katkam40bc50d2017-05-19 13:21:28 +0530429 wcd9xxx->dev, "%s: No write allowed. dev_up = %lu\n",
Banajit Goswamide8271c2017-01-18 00:28:59 -0800430 __func__, wcd9xxx->dev_up);
431 return 0;
432 }
433
434 msgs = kzalloc(size * (sizeof(struct slim_val_inf)), GFP_KERNEL);
435 if (!msgs) {
436 ret = -ENOMEM;
437 goto mem_fail;
438 }
439
440 mutex_lock(&wcd9xxx->io_lock);
441 reg = bulk_reg->reg;
442 for (i = 0; i < size; i++) {
443 msgs[i].start_offset = WCD9XXX_REGISTER_START_OFFSET +
444 (bulk_reg->reg & 0xFF);
445 msgs[i].num_bytes = bulk_reg->bytes;
446 msgs[i].wbuf = bulk_reg->buf;
447 bulk_reg++;
448 }
449 ret = wcd9xxx_page_write(wcd9xxx, &reg);
450 if (ret) {
451 pr_err("%s: Page write error for reg: 0x%x\n",
452 __func__, reg);
453 goto err;
454 }
455
456 ret = slim_bulk_msg_write(is_interface ?
457 wcd9xxx->slim_slave : wcd9xxx->slim,
458 SLIM_MSG_MT_CORE,
459 SLIM_MSG_MC_CHANGE_VALUE, msgs, size,
460 NULL, NULL);
461 if (ret)
462 pr_err("%s: Error, Codec bulk write failed (%d)\n",
463 __func__, ret);
464 /* 100 usec sleep is needed as per HW requirement */
465 usleep_range(100, 110);
466err:
467 mutex_unlock(&wcd9xxx->io_lock);
468 kfree(msgs);
469mem_fail:
470 return ret;
471}
472EXPORT_SYMBOL(wcd9xxx_slim_bulk_write);
473
474static int wcd9xxx_num_irq_regs(const struct wcd9xxx *wcd9xxx)
475{
476 return (wcd9xxx->codec_type->num_irqs / 8) +
477 ((wcd9xxx->codec_type->num_irqs % 8) ? 1 : 0);
478}
479
480static int wcd9xxx_regmap_init_cache(struct wcd9xxx *wcd9xxx)
481{
482 struct regmap_config *regmap_config;
483 int rc;
484
485 regmap_config = wcd9xxx_get_regmap_config(wcd9xxx->type);
486 if (!regmap_config) {
487 dev_err(wcd9xxx->dev, "regmap config is not defined\n");
488 return -EINVAL;
489 }
490
491 rc = regmap_reinit_cache(wcd9xxx->regmap, regmap_config);
492 if (rc != 0) {
493 dev_err(wcd9xxx->dev, "%s:Failed to reinit register cache: %d\n",
494 __func__, rc);
495 }
496
497 return rc;
498}
499
500static int wcd9xxx_device_init(struct wcd9xxx *wcd9xxx)
501{
502 int ret = 0, i;
503 struct wcd9xxx_core_resource *core_res = &wcd9xxx->core_res;
504 regmap_patch_fptr regmap_apply_patch = NULL;
505
506 mutex_init(&wcd9xxx->io_lock);
507 mutex_init(&wcd9xxx->xfer_lock);
Vidyakumar Athota6258e3c2017-01-04 11:46:17 -0800508 mutex_init(&wcd9xxx->reset_lock);
Banajit Goswamide8271c2017-01-18 00:28:59 -0800509
510 ret = wcd9xxx_bringup(wcd9xxx->dev);
511 if (ret) {
512 ret = -EPROBE_DEFER;
513 goto err_bring_up;
514 }
515
516 wcd9xxx->codec_type = devm_kzalloc(wcd9xxx->dev,
517 sizeof(struct wcd9xxx_codec_type), GFP_KERNEL);
518 if (!wcd9xxx->codec_type) {
519 ret = -ENOMEM;
520 goto err_bring_up;
521 }
522 ret = wcd9xxx_get_codec_info(wcd9xxx->dev);
523 if (ret) {
524 ret = -EPROBE_DEFER;
525 goto fail_cdc_fill;
526 }
527 wcd9xxx->version = wcd9xxx->codec_type->version;
528 if (!wcd9xxx->codec_type->dev || !wcd9xxx->codec_type->size)
529 goto fail_cdc_fill;
530
531 core_res->parent = wcd9xxx;
532 core_res->dev = wcd9xxx->dev;
533 core_res->intr_table = wcd9xxx->codec_type->intr_tbl;
534 core_res->intr_table_size = wcd9xxx->codec_type->intr_tbl_size;
535
536 for (i = 0; i < WCD9XXX_INTR_REG_MAX; i++)
537 wcd9xxx->core_res.intr_reg[i] =
538 wcd9xxx->codec_type->intr_reg[i];
539
540 wcd9xxx_core_res_init(&wcd9xxx->core_res,
541 wcd9xxx->codec_type->num_irqs,
542 wcd9xxx_num_irq_regs(wcd9xxx),
543 wcd9xxx->regmap);
544
545 if (wcd9xxx_core_irq_init(&wcd9xxx->core_res))
546 goto err;
547
548 ret = wcd9xxx_regmap_init_cache(wcd9xxx);
549 if (ret)
550 goto err_irq;
551
552 regmap_apply_patch = wcd9xxx_get_regmap_reg_patch(
553 wcd9xxx->type);
554 if (regmap_apply_patch) {
555 ret = regmap_apply_patch(wcd9xxx->regmap,
556 wcd9xxx->version);
557 if (ret)
558 dev_err(wcd9xxx->dev,
559 "Failed to register patch: %d\n", ret);
560 }
561
562 ret = mfd_add_devices(wcd9xxx->dev, -1, wcd9xxx->codec_type->dev,
563 wcd9xxx->codec_type->size, NULL, 0, NULL);
564 if (ret != 0) {
565 dev_err(wcd9xxx->dev, "Failed to add children: %d\n", ret);
566 goto err_irq;
567 }
568
569 ret = device_init_wakeup(wcd9xxx->dev, true);
570 if (ret) {
571 dev_err(wcd9xxx->dev, "Device wakeup init failed: %d\n", ret);
572 goto err_irq;
573 }
574
575 return ret;
576err_irq:
577 wcd9xxx_irq_exit(&wcd9xxx->core_res);
578fail_cdc_fill:
579 devm_kfree(wcd9xxx->dev, wcd9xxx->codec_type);
580 wcd9xxx->codec_type = NULL;
581err:
582 wcd9xxx_bringdown(wcd9xxx->dev);
583 wcd9xxx_core_res_deinit(&wcd9xxx->core_res);
584err_bring_up:
585 mutex_destroy(&wcd9xxx->io_lock);
586 mutex_destroy(&wcd9xxx->xfer_lock);
Vidyakumar Athota6258e3c2017-01-04 11:46:17 -0800587 mutex_destroy(&wcd9xxx->reset_lock);
Banajit Goswamide8271c2017-01-18 00:28:59 -0800588 return ret;
589}
590
591static void wcd9xxx_device_exit(struct wcd9xxx *wcd9xxx)
592{
593 device_init_wakeup(wcd9xxx->dev, false);
594 wcd9xxx_irq_exit(&wcd9xxx->core_res);
595 wcd9xxx_bringdown(wcd9xxx->dev);
596 wcd9xxx_reset_low(wcd9xxx->dev);
597 wcd9xxx_core_res_deinit(&wcd9xxx->core_res);
598 mutex_destroy(&wcd9xxx->io_lock);
599 mutex_destroy(&wcd9xxx->xfer_lock);
Vidyakumar Athota6258e3c2017-01-04 11:46:17 -0800600 mutex_destroy(&wcd9xxx->reset_lock);
Banajit Goswamide8271c2017-01-18 00:28:59 -0800601 if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_SLIMBUS)
602 slim_remove_device(wcd9xxx->slim_slave);
603}
604
605
606#ifdef CONFIG_DEBUG_FS
607struct wcd9xxx *debugCodec;
608
609static struct dentry *debugfs_wcd9xxx_dent;
610static struct dentry *debugfs_peek;
611static struct dentry *debugfs_poke;
612static struct dentry *debugfs_power_state;
613static struct dentry *debugfs_reg_dump;
614
615static unsigned char read_data;
616
617static int codec_debug_open(struct inode *inode, struct file *file)
618{
619 file->private_data = inode->i_private;
620 return 0;
621}
622
623static int get_parameters(char *buf, long int *param1, int num_of_par)
624{
625 char *token;
626 int base, cnt;
627
628 token = strsep(&buf, " ");
629
630 for (cnt = 0; cnt < num_of_par; cnt++) {
631 if (token != NULL) {
632 if ((token[1] == 'x') || (token[1] == 'X'))
633 base = 16;
634 else
635 base = 10;
636
637 if (kstrtoul(token, base, &param1[cnt]) != 0)
638 return -EINVAL;
639
640 token = strsep(&buf, " ");
641 } else
642 return -EINVAL;
643 }
644 return 0;
645}
646
647static ssize_t wcd9xxx_slimslave_reg_show(char __user *ubuf, size_t count,
648 loff_t *ppos)
649{
650 int i, reg_val, len;
651 ssize_t total = 0;
652 char tmp_buf[25]; /* each line is 12 bytes but 25 for margin of error */
653
654 for (i = (int) *ppos / 12; i <= SLIM_MAX_REG_ADDR; i++) {
655 reg_val = wcd9xxx_interface_reg_read(debugCodec, i);
656 len = snprintf(tmp_buf, sizeof(tmp_buf),
657 "0x%.3x: 0x%.2x\n", i, reg_val);
658
659 if ((total + len) >= count - 1)
660 break;
661 if (copy_to_user((ubuf + total), tmp_buf, len)) {
662 pr_err("%s: fail to copy reg dump\n", __func__);
663 total = -EFAULT;
664 goto copy_err;
665 }
666 *ppos += len;
667 total += len;
668 }
669
670copy_err:
671 return total;
672}
673
674static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
675 size_t count, loff_t *ppos)
676{
677 char lbuf[8];
678 char *access_str = file->private_data;
679 ssize_t ret_cnt;
680
681 if (*ppos < 0 || !count)
682 return -EINVAL;
683
684 if (!strcmp(access_str, "slimslave_peek")) {
685 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
686 ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
687 strnlen(lbuf, 7));
688 } else if (!strcmp(access_str, "slimslave_reg_dump")) {
689 ret_cnt = wcd9xxx_slimslave_reg_show(ubuf, count, ppos);
690 } else {
691 pr_err("%s: %s not permitted to read\n", __func__, access_str);
692 ret_cnt = -EPERM;
693 }
694
695 return ret_cnt;
696}
697
698static void wcd9xxx_set_reset_pin_state(struct wcd9xxx *wcd9xxx,
699 struct wcd9xxx_pdata *pdata,
700 bool active)
701{
702 if (wcd9xxx->wcd_rst_np) {
703 if (active)
704 msm_cdc_pinctrl_select_active_state(
705 wcd9xxx->wcd_rst_np);
706 else
707 msm_cdc_pinctrl_select_sleep_state(
708 wcd9xxx->wcd_rst_np);
709
710 return;
711 } else if (gpio_is_valid(wcd9xxx->reset_gpio)) {
712 gpio_direction_output(wcd9xxx->reset_gpio,
713 (active == true ? 1 : 0));
714 }
715}
716
717static int codec_debug_process_cdc_power(char *lbuf)
718{
719 long int param;
720 int rc;
721 struct wcd9xxx_pdata *pdata;
722
723 if (wcd9xxx_get_intf_type() != WCD9XXX_INTERFACE_TYPE_SLIMBUS) {
724 pr_err("%s: CODEC is not in SLIMBUS mode\n", __func__);
725 rc = -EPERM;
726 goto error_intf;
727 }
728
729 rc = get_parameters(lbuf, &param, 1);
730
731 if (likely(!rc)) {
732 pdata = debugCodec->slim->dev.platform_data;
733 if (param == 0) {
734 wcd9xxx_slim_device_down(debugCodec->slim);
735 msm_cdc_disable_static_supplies(debugCodec->dev,
736 debugCodec->supplies,
737 pdata->regulator,
738 pdata->num_supplies);
739 wcd9xxx_set_reset_pin_state(debugCodec, pdata, false);
740 } else if (param == 1) {
741 msm_cdc_enable_static_supplies(debugCodec->dev,
742 debugCodec->supplies,
743 pdata->regulator,
744 pdata->num_supplies);
745 usleep_range(1000, 2000);
746 wcd9xxx_set_reset_pin_state(debugCodec, pdata, false);
747 usleep_range(1000, 2000);
748 wcd9xxx_set_reset_pin_state(debugCodec, pdata, true);
749 usleep_range(1000, 2000);
750 wcd9xxx_slim_device_up(debugCodec->slim);
751 } else {
752 pr_err("%s: invalid command %ld\n", __func__, param);
753 }
754 }
755
756error_intf:
757 return rc;
758}
759
760static ssize_t codec_debug_write(struct file *filp,
761 const char __user *ubuf, size_t cnt, loff_t *ppos)
762{
763 char *access_str = filp->private_data;
764 char lbuf[32];
765 int rc;
766 long int param[5];
767
768 if (cnt > sizeof(lbuf) - 1)
769 return -EINVAL;
770
771 rc = copy_from_user(lbuf, ubuf, cnt);
772 if (rc)
773 return -EFAULT;
774
775 lbuf[cnt] = '\0';
776
777 if (!strcmp(access_str, "slimslave_poke")) {
778 /* write */
779 rc = get_parameters(lbuf, param, 2);
780 if ((param[0] <= 0x3FF) && (param[1] <= 0xFF) &&
781 (rc == 0))
782 wcd9xxx_interface_reg_write(debugCodec, param[0],
783 param[1]);
784 else
785 rc = -EINVAL;
786 } else if (!strcmp(access_str, "slimslave_peek")) {
787 /* read */
788 rc = get_parameters(lbuf, param, 1);
789 if ((param[0] <= 0x3FF) && (rc == 0))
790 read_data = wcd9xxx_interface_reg_read(debugCodec,
791 param[0]);
792 else
793 rc = -EINVAL;
794 } else if (!strcmp(access_str, "power_state")) {
795 rc = codec_debug_process_cdc_power(lbuf);
796 }
797
798 if (rc == 0)
799 rc = cnt;
800 else
801 pr_err("%s: rc = %d\n", __func__, rc);
802
803 return rc;
804}
805
806static const struct file_operations codec_debug_ops = {
807 .open = codec_debug_open,
808 .write = codec_debug_write,
809 .read = codec_debug_read
810};
811#endif
812
813static struct wcd9xxx_i2c *wcd9xxx_i2c_get_device_info(struct wcd9xxx *wcd9xxx,
814 u16 reg)
815{
816 u16 mask = 0x0f00;
817 int value = 0;
818 struct wcd9xxx_i2c *wcd9xxx_i2c = NULL;
819
820 if (wcd9xxx->type == WCD9335) {
821 wcd9xxx_i2c = &wcd9xxx_modules[0];
822 } else {
823 value = ((reg & mask) >> 8) & 0x000f;
824 switch (value) {
825 case 0:
826 wcd9xxx_i2c = &wcd9xxx_modules[0];
827 break;
828 case 1:
829 wcd9xxx_i2c = &wcd9xxx_modules[1];
830 break;
831 case 2:
832 wcd9xxx_i2c = &wcd9xxx_modules[2];
833 break;
834 case 3:
835 wcd9xxx_i2c = &wcd9xxx_modules[3];
836 break;
837
838 default:
839 break;
840 }
841 }
842 return wcd9xxx_i2c;
843}
844
845static int wcd9xxx_i2c_write_device(struct wcd9xxx *wcd9xxx, u16 reg, u8 *value,
846 u32 bytes)
847{
848
849 struct i2c_msg *msg;
850 int ret = 0;
851 u8 reg_addr = 0;
852 u8 data[bytes + 1];
853 struct wcd9xxx_i2c *wcd9xxx_i2c;
854
855 wcd9xxx_i2c = wcd9xxx_i2c_get_device_info(wcd9xxx, reg);
856 if (wcd9xxx_i2c == NULL || wcd9xxx_i2c->client == NULL) {
857 pr_err("failed to get device info\n");
858 return -ENODEV;
859 }
860 reg_addr = (u8)reg;
861 msg = &wcd9xxx_i2c->xfer_msg[0];
862 msg->addr = wcd9xxx_i2c->client->addr;
863 msg->len = bytes + 1;
864 msg->flags = 0;
865 data[0] = reg;
866 data[1] = *value;
867 msg->buf = data;
868 ret = i2c_transfer(wcd9xxx_i2c->client->adapter,
869 wcd9xxx_i2c->xfer_msg, 1);
870 /* Try again if the write fails */
871 if (ret != 1) {
872 ret = i2c_transfer(wcd9xxx_i2c->client->adapter,
873 wcd9xxx_i2c->xfer_msg, 1);
874 if (ret != 1) {
875 pr_err("failed to write the device\n");
876 return ret;
877 }
878 }
879 pr_debug("write success register = %x val = %x\n", reg, data[1]);
880 return 0;
881}
882
883
884static int wcd9xxx_i2c_read_device(struct wcd9xxx *wcd9xxx, unsigned short reg,
885 int bytes, unsigned char *dest)
886{
887 struct i2c_msg *msg;
888 int ret = 0;
889 u8 reg_addr = 0;
890 struct wcd9xxx_i2c *wcd9xxx_i2c;
891 u8 i = 0;
892
893 wcd9xxx_i2c = wcd9xxx_i2c_get_device_info(wcd9xxx, reg);
894 if (wcd9xxx_i2c == NULL || wcd9xxx_i2c->client == NULL) {
895 pr_err("failed to get device info\n");
896 return -ENODEV;
897 }
898 for (i = 0; i < bytes; i++) {
899 reg_addr = (u8)reg++;
900 msg = &wcd9xxx_i2c->xfer_msg[0];
901 msg->addr = wcd9xxx_i2c->client->addr;
902 msg->len = 1;
903 msg->flags = 0;
904 msg->buf = &reg_addr;
905
906 msg = &wcd9xxx_i2c->xfer_msg[1];
907 msg->addr = wcd9xxx_i2c->client->addr;
908 msg->len = 1;
909 msg->flags = I2C_M_RD;
910 msg->buf = dest++;
911 ret = i2c_transfer(wcd9xxx_i2c->client->adapter,
912 wcd9xxx_i2c->xfer_msg, 2);
913
914 /* Try again if read fails first time */
915 if (ret != 2) {
916 ret = i2c_transfer(wcd9xxx_i2c->client->adapter,
917 wcd9xxx_i2c->xfer_msg, 2);
918 if (ret != 2) {
919 pr_err("failed to read wcd9xxx register\n");
920 return ret;
921 }
922 }
923 }
924 return 0;
925}
926
927int wcd9xxx_i2c_read(struct wcd9xxx *wcd9xxx, unsigned short reg,
928 int bytes, void *dest, bool interface_reg)
929{
930 return wcd9xxx_i2c_read_device(wcd9xxx, reg, bytes, dest);
931}
932
933int wcd9xxx_i2c_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
934 int bytes, void *src, bool interface_reg)
935{
936 return wcd9xxx_i2c_write_device(wcd9xxx, reg, src, bytes);
937}
938
939static int wcd9xxx_i2c_get_client_index(struct i2c_client *client,
940 int *wcd9xx_index)
941{
942 int ret = 0;
943
944 switch (client->addr) {
945 case WCD9XXX_I2C_TOP_SLAVE_ADDR:
946 *wcd9xx_index = WCD9XXX_I2C_TOP_LEVEL;
947 break;
948 case WCD9XXX_ANALOG_I2C_SLAVE_ADDR:
949 *wcd9xx_index = WCD9XXX_I2C_ANALOG;
950 break;
951 case WCD9XXX_DIGITAL1_I2C_SLAVE_ADDR:
952 *wcd9xx_index = WCD9XXX_I2C_DIGITAL_1;
953 break;
954 case WCD9XXX_DIGITAL2_I2C_SLAVE_ADDR:
955 *wcd9xx_index = WCD9XXX_I2C_DIGITAL_2;
956 break;
957 default:
958 ret = -EINVAL;
959 break;
960 }
961 return ret;
962}
963
964static int wcd9xxx_i2c_probe(struct i2c_client *client,
965 const struct i2c_device_id *id)
966{
967 struct wcd9xxx *wcd9xxx = NULL;
968 struct wcd9xxx_pdata *pdata = NULL;
969 int val = 0;
970 int ret = 0;
971 int wcd9xx_index = 0;
972 struct device *dev;
973 int intf_type;
974 const struct of_device_id *of_id;
975
976 intf_type = wcd9xxx_get_intf_type();
977
978 pr_debug("%s: interface status %d\n", __func__, intf_type);
979 if (intf_type == WCD9XXX_INTERFACE_TYPE_SLIMBUS) {
980 dev_dbg(&client->dev, "%s:Codec is detected in slimbus mode\n",
981 __func__);
982 return -ENODEV;
983 } else if (intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
984 ret = wcd9xxx_i2c_get_client_index(client, &wcd9xx_index);
985 if (ret != 0)
986 dev_err(&client->dev, "%s: I2C set codec I2C\n"
987 "client failed\n", __func__);
988 else {
989 dev_err(&client->dev, "%s:probe for other slaves\n"
990 "devices of codec I2C slave Addr = %x\n",
991 __func__, client->addr);
992 wcd9xxx_modules[wcd9xx_index].client = client;
993 }
994 return ret;
995 } else if (intf_type == WCD9XXX_INTERFACE_TYPE_PROBING) {
996 dev = &client->dev;
997 if (client->dev.of_node) {
998 dev_dbg(&client->dev, "%s:Platform data\n"
999 "from device tree\n", __func__);
1000 pdata = wcd9xxx_populate_dt_data(&client->dev);
1001 if (!pdata) {
1002 dev_err(&client->dev,
1003 "%s: Fail to obtain pdata from device tree\n",
1004 __func__);
1005 ret = -EINVAL;
1006 goto fail;
1007 }
1008 client->dev.platform_data = pdata;
1009 } else {
1010 dev_dbg(&client->dev, "%s:Platform data from\n"
1011 "board file\n", __func__);
1012 pdata = client->dev.platform_data;
1013 }
1014 wcd9xxx = devm_kzalloc(&client->dev, sizeof(struct wcd9xxx),
1015 GFP_KERNEL);
1016 if (!wcd9xxx) {
1017 ret = -ENOMEM;
1018 goto fail;
1019 }
1020
1021 if (!pdata) {
1022 dev_dbg(&client->dev, "no platform data?\n");
1023 ret = -EINVAL;
1024 goto fail;
1025 }
1026 wcd9xxx->type = WCD9XXX;
1027 if (client->dev.of_node) {
1028 of_id = of_match_device(wcd9xxx_of_match, &client->dev);
1029 if (of_id) {
1030 wcd9xxx->type = *((int *)of_id->data);
1031 dev_info(&client->dev, "%s: codec type is %d\n",
1032 __func__, wcd9xxx->type);
1033 }
1034 } else {
1035 dev_info(&client->dev, "%s: dev.of_node is NULL, default to WCD9XXX\n",
1036 __func__);
1037 wcd9xxx->type = WCD9XXX;
1038 }
1039 wcd9xxx->regmap = wcd9xxx_regmap_init(&client->dev,
1040 &wcd9xxx_i2c_base_regmap_config);
1041 if (IS_ERR(wcd9xxx->regmap)) {
1042 ret = PTR_ERR(wcd9xxx->regmap);
1043 dev_err(&client->dev, "%s: Failed to allocate register map: %d\n",
1044 __func__, ret);
1045 goto err_codec;
1046 }
1047 wcd9xxx->reset_gpio = pdata->reset_gpio;
1048 wcd9xxx->wcd_rst_np = pdata->wcd_rst_np;
1049
1050 if (!wcd9xxx->wcd_rst_np) {
1051 pdata->use_pinctrl = false;
1052 dev_err(&client->dev, "%s: pinctrl not used for rst_n\n",
1053 __func__);
1054 goto err_codec;
1055 }
1056
1057 if (i2c_check_functionality(client->adapter,
1058 I2C_FUNC_I2C) == 0) {
1059 dev_dbg(&client->dev, "can't talk I2C?\n");
1060 ret = -EIO;
1061 goto fail;
1062 }
1063 dev_set_drvdata(&client->dev, wcd9xxx);
1064 wcd9xxx->dev = &client->dev;
1065 wcd9xxx->dev_up = true;
1066 if (client->dev.of_node)
1067 wcd9xxx->mclk_rate = pdata->mclk_rate;
1068
1069 wcd9xxx->num_of_supplies = pdata->num_supplies;
1070 ret = msm_cdc_init_supplies(wcd9xxx->dev, &wcd9xxx->supplies,
1071 pdata->regulator,
1072 pdata->num_supplies);
1073 if (!wcd9xxx->supplies) {
1074 dev_err(wcd9xxx->dev, "%s: Cannot init wcd supplies\n",
1075 __func__);
1076 goto err_codec;
1077 }
1078 ret = msm_cdc_enable_static_supplies(wcd9xxx->dev,
1079 wcd9xxx->supplies,
1080 pdata->regulator,
1081 pdata->num_supplies);
1082 if (ret) {
1083 dev_err(wcd9xxx->dev, "%s: wcd static supply enable failed!\n",
1084 __func__);
1085 goto err_codec;
1086 }
1087 /* For WCD9335, it takes about 600us for the Vout_A and
1088 * Vout_D to be ready after BUCK_SIDO is powered up\
1089 * SYS_RST_N shouldn't be pulled high during this time
1090 */
1091 if (wcd9xxx->type == WCD9335)
1092 usleep_range(600, 650);
1093 else
1094 usleep_range(5, 10);
1095
1096 ret = wcd9xxx_reset(wcd9xxx->dev);
1097 if (ret) {
1098 pr_err("%s: Resetting Codec failed\n", __func__);
1099 goto err_supplies;
1100 }
1101
1102 ret = wcd9xxx_i2c_get_client_index(client, &wcd9xx_index);
1103 if (ret != 0) {
1104 pr_err("%s:Set codec I2C client failed\n", __func__);
1105 goto err_supplies;
1106 }
1107
1108 wcd9xxx_modules[wcd9xx_index].client = client;
1109 wcd9xxx->read_dev = wcd9xxx_i2c_read;
1110 wcd9xxx->write_dev = wcd9xxx_i2c_write;
1111 if (!wcd9xxx->dev->of_node)
1112 wcd9xxx_assign_irq(&wcd9xxx->core_res,
1113 pdata->irq, pdata->irq_base);
1114
1115 ret = wcd9xxx_device_init(wcd9xxx);
1116 if (ret) {
1117 pr_err("%s: error, initializing device failed (%d)\n",
1118 __func__, ret);
1119 goto err_device_init;
1120 }
1121
1122 ret = wcd9xxx_i2c_read(wcd9xxx, WCD9XXX_A_CHIP_STATUS, 1,
1123 &val, 0);
1124 if (ret < 0)
1125 pr_err("%s: failed to read the wcd9xxx status (%d)\n",
1126 __func__, ret);
1127 if (val != wcd9xxx->codec_type->i2c_chip_status)
1128 pr_err("%s: unknown chip status 0x%x\n", __func__, val);
1129
1130 wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_I2C);
1131
1132 return ret;
1133 }
1134
1135 pr_err("%s: I2C probe in wrong state\n", __func__);
1136
1137
1138err_device_init:
1139 wcd9xxx_reset_low(wcd9xxx->dev);
1140err_supplies:
1141 msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies,
1142 pdata->regulator,
1143 pdata->num_supplies);
1144 pdata->regulator = NULL;
1145 pdata->num_supplies = 0;
1146err_codec:
1147 devm_kfree(&client->dev, wcd9xxx);
1148 dev_set_drvdata(&client->dev, NULL);
1149fail:
1150 return ret;
1151}
1152
1153static int wcd9xxx_i2c_remove(struct i2c_client *client)
1154{
1155 struct wcd9xxx *wcd9xxx;
1156 struct wcd9xxx_pdata *pdata = client->dev.platform_data;
1157
1158 wcd9xxx = dev_get_drvdata(&client->dev);
1159 msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies,
1160 pdata->regulator,
1161 pdata->num_supplies);
1162 wcd9xxx_device_exit(wcd9xxx);
1163 dev_set_drvdata(&client->dev, NULL);
1164 return 0;
1165}
1166
1167static int wcd9xxx_dt_parse_slim_interface_dev_info(struct device *dev,
1168 struct slim_device *slim_ifd)
1169{
1170 int ret = 0;
1171 struct property *prop;
1172
1173 ret = of_property_read_string(dev->of_node, "qcom,cdc-slim-ifd",
1174 &slim_ifd->name);
1175 if (ret) {
1176 dev_err(dev, "Looking up %s property in node %s failed",
1177 "qcom,cdc-slim-ifd-dev", dev->of_node->full_name);
1178 return -ENODEV;
1179 }
1180 prop = of_find_property(dev->of_node,
1181 "qcom,cdc-slim-ifd-elemental-addr", NULL);
1182 if (!prop) {
1183 dev_err(dev, "Looking up %s property in node %s failed",
1184 "qcom,cdc-slim-ifd-elemental-addr",
1185 dev->of_node->full_name);
1186 return -ENODEV;
1187 } else if (prop->length != 6) {
1188 dev_err(dev, "invalid codec slim ifd addr. addr length = %d\n",
1189 prop->length);
1190 return -ENODEV;
1191 }
1192 memcpy(slim_ifd->e_addr, prop->value, 6);
1193
1194 return 0;
1195}
1196
1197static int wcd9xxx_slim_get_laddr(struct slim_device *sb,
1198 const u8 *e_addr, u8 e_len, u8 *laddr)
1199{
1200 int ret;
1201 const unsigned long timeout = jiffies +
1202 msecs_to_jiffies(SLIMBUS_PRESENT_TIMEOUT);
1203
1204 do {
1205 ret = slim_get_logical_addr(sb, e_addr, e_len, laddr);
1206 if (!ret)
1207 break;
1208 /* Give SLIMBUS time to report present and be ready. */
1209 usleep_range(1000, 1100);
1210 pr_debug_ratelimited("%s: retyring get logical addr\n",
1211 __func__);
1212 } while time_before(jiffies, timeout);
1213
1214 return ret;
1215}
1216
1217static int wcd9xxx_slim_probe(struct slim_device *slim)
1218{
1219 struct wcd9xxx *wcd9xxx;
1220 struct wcd9xxx_pdata *pdata;
Karthikeyan Manif18db022016-12-02 17:55:02 -08001221 const struct slim_device_id *device_id;
Banajit Goswamide8271c2017-01-18 00:28:59 -08001222 int ret = 0;
1223 int intf_type;
1224
1225 intf_type = wcd9xxx_get_intf_type();
1226
Karthikeyan Manif18db022016-12-02 17:55:02 -08001227 wcd9xxx = devm_kzalloc(&slim->dev, sizeof(struct wcd9xxx),
1228 GFP_KERNEL);
1229 if (!wcd9xxx) {
1230 ret = -ENOMEM;
1231 goto err;
1232 }
1233
Banajit Goswamide8271c2017-01-18 00:28:59 -08001234 if (!slim) {
1235 ret = -EINVAL;
1236 goto err;
1237 }
1238
1239 if (intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
1240 dev_dbg(&slim->dev, "%s:Codec is detected in I2C mode\n",
1241 __func__);
Karthikeyan Manif18db022016-12-02 17:55:02 -08001242 ret = -ENODEV;
1243 goto err;
Banajit Goswamide8271c2017-01-18 00:28:59 -08001244 }
1245 if (slim->dev.of_node) {
1246 dev_info(&slim->dev, "Platform data from device tree\n");
1247 pdata = wcd9xxx_populate_dt_data(&slim->dev);
1248 if (!pdata) {
1249 dev_err(&slim->dev,
1250 "%s: Fail to obtain pdata from device tree\n",
1251 __func__);
1252 ret = -EINVAL;
1253 goto err;
1254 }
1255
1256 ret = wcd9xxx_dt_parse_slim_interface_dev_info(&slim->dev,
1257 &pdata->slimbus_slave_device);
1258 if (ret) {
1259 dev_err(&slim->dev, "Error, parsing slim interface\n");
1260 devm_kfree(&slim->dev, pdata);
1261 ret = -EINVAL;
1262 goto err;
1263 }
1264 slim->dev.platform_data = pdata;
1265
1266 } else {
1267 dev_info(&slim->dev, "Platform data from board file\n");
1268 pdata = slim->dev.platform_data;
1269 }
1270
1271 if (!pdata) {
1272 dev_err(&slim->dev, "Error, no platform data\n");
1273 ret = -EINVAL;
1274 goto err;
1275 }
1276
Banajit Goswamide8271c2017-01-18 00:28:59 -08001277 if (!slim->ctrl) {
1278 dev_err(&slim->dev, "%s: Error, no SLIMBUS control data\n",
1279 __func__);
1280 ret = -EINVAL;
1281 goto err_codec;
1282 }
Rohit Kumar1fe7ae12017-07-17 18:05:16 +05301283
1284 if (pdata->has_buck_vsel_gpio)
1285 msm_cdc_pinctrl_select_active_state(pdata->buck_vsel_ctl_np);
1286
Karthikeyan Manif18db022016-12-02 17:55:02 -08001287 device_id = slim_get_device_id(slim);
1288 if (!device_id) {
1289 dev_err(&slim->dev, "%s: Error, no device id\n", __func__);
1290 ret = -EINVAL;
1291 goto err;
1292 }
1293
1294 wcd9xxx->type = device_id->driver_data;
Banajit Goswamide8271c2017-01-18 00:28:59 -08001295 dev_info(&slim->dev, "%s: probing for wcd type: %d, name: %s\n",
Karthikeyan Manif18db022016-12-02 17:55:02 -08001296 __func__, wcd9xxx->type, device_id->name);
Banajit Goswamide8271c2017-01-18 00:28:59 -08001297
1298 /* wcd9xxx members init */
1299 wcd9xxx->multi_reg_write = wcd9xxx_slim_multi_reg_write;
1300 wcd9xxx->slim = slim;
1301 slim_set_clientdata(slim, wcd9xxx);
1302 wcd9xxx->reset_gpio = pdata->reset_gpio;
1303 wcd9xxx->dev = &slim->dev;
1304 wcd9xxx->mclk_rate = pdata->mclk_rate;
1305 wcd9xxx->dev_up = true;
1306 wcd9xxx->wcd_rst_np = pdata->wcd_rst_np;
1307
1308 wcd9xxx->regmap = wcd9xxx_regmap_init(&slim->dev,
1309 &wcd9xxx_base_regmap_config);
1310 if (IS_ERR(wcd9xxx->regmap)) {
1311 ret = PTR_ERR(wcd9xxx->regmap);
1312 dev_err(&slim->dev, "%s: Failed to allocate register map: %d\n",
1313 __func__, ret);
1314 goto err_codec;
1315 }
1316
1317 if (!wcd9xxx->wcd_rst_np) {
1318 pdata->use_pinctrl = false;
1319 dev_err(&slim->dev, "%s: pinctrl not used for rst_n\n",
1320 __func__);
1321 goto err_codec;
1322 }
1323
1324 wcd9xxx->num_of_supplies = pdata->num_supplies;
1325 ret = msm_cdc_init_supplies(&slim->dev, &wcd9xxx->supplies,
1326 pdata->regulator,
1327 pdata->num_supplies);
1328 if (!wcd9xxx->supplies) {
1329 dev_err(wcd9xxx->dev, "%s: Cannot init wcd supplies\n",
1330 __func__);
1331 goto err_codec;
1332 }
1333 ret = msm_cdc_enable_static_supplies(wcd9xxx->dev,
1334 wcd9xxx->supplies,
1335 pdata->regulator,
1336 pdata->num_supplies);
1337 if (ret) {
1338 dev_err(wcd9xxx->dev, "%s: wcd static supply enable failed!\n",
1339 __func__);
1340 goto err_codec;
1341 }
1342
1343 /*
1344 * For WCD9335, it takes about 600us for the Vout_A and
1345 * Vout_D to be ready after BUCK_SIDO is powered up.
1346 * SYS_RST_N shouldn't be pulled high during this time
1347 */
1348 if (wcd9xxx->type == WCD9335 || wcd9xxx->type == WCD934X)
1349 usleep_range(600, 650);
1350 else
1351 usleep_range(5, 10);
1352
1353 ret = wcd9xxx_reset(&slim->dev);
1354 if (ret) {
1355 dev_err(&slim->dev, "%s: Resetting Codec failed\n", __func__);
1356 goto err_supplies;
1357 }
1358
1359 ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim, wcd9xxx->slim->e_addr,
1360 ARRAY_SIZE(wcd9xxx->slim->e_addr),
1361 &wcd9xxx->slim->laddr);
1362 if (ret) {
1363 dev_err(&slim->dev, "%s: failed to get slimbus %s logical address: %d\n",
1364 __func__, wcd9xxx->slim->name, ret);
1365 goto err_reset;
1366 }
1367 wcd9xxx->read_dev = wcd9xxx_slim_read_device;
1368 wcd9xxx->write_dev = wcd9xxx_slim_write_device;
1369 wcd9xxx_pgd_la = wcd9xxx->slim->laddr;
1370 wcd9xxx->slim_slave = &pdata->slimbus_slave_device;
1371 if (!wcd9xxx->dev->of_node)
1372 wcd9xxx_assign_irq(&wcd9xxx->core_res,
1373 pdata->irq, pdata->irq_base);
1374
1375 ret = slim_add_device(slim->ctrl, wcd9xxx->slim_slave);
1376 if (ret) {
1377 dev_err(&slim->dev, "%s: error, adding SLIMBUS device failed\n",
1378 __func__);
1379 goto err_reset;
1380 }
1381
1382 ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim_slave,
1383 wcd9xxx->slim_slave->e_addr,
1384 ARRAY_SIZE(wcd9xxx->slim_slave->e_addr),
1385 &wcd9xxx->slim_slave->laddr);
1386 if (ret) {
1387 dev_err(&slim->dev, "%s: failed to get slimbus %s logical address: %d\n",
1388 __func__, wcd9xxx->slim->name, ret);
1389 goto err_slim_add;
1390 }
1391 wcd9xxx_inf_la = wcd9xxx->slim_slave->laddr;
1392 wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_SLIMBUS);
1393
1394 ret = wcd9xxx_device_init(wcd9xxx);
1395 if (ret) {
1396 dev_err(&slim->dev, "%s: error, initializing device failed (%d)\n",
1397 __func__, ret);
1398 goto err_slim_add;
1399 }
1400#ifdef CONFIG_DEBUG_FS
1401 debugCodec = wcd9xxx;
1402
1403 debugfs_wcd9xxx_dent = debugfs_create_dir
1404 ("wcd9xxx_core", 0);
1405 if (!IS_ERR(debugfs_wcd9xxx_dent)) {
1406 debugfs_peek = debugfs_create_file("slimslave_peek",
1407 S_IFREG | 0444, debugfs_wcd9xxx_dent,
1408 (void *) "slimslave_peek", &codec_debug_ops);
1409
1410 debugfs_poke = debugfs_create_file("slimslave_poke",
1411 S_IFREG | 0444, debugfs_wcd9xxx_dent,
1412 (void *) "slimslave_poke", &codec_debug_ops);
1413
1414 debugfs_power_state = debugfs_create_file("power_state",
1415 S_IFREG | 0444, debugfs_wcd9xxx_dent,
1416 (void *) "power_state", &codec_debug_ops);
1417
1418 debugfs_reg_dump = debugfs_create_file("slimslave_reg_dump",
1419 S_IFREG | 0444, debugfs_wcd9xxx_dent,
1420 (void *) "slimslave_reg_dump", &codec_debug_ops);
1421 }
1422#endif
1423
1424 return ret;
1425
1426err_slim_add:
1427 slim_remove_device(wcd9xxx->slim_slave);
1428err_reset:
1429 wcd9xxx_reset_low(wcd9xxx->dev);
1430err_supplies:
1431 msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies,
1432 pdata->regulator,
1433 pdata->num_supplies);
1434err_codec:
1435 slim_set_clientdata(slim, NULL);
1436err:
Karthikeyan Manif18db022016-12-02 17:55:02 -08001437 devm_kfree(&slim->dev, wcd9xxx);
Banajit Goswamide8271c2017-01-18 00:28:59 -08001438 return ret;
1439}
1440static int wcd9xxx_slim_remove(struct slim_device *pdev)
1441{
1442 struct wcd9xxx *wcd9xxx;
1443 struct wcd9xxx_pdata *pdata = pdev->dev.platform_data;
1444
1445#ifdef CONFIG_DEBUG_FS
1446 debugfs_remove_recursive(debugfs_wcd9xxx_dent);
1447#endif
1448 wcd9xxx = slim_get_devicedata(pdev);
1449 wcd9xxx_deinit_slimslave(wcd9xxx);
1450 slim_remove_device(wcd9xxx->slim_slave);
1451 msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies,
1452 pdata->regulator,
1453 pdata->num_supplies);
1454 wcd9xxx_device_exit(wcd9xxx);
1455 slim_set_clientdata(pdev, NULL);
1456 return 0;
1457}
1458
1459static int wcd9xxx_device_up(struct wcd9xxx *wcd9xxx)
1460{
1461 int ret = 0;
1462 struct wcd9xxx_core_resource *wcd9xxx_res = &wcd9xxx->core_res;
1463
1464 dev_info(wcd9xxx->dev, "%s: codec bring up\n", __func__);
1465 wcd9xxx_bringup(wcd9xxx->dev);
1466 ret = wcd9xxx_irq_init(wcd9xxx_res);
1467 if (ret) {
1468 pr_err("%s: wcd9xx_irq_init failed : %d\n", __func__, ret);
1469 } else {
1470 if (wcd9xxx->post_reset)
1471 ret = wcd9xxx->post_reset(wcd9xxx);
1472 }
1473 return ret;
1474}
1475
1476static int wcd9xxx_slim_device_reset(struct slim_device *sldev)
1477{
1478 int ret;
1479 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1480
1481 if (!wcd9xxx) {
1482 pr_err("%s: wcd9xxx is NULL\n", __func__);
1483 return -EINVAL;
1484 }
1485
Ramprasad Katkam40bc50d2017-05-19 13:21:28 +05301486 /*
1487 * Wait for 500 ms for device down to complete. Observed delay
1488 * of ~200ms for device down to complete after being called,
1489 * due to context switch issue.
1490 */
1491 ret = wait_on_bit_timeout(&wcd9xxx->dev_up, 0,
1492 TASK_INTERRUPTIBLE,
1493 msecs_to_jiffies(500));
1494 if (ret)
1495 pr_err("%s: slim device down not complete in 500 msec\n",
1496 __func__);
Banajit Goswamide8271c2017-01-18 00:28:59 -08001497
Vidyakumar Athota6258e3c2017-01-04 11:46:17 -08001498 mutex_lock(&wcd9xxx->reset_lock);
Ramprasad Katkam40bc50d2017-05-19 13:21:28 +05301499
1500 dev_info(wcd9xxx->dev, "%s: device reset, dev_up = %lu\n",
1501 __func__, wcd9xxx->dev_up);
1502 if (wcd9xxx->dev_up) {
1503 mutex_unlock(&wcd9xxx->reset_lock);
1504 return 0;
1505 }
1506
Banajit Goswamide8271c2017-01-18 00:28:59 -08001507 ret = wcd9xxx_reset(wcd9xxx->dev);
1508 if (ret)
1509 dev_err(wcd9xxx->dev, "%s: Resetting Codec failed\n", __func__);
Vidyakumar Athota6258e3c2017-01-04 11:46:17 -08001510 mutex_unlock(&wcd9xxx->reset_lock);
Banajit Goswamide8271c2017-01-18 00:28:59 -08001511
1512 return ret;
1513}
1514
1515static int wcd9xxx_slim_device_up(struct slim_device *sldev)
1516{
1517 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
Vidyakumar Athota6258e3c2017-01-04 11:46:17 -08001518 int ret = 0;
Banajit Goswamide8271c2017-01-18 00:28:59 -08001519
1520 if (!wcd9xxx) {
1521 pr_err("%s: wcd9xxx is NULL\n", __func__);
1522 return -EINVAL;
1523 }
Ramprasad Katkam40bc50d2017-05-19 13:21:28 +05301524 dev_info(wcd9xxx->dev, "%s: slim device up, dev_up = %lu\n",
1525 __func__, wcd9xxx->dev_up);
Banajit Goswamide8271c2017-01-18 00:28:59 -08001526 if (wcd9xxx->dev_up)
1527 return 0;
1528
1529 wcd9xxx->dev_up = true;
Vidyakumar Athota6258e3c2017-01-04 11:46:17 -08001530
1531 mutex_lock(&wcd9xxx->reset_lock);
1532 ret = wcd9xxx_device_up(wcd9xxx);
1533 mutex_unlock(&wcd9xxx->reset_lock);
1534
1535 return ret;
Banajit Goswamide8271c2017-01-18 00:28:59 -08001536}
1537
1538static int wcd9xxx_slim_device_down(struct slim_device *sldev)
1539{
1540 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1541
1542 if (!wcd9xxx) {
1543 pr_err("%s: wcd9xxx is NULL\n", __func__);
1544 return -EINVAL;
1545 }
1546
Vidyakumar Athota6258e3c2017-01-04 11:46:17 -08001547 mutex_lock(&wcd9xxx->reset_lock);
Ramprasad Katkam40bc50d2017-05-19 13:21:28 +05301548
1549 dev_info(wcd9xxx->dev, "%s: device down, dev_up = %lu\n",
1550 __func__, wcd9xxx->dev_up);
1551 if (!wcd9xxx->dev_up) {
1552 mutex_unlock(&wcd9xxx->reset_lock);
1553 return 0;
1554 }
1555
Banajit Goswamide8271c2017-01-18 00:28:59 -08001556 if (wcd9xxx->dev_down)
1557 wcd9xxx->dev_down(wcd9xxx);
Banajit Goswamidf4fd782016-01-06 12:13:17 -08001558 wcd9xxx_irq_exit(&wcd9xxx->core_res);
Banajit Goswamide8271c2017-01-18 00:28:59 -08001559 wcd9xxx_reset_low(wcd9xxx->dev);
Ramprasad Katkam40bc50d2017-05-19 13:21:28 +05301560 wcd9xxx->dev_up = false;
Vidyakumar Athota6258e3c2017-01-04 11:46:17 -08001561 mutex_unlock(&wcd9xxx->reset_lock);
1562
Banajit Goswamide8271c2017-01-18 00:28:59 -08001563 return 0;
1564}
1565
1566static int wcd9xxx_slim_resume(struct slim_device *sldev)
1567{
1568 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1569
1570 return wcd9xxx_core_res_resume(&wcd9xxx->core_res);
1571}
1572
1573static int wcd9xxx_i2c_resume(struct device *dev)
1574{
1575 struct wcd9xxx *wcd9xxx = dev_get_drvdata(dev);
1576
1577 if (wcd9xxx)
1578 return wcd9xxx_core_res_resume(&wcd9xxx->core_res);
1579 else
1580 return 0;
1581}
1582
1583static int wcd9xxx_slim_suspend(struct slim_device *sldev, pm_message_t pmesg)
1584{
1585 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1586
1587 return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg);
1588}
1589
1590static int wcd9xxx_i2c_suspend(struct device *dev)
1591{
1592 struct wcd9xxx *wcd9xxx = dev_get_drvdata(dev);
1593 pm_message_t pmesg = {0};
1594
1595 if (wcd9xxx)
1596 return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg);
1597 else
1598 return 0;
1599}
1600
1601static const struct slim_device_id wcd_slim_device_id[] = {
1602 {"sitar-slim", 0},
1603 {"sitar1p1-slim", 0},
1604 {"tabla-slim", 0},
1605 {"tabla2x-slim", 0},
1606 {"taiko-slim-pgd", 0},
1607 {"tapan-slim-pgd", 0},
1608 {"tomtom-slim-pgd", WCD9330},
1609 {"tasha-slim-pgd", WCD9335},
1610 {"tavil-slim-pgd", WCD934X},
1611 {}
1612};
1613
1614static struct slim_driver wcd_slim_driver = {
1615 .driver = {
1616 .name = "wcd-slim",
1617 .owner = THIS_MODULE,
1618 },
1619 .probe = wcd9xxx_slim_probe,
1620 .remove = wcd9xxx_slim_remove,
1621 .id_table = wcd_slim_device_id,
1622 .resume = wcd9xxx_slim_resume,
1623 .suspend = wcd9xxx_slim_suspend,
1624 .device_up = wcd9xxx_slim_device_up,
1625 .reset_device = wcd9xxx_slim_device_reset,
1626 .device_down = wcd9xxx_slim_device_down,
1627};
1628
1629static struct i2c_device_id wcd9xxx_id_table[] = {
1630 {"wcd9xxx-i2c", WCD9XXX_I2C_TOP_LEVEL},
1631 {"wcd9xxx-i2c", WCD9XXX_I2C_ANALOG},
1632 {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_1},
1633 {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_2},
1634 {}
1635};
1636
1637static struct i2c_device_id tasha_id_table[] = {
1638 {"tasha-i2c-pgd", WCD9XXX_I2C_TOP_LEVEL},
1639 {}
1640};
1641
1642static struct i2c_device_id tabla_id_table[] = {
1643 {"tabla top level", WCD9XXX_I2C_TOP_LEVEL},
1644 {"tabla analog", WCD9XXX_I2C_ANALOG},
1645 {"tabla digital1", WCD9XXX_I2C_DIGITAL_1},
1646 {"tabla digital2", WCD9XXX_I2C_DIGITAL_2},
1647 {}
1648};
1649MODULE_DEVICE_TABLE(i2c, tabla_id_table);
1650
1651static const struct dev_pm_ops wcd9xxx_i2c_pm_ops = {
1652 .suspend = wcd9xxx_i2c_suspend,
1653 .resume = wcd9xxx_i2c_resume,
1654};
1655
1656static struct i2c_driver tabla_i2c_driver = {
1657 .driver = {
1658 .owner = THIS_MODULE,
1659 .name = "tabla-i2c-core",
1660 .pm = &wcd9xxx_i2c_pm_ops,
1661 },
1662 .id_table = tabla_id_table,
1663 .probe = wcd9xxx_i2c_probe,
1664 .remove = wcd9xxx_i2c_remove,
1665};
1666
1667static struct i2c_driver wcd9xxx_i2c_driver = {
1668 .driver = {
1669 .owner = THIS_MODULE,
1670 .name = "wcd9xxx-i2c-core",
1671 .pm = &wcd9xxx_i2c_pm_ops,
1672 },
1673 .id_table = wcd9xxx_id_table,
1674 .probe = wcd9xxx_i2c_probe,
1675 .remove = wcd9xxx_i2c_remove,
1676};
1677
1678static struct i2c_driver wcd9335_i2c_driver = {
1679 .driver = {
1680 .owner = THIS_MODULE,
1681 .name = "tasha-i2c-core",
1682 .pm = &wcd9xxx_i2c_pm_ops,
1683 },
1684 .id_table = tasha_id_table,
1685 .probe = wcd9xxx_i2c_probe,
1686 .remove = wcd9xxx_i2c_remove,
1687};
1688
Karthikeyan Manieea18362017-06-27 18:09:46 -07001689int wcd9xxx_init(void)
Banajit Goswamide8271c2017-01-18 00:28:59 -08001690{
1691 int ret[NUM_WCD9XXX_REG_RET] = {0};
1692 int i = 0;
1693
1694 wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING);
1695
1696 ret[0] = i2c_add_driver(&tabla_i2c_driver);
1697 if (ret[0])
1698 pr_err("%s: Failed to add the tabla2x I2C driver: %d\n",
1699 __func__, ret[0]);
1700
1701 ret[1] = i2c_add_driver(&wcd9xxx_i2c_driver);
1702 if (ret[1])
1703 pr_err("%s: Failed to add the wcd9xxx I2C driver: %d\n",
1704 __func__, ret[1]);
1705
1706 ret[2] = i2c_add_driver(&wcd9335_i2c_driver);
1707 if (ret[2])
1708 pr_err("%s: Failed to add the wcd9335 I2C driver: %d\n",
1709 __func__, ret[2]);
1710
1711 ret[3] = slim_driver_register(&wcd_slim_driver);
1712 if (ret[3])
1713 pr_err("%s: Failed to register wcd SB driver: %d\n",
1714 __func__, ret[3]);
1715
1716 for (i = 0; i < NUM_WCD9XXX_REG_RET; i++) {
1717 if (ret[i])
1718 return ret[i];
1719 }
1720
1721 return 0;
1722}
Banajit Goswamide8271c2017-01-18 00:28:59 -08001723
Karthikeyan Manieea18362017-06-27 18:09:46 -07001724void wcd9xxx_exit(void)
Banajit Goswamide8271c2017-01-18 00:28:59 -08001725{
1726 wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING);
1727
1728 i2c_del_driver(&tabla_i2c_driver);
1729 i2c_del_driver(&wcd9xxx_i2c_driver);
1730 i2c_del_driver(&wcd9335_i2c_driver);
1731 slim_driver_unregister(&wcd_slim_driver);
1732}
Banajit Goswamide8271c2017-01-18 00:28:59 -08001733
1734MODULE_DESCRIPTION("Codec core driver");
1735MODULE_LICENSE("GPL v2");