blob: 2ab5e894864e803e9e1ba2c4ba99a4ede09919d4 [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/* 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>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053019#include <linux/delay.h>
20#include <linux/gpio.h>
21#include <linux/debugfs.h>
22#include <linux/i2c.h>
23#include <linux/regmap.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053024#include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053025#include <sound/soc.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053026#include "core.h"
27#include "pdata.h"
28#include "msm-cdc-pinctrl.h"
29#include "msm-cdc-supply.h"
30#include "wcd9xxx-irq.h"
31#include "wcd9xxx-utils.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053032#include "wcd9xxx-regmap.h"
Laxminath Kasam605b42f2017-08-01 22:02:15 +053033#include "wcd9xxx-slimslave.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053034
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(
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530231 wcd9xxx->dev, "%s: No read allowed. dev_up = %lu\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530232 __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(
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530271 wcd9xxx->dev, "%s: No write allowed. dev_up = %lu\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530272 __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(
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530348 wcd9xxx->dev, "%s: No write allowed. dev_up = %lu\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530349 __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(
Asish Bhattacharya84f7f732017-07-25 16:29:27 +0530429 wcd9xxx->dev, "%s: No write allowed. dev_up = %lu\n",
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530430 __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);
508 mutex_init(&wcd9xxx->reset_lock);
509
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);
587 mutex_destroy(&wcd9xxx->reset_lock);
588 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);
600 mutex_destroy(&wcd9xxx->reset_lock);
601 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;
1221 const struct slim_device_id *device_id;
1222 int ret = 0;
1223 int intf_type;
1224
1225 intf_type = wcd9xxx_get_intf_type();
1226
1227 wcd9xxx = devm_kzalloc(&slim->dev, sizeof(struct wcd9xxx),
1228 GFP_KERNEL);
1229 if (!wcd9xxx) {
1230 ret = -ENOMEM;
1231 goto err;
1232 }
1233
1234 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__);
1242 ret = -ENODEV;
1243 goto err;
1244 }
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
1277 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 }
1283 device_id = slim_get_device_id(slim);
1284 if (!device_id) {
1285 dev_err(&slim->dev, "%s: Error, no device id\n", __func__);
1286 ret = -EINVAL;
1287 goto err;
1288 }
1289
1290 wcd9xxx->type = device_id->driver_data;
1291 dev_info(&slim->dev, "%s: probing for wcd type: %d, name: %s\n",
1292 __func__, wcd9xxx->type, device_id->name);
1293
1294 /* wcd9xxx members init */
1295 wcd9xxx->multi_reg_write = wcd9xxx_slim_multi_reg_write;
1296 wcd9xxx->slim = slim;
1297 slim_set_clientdata(slim, wcd9xxx);
1298 wcd9xxx->reset_gpio = pdata->reset_gpio;
1299 wcd9xxx->dev = &slim->dev;
1300 wcd9xxx->mclk_rate = pdata->mclk_rate;
1301 wcd9xxx->dev_up = true;
1302 wcd9xxx->wcd_rst_np = pdata->wcd_rst_np;
1303
1304 wcd9xxx->regmap = wcd9xxx_regmap_init(&slim->dev,
1305 &wcd9xxx_base_regmap_config);
1306 if (IS_ERR(wcd9xxx->regmap)) {
1307 ret = PTR_ERR(wcd9xxx->regmap);
1308 dev_err(&slim->dev, "%s: Failed to allocate register map: %d\n",
1309 __func__, ret);
1310 goto err_codec;
1311 }
1312
1313 if (!wcd9xxx->wcd_rst_np) {
1314 pdata->use_pinctrl = false;
1315 dev_err(&slim->dev, "%s: pinctrl not used for rst_n\n",
1316 __func__);
1317 goto err_codec;
1318 }
1319
1320 wcd9xxx->num_of_supplies = pdata->num_supplies;
1321 ret = msm_cdc_init_supplies(&slim->dev, &wcd9xxx->supplies,
1322 pdata->regulator,
1323 pdata->num_supplies);
1324 if (!wcd9xxx->supplies) {
1325 dev_err(wcd9xxx->dev, "%s: Cannot init wcd supplies\n",
1326 __func__);
1327 goto err_codec;
1328 }
1329 ret = msm_cdc_enable_static_supplies(wcd9xxx->dev,
1330 wcd9xxx->supplies,
1331 pdata->regulator,
1332 pdata->num_supplies);
1333 if (ret) {
1334 dev_err(wcd9xxx->dev, "%s: wcd static supply enable failed!\n",
1335 __func__);
1336 goto err_codec;
1337 }
1338
1339 /*
1340 * For WCD9335, it takes about 600us for the Vout_A and
1341 * Vout_D to be ready after BUCK_SIDO is powered up.
1342 * SYS_RST_N shouldn't be pulled high during this time
1343 */
1344 if (wcd9xxx->type == WCD9335 || wcd9xxx->type == WCD934X)
1345 usleep_range(600, 650);
1346 else
1347 usleep_range(5, 10);
1348
1349 ret = wcd9xxx_reset(&slim->dev);
1350 if (ret) {
1351 dev_err(&slim->dev, "%s: Resetting Codec failed\n", __func__);
1352 goto err_supplies;
1353 }
1354
1355 ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim, wcd9xxx->slim->e_addr,
1356 ARRAY_SIZE(wcd9xxx->slim->e_addr),
1357 &wcd9xxx->slim->laddr);
1358 if (ret) {
1359 dev_err(&slim->dev, "%s: failed to get slimbus %s logical address: %d\n",
1360 __func__, wcd9xxx->slim->name, ret);
1361 goto err_reset;
1362 }
1363 wcd9xxx->read_dev = wcd9xxx_slim_read_device;
1364 wcd9xxx->write_dev = wcd9xxx_slim_write_device;
1365 wcd9xxx_pgd_la = wcd9xxx->slim->laddr;
1366 wcd9xxx->slim_slave = &pdata->slimbus_slave_device;
1367 if (!wcd9xxx->dev->of_node)
1368 wcd9xxx_assign_irq(&wcd9xxx->core_res,
1369 pdata->irq, pdata->irq_base);
1370
1371 ret = slim_add_device(slim->ctrl, wcd9xxx->slim_slave);
1372 if (ret) {
1373 dev_err(&slim->dev, "%s: error, adding SLIMBUS device failed\n",
1374 __func__);
1375 goto err_reset;
1376 }
1377
1378 ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim_slave,
1379 wcd9xxx->slim_slave->e_addr,
1380 ARRAY_SIZE(wcd9xxx->slim_slave->e_addr),
1381 &wcd9xxx->slim_slave->laddr);
1382 if (ret) {
1383 dev_err(&slim->dev, "%s: failed to get slimbus %s logical address: %d\n",
1384 __func__, wcd9xxx->slim->name, ret);
1385 goto err_slim_add;
1386 }
1387 wcd9xxx_inf_la = wcd9xxx->slim_slave->laddr;
1388 wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_SLIMBUS);
1389
1390 ret = wcd9xxx_device_init(wcd9xxx);
1391 if (ret) {
1392 dev_err(&slim->dev, "%s: error, initializing device failed (%d)\n",
1393 __func__, ret);
1394 goto err_slim_add;
1395 }
1396#ifdef CONFIG_DEBUG_FS
1397 debugCodec = wcd9xxx;
1398
1399 debugfs_wcd9xxx_dent = debugfs_create_dir
1400 ("wcd9xxx_core", 0);
1401 if (!IS_ERR(debugfs_wcd9xxx_dent)) {
1402 debugfs_peek = debugfs_create_file("slimslave_peek",
1403 S_IFREG | 0444, debugfs_wcd9xxx_dent,
1404 (void *) "slimslave_peek", &codec_debug_ops);
1405
1406 debugfs_poke = debugfs_create_file("slimslave_poke",
1407 S_IFREG | 0444, debugfs_wcd9xxx_dent,
1408 (void *) "slimslave_poke", &codec_debug_ops);
1409
1410 debugfs_power_state = debugfs_create_file("power_state",
1411 S_IFREG | 0444, debugfs_wcd9xxx_dent,
1412 (void *) "power_state", &codec_debug_ops);
1413
1414 debugfs_reg_dump = debugfs_create_file("slimslave_reg_dump",
1415 S_IFREG | 0444, debugfs_wcd9xxx_dent,
1416 (void *) "slimslave_reg_dump", &codec_debug_ops);
1417 }
1418#endif
1419
1420 return ret;
1421
1422err_slim_add:
1423 slim_remove_device(wcd9xxx->slim_slave);
1424err_reset:
1425 wcd9xxx_reset_low(wcd9xxx->dev);
1426err_supplies:
1427 msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies,
1428 pdata->regulator,
1429 pdata->num_supplies);
1430err_codec:
1431 slim_set_clientdata(slim, NULL);
1432err:
1433 devm_kfree(&slim->dev, wcd9xxx);
1434 return ret;
1435}
1436static int wcd9xxx_slim_remove(struct slim_device *pdev)
1437{
1438 struct wcd9xxx *wcd9xxx;
1439 struct wcd9xxx_pdata *pdata = pdev->dev.platform_data;
1440
1441#ifdef CONFIG_DEBUG_FS
1442 debugfs_remove_recursive(debugfs_wcd9xxx_dent);
1443#endif
1444 wcd9xxx = slim_get_devicedata(pdev);
1445 wcd9xxx_deinit_slimslave(wcd9xxx);
1446 slim_remove_device(wcd9xxx->slim_slave);
1447 msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies,
1448 pdata->regulator,
1449 pdata->num_supplies);
1450 wcd9xxx_device_exit(wcd9xxx);
1451 slim_set_clientdata(pdev, NULL);
1452 return 0;
1453}
1454
1455static int wcd9xxx_device_up(struct wcd9xxx *wcd9xxx)
1456{
1457 int ret = 0;
1458 struct wcd9xxx_core_resource *wcd9xxx_res = &wcd9xxx->core_res;
1459
1460 dev_info(wcd9xxx->dev, "%s: codec bring up\n", __func__);
1461 wcd9xxx_bringup(wcd9xxx->dev);
1462 ret = wcd9xxx_irq_init(wcd9xxx_res);
1463 if (ret) {
1464 pr_err("%s: wcd9xx_irq_init failed : %d\n", __func__, ret);
1465 } else {
1466 if (wcd9xxx->post_reset)
1467 ret = wcd9xxx->post_reset(wcd9xxx);
1468 }
1469 return ret;
1470}
1471
1472static int wcd9xxx_slim_device_reset(struct slim_device *sldev)
1473{
1474 int ret;
1475 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1476
1477 if (!wcd9xxx) {
1478 pr_err("%s: wcd9xxx is NULL\n", __func__);
1479 return -EINVAL;
1480 }
1481
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301482 /*
1483 * Wait for 500 ms for device down to complete. Observed delay
1484 * of ~200ms for device down to complete after being called,
1485 * due to context switch issue.
1486 */
1487 ret = wait_on_bit_timeout(&wcd9xxx->dev_up, 0,
1488 TASK_INTERRUPTIBLE,
1489 msecs_to_jiffies(500));
1490 if (ret)
1491 pr_err("%s: slim device down not complete in 500 msec\n",
1492 __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301493
1494 mutex_lock(&wcd9xxx->reset_lock);
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301495
1496 dev_info(wcd9xxx->dev, "%s: device reset, dev_up = %lu\n",
1497 __func__, wcd9xxx->dev_up);
1498 if (wcd9xxx->dev_up) {
1499 mutex_unlock(&wcd9xxx->reset_lock);
1500 return 0;
1501 }
1502
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301503 ret = wcd9xxx_reset(wcd9xxx->dev);
1504 if (ret)
1505 dev_err(wcd9xxx->dev, "%s: Resetting Codec failed\n", __func__);
1506 mutex_unlock(&wcd9xxx->reset_lock);
1507
1508 return ret;
1509}
1510
1511static int wcd9xxx_slim_device_up(struct slim_device *sldev)
1512{
1513 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1514 int ret = 0;
1515
1516 if (!wcd9xxx) {
1517 pr_err("%s: wcd9xxx is NULL\n", __func__);
1518 return -EINVAL;
1519 }
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301520 dev_info(wcd9xxx->dev, "%s: slim device up, dev_up = %lu\n",
1521 __func__, wcd9xxx->dev_up);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301522 if (wcd9xxx->dev_up)
1523 return 0;
1524
1525 wcd9xxx->dev_up = true;
1526
1527 mutex_lock(&wcd9xxx->reset_lock);
1528 ret = wcd9xxx_device_up(wcd9xxx);
1529 mutex_unlock(&wcd9xxx->reset_lock);
1530
1531 return ret;
1532}
1533
1534static int wcd9xxx_slim_device_down(struct slim_device *sldev)
1535{
1536 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1537
1538 if (!wcd9xxx) {
1539 pr_err("%s: wcd9xxx is NULL\n", __func__);
1540 return -EINVAL;
1541 }
1542
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301543 mutex_lock(&wcd9xxx->reset_lock);
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301544
1545 dev_info(wcd9xxx->dev, "%s: device down, dev_up = %lu\n",
1546 __func__, wcd9xxx->dev_up);
1547 if (!wcd9xxx->dev_up) {
1548 mutex_unlock(&wcd9xxx->reset_lock);
1549 return 0;
1550 }
1551
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301552 if (wcd9xxx->dev_down)
1553 wcd9xxx->dev_down(wcd9xxx);
1554 wcd9xxx_irq_exit(&wcd9xxx->core_res);
1555 wcd9xxx_reset_low(wcd9xxx->dev);
Asish Bhattacharya84f7f732017-07-25 16:29:27 +05301556 wcd9xxx->dev_up = false;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301557 mutex_unlock(&wcd9xxx->reset_lock);
1558
1559 return 0;
1560}
1561
1562static int wcd9xxx_slim_resume(struct slim_device *sldev)
1563{
1564 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1565
1566 return wcd9xxx_core_res_resume(&wcd9xxx->core_res);
1567}
1568
1569static int wcd9xxx_i2c_resume(struct device *dev)
1570{
1571 struct wcd9xxx *wcd9xxx = dev_get_drvdata(dev);
1572
1573 if (wcd9xxx)
1574 return wcd9xxx_core_res_resume(&wcd9xxx->core_res);
1575 else
1576 return 0;
1577}
1578
1579static int wcd9xxx_slim_suspend(struct slim_device *sldev, pm_message_t pmesg)
1580{
1581 struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
1582
1583 return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg);
1584}
1585
1586static int wcd9xxx_i2c_suspend(struct device *dev)
1587{
1588 struct wcd9xxx *wcd9xxx = dev_get_drvdata(dev);
1589 pm_message_t pmesg = {0};
1590
1591 if (wcd9xxx)
1592 return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg);
1593 else
1594 return 0;
1595}
1596
1597static const struct slim_device_id wcd_slim_device_id[] = {
1598 {"sitar-slim", 0},
1599 {"sitar1p1-slim", 0},
1600 {"tabla-slim", 0},
1601 {"tabla2x-slim", 0},
1602 {"taiko-slim-pgd", 0},
1603 {"tapan-slim-pgd", 0},
1604 {"tomtom-slim-pgd", WCD9330},
1605 {"tasha-slim-pgd", WCD9335},
1606 {"tavil-slim-pgd", WCD934X},
1607 {}
1608};
1609
1610static struct slim_driver wcd_slim_driver = {
1611 .driver = {
1612 .name = "wcd-slim",
1613 .owner = THIS_MODULE,
1614 },
1615 .probe = wcd9xxx_slim_probe,
1616 .remove = wcd9xxx_slim_remove,
1617 .id_table = wcd_slim_device_id,
1618 .resume = wcd9xxx_slim_resume,
1619 .suspend = wcd9xxx_slim_suspend,
1620 .device_up = wcd9xxx_slim_device_up,
1621 .reset_device = wcd9xxx_slim_device_reset,
1622 .device_down = wcd9xxx_slim_device_down,
1623};
1624
1625static struct i2c_device_id wcd9xxx_id_table[] = {
1626 {"wcd9xxx-i2c", WCD9XXX_I2C_TOP_LEVEL},
1627 {"wcd9xxx-i2c", WCD9XXX_I2C_ANALOG},
1628 {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_1},
1629 {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_2},
1630 {}
1631};
1632
1633static struct i2c_device_id tasha_id_table[] = {
1634 {"tasha-i2c-pgd", WCD9XXX_I2C_TOP_LEVEL},
1635 {}
1636};
1637
1638static struct i2c_device_id tabla_id_table[] = {
1639 {"tabla top level", WCD9XXX_I2C_TOP_LEVEL},
1640 {"tabla analog", WCD9XXX_I2C_ANALOG},
1641 {"tabla digital1", WCD9XXX_I2C_DIGITAL_1},
1642 {"tabla digital2", WCD9XXX_I2C_DIGITAL_2},
1643 {}
1644};
1645MODULE_DEVICE_TABLE(i2c, tabla_id_table);
1646
1647static const struct dev_pm_ops wcd9xxx_i2c_pm_ops = {
1648 .suspend = wcd9xxx_i2c_suspend,
1649 .resume = wcd9xxx_i2c_resume,
1650};
1651
1652static struct i2c_driver tabla_i2c_driver = {
1653 .driver = {
1654 .owner = THIS_MODULE,
1655 .name = "tabla-i2c-core",
1656 .pm = &wcd9xxx_i2c_pm_ops,
1657 },
1658 .id_table = tabla_id_table,
1659 .probe = wcd9xxx_i2c_probe,
1660 .remove = wcd9xxx_i2c_remove,
1661};
1662
1663static struct i2c_driver wcd9xxx_i2c_driver = {
1664 .driver = {
1665 .owner = THIS_MODULE,
1666 .name = "wcd9xxx-i2c-core",
1667 .pm = &wcd9xxx_i2c_pm_ops,
1668 },
1669 .id_table = wcd9xxx_id_table,
1670 .probe = wcd9xxx_i2c_probe,
1671 .remove = wcd9xxx_i2c_remove,
1672};
1673
1674static struct i2c_driver wcd9335_i2c_driver = {
1675 .driver = {
1676 .owner = THIS_MODULE,
1677 .name = "tasha-i2c-core",
1678 .pm = &wcd9xxx_i2c_pm_ops,
1679 },
1680 .id_table = tasha_id_table,
1681 .probe = wcd9xxx_i2c_probe,
1682 .remove = wcd9xxx_i2c_remove,
1683};
1684
1685int wcd9xxx_init(void)
1686{
1687 int ret[NUM_WCD9XXX_REG_RET] = {0};
1688 int i = 0;
1689
1690 wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING);
1691
1692 ret[0] = i2c_add_driver(&tabla_i2c_driver);
1693 if (ret[0])
1694 pr_err("%s: Failed to add the tabla2x I2C driver: %d\n",
1695 __func__, ret[0]);
1696
1697 ret[1] = i2c_add_driver(&wcd9xxx_i2c_driver);
1698 if (ret[1])
1699 pr_err("%s: Failed to add the wcd9xxx I2C driver: %d\n",
1700 __func__, ret[1]);
1701
1702 ret[2] = i2c_add_driver(&wcd9335_i2c_driver);
1703 if (ret[2])
1704 pr_err("%s: Failed to add the wcd9335 I2C driver: %d\n",
1705 __func__, ret[2]);
1706
1707 ret[3] = slim_driver_register(&wcd_slim_driver);
1708 if (ret[3])
1709 pr_err("%s: Failed to register wcd SB driver: %d\n",
1710 __func__, ret[3]);
1711
1712 for (i = 0; i < NUM_WCD9XXX_REG_RET; i++) {
1713 if (ret[i])
1714 return ret[i];
1715 }
1716
1717 return 0;
1718}
1719
1720void wcd9xxx_exit(void)
1721{
1722 wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING);
1723
1724 i2c_del_driver(&tabla_i2c_driver);
1725 i2c_del_driver(&wcd9xxx_i2c_driver);
1726 i2c_del_driver(&wcd9335_i2c_driver);
1727 slim_driver_unregister(&wcd_slim_driver);
1728}
1729
1730MODULE_DESCRIPTION("Codec core driver");
1731MODULE_LICENSE("GPL v2");