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