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