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