Vidyakumar Athota | 1309347 | 2018-01-22 18:32:55 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2018, The Linux Foundation. All rights reserved. |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of_device.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/ratelimit.h> |
| 18 | #include <linux/mfd/core.h> |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 19 | #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 Kasam | 605b42f | 2017-08-01 22:02:15 +0530 | [diff] [blame] | 24 | #include <linux/mfd/wcd9xxx/wcd9xxx_registers.h> |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 25 | #include <sound/soc.h> |
Laxminath Kasam | 605b42f | 2017-08-01 22:02:15 +0530 | [diff] [blame] | 26 | #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 Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 32 | #include "wcd9xxx-regmap.h" |
Laxminath Kasam | 605b42f | 2017-08-01 22:02:15 +0530 | [diff] [blame] | 33 | #include "wcd9xxx-slimslave.h" |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 34 | |
| 35 | #define WCD9XXX_REGISTER_START_OFFSET 0x800 |
| 36 | #define WCD9XXX_SLIM_RW_MAX_TRIES 3 |
| 37 | #define SLIMBUS_PRESENT_TIMEOUT 100 |
| 38 | |
| 39 | #define MAX_WCD9XXX_DEVICE 4 |
| 40 | #define WCD9XXX_I2C_GSBI_SLAVE_ID "3-000d" |
| 41 | #define WCD9XXX_I2C_TOP_SLAVE_ADDR 0x0d |
| 42 | #define WCD9XXX_ANALOG_I2C_SLAVE_ADDR 0x77 |
| 43 | #define WCD9XXX_DIGITAL1_I2C_SLAVE_ADDR 0x66 |
| 44 | #define WCD9XXX_DIGITAL2_I2C_SLAVE_ADDR 0x55 |
| 45 | #define WCD9XXX_I2C_TOP_LEVEL 0 |
| 46 | #define WCD9XXX_I2C_ANALOG 1 |
| 47 | #define WCD9XXX_I2C_DIGITAL_1 2 |
| 48 | #define WCD9XXX_I2C_DIGITAL_2 3 |
| 49 | |
| 50 | /* |
| 51 | * Number of return values needs to be checked for each |
| 52 | * registration of Slimbus of I2C bus for each codec |
| 53 | */ |
Karthikeyan Mani | 57d3b66 | 2017-12-11 15:37:06 -0800 | [diff] [blame^] | 54 | #define NUM_WCD9XXX_REG_RET 5 |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 55 | |
| 56 | #define SLIM_USR_MC_REPEAT_CHANGE_VALUE 0x0 |
| 57 | #define SLIM_REPEAT_WRITE_MAX_SLICE 16 |
| 58 | #define REG_BYTES 2 |
| 59 | #define VAL_BYTES 1 |
| 60 | #define WCD9XXX_PAGE_NUM(reg) (((reg) >> 8) & 0xff) |
| 61 | #define WCD9XXX_PAGE_SIZE 256 |
| 62 | |
| 63 | struct 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 | |
| 70 | static struct regmap_config wcd9xxx_base_regmap_config = { |
| 71 | .reg_bits = 16, |
| 72 | .val_bits = 8, |
| 73 | .can_multi_write = true, |
| 74 | }; |
| 75 | |
| 76 | static 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 | |
| 83 | static u8 wcd9xxx_pgd_la; |
| 84 | static u8 wcd9xxx_inf_la; |
| 85 | |
| 86 | static const int wcd9xxx_cdc_types[] = { |
| 87 | [WCD9XXX] = WCD9XXX, |
| 88 | [WCD9330] = WCD9330, |
| 89 | [WCD9335] = WCD9335, |
| 90 | [WCD934X] = WCD934X, |
Vidyakumar Athota | 1309347 | 2018-01-22 18:32:55 -0800 | [diff] [blame] | 91 | [WCD9360] = WCD9360, |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static const struct of_device_id wcd9xxx_of_match[] = { |
Karthikeyan Mani | 57d3b66 | 2017-12-11 15:37:06 -0800 | [diff] [blame^] | 95 | { .compatible = "qcom,tavil-i2c", |
| 96 | .data = (void *)&wcd9xxx_cdc_types[WCD934X]}, |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 97 | { .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 | }; |
| 103 | MODULE_DEVICE_TABLE(of, wcd9xxx_of_match); |
| 104 | |
| 105 | static int wcd9xxx_slim_device_up(struct slim_device *sldev); |
| 106 | static int wcd9xxx_slim_device_down(struct slim_device *sldev); |
| 107 | |
| 108 | struct wcd9xxx_i2c wcd9xxx_modules[MAX_WCD9XXX_DEVICE]; |
| 109 | |
| 110 | static 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 | */ |
| 173 | int 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 | } |
| 195 | EXPORT_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 | */ |
| 206 | int 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 | } |
| 219 | EXPORT_SYMBOL(wcd9xxx_interface_reg_write); |
| 220 | |
| 221 | static 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 Goswami | f6bc713 | 2017-10-20 22:29:42 -0700 | [diff] [blame] | 234 | wcd9xxx->dev, "%s: No read allowed. dev_up = %d\n", |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 235 | __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 | */ |
| 261 | static 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 Goswami | f6bc713 | 2017-10-20 22:29:42 -0700 | [diff] [blame] | 274 | wcd9xxx->dev, "%s: No write allowed. dev_up = %d\n", |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 275 | __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 | |
| 296 | static 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 | */ |
| 326 | int 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 Athota | 1309347 | 2018-01-22 18:32:55 -0800 | [diff] [blame] | 333 | if (wcd9xxx->type == WCD9335 || wcd9xxx->type == WCD934X || |
| 334 | wcd9xxx->type == WCD9360) { |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 335 | ret = wcd9xxx_page_write(wcd9xxx, ®); |
| 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 Goswami | f6bc713 | 2017-10-20 22:29:42 -0700 | [diff] [blame] | 352 | wcd9xxx->dev, "%s: No write allowed. dev_up = %d\n", |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 353 | __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 | |
| 380 | done: |
| 381 | mutex_unlock(&wcd9xxx->io_lock); |
| 382 | |
| 383 | return ret; |
| 384 | } |
| 385 | EXPORT_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 | */ |
| 394 | int 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 | } |
| 406 | EXPORT_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 | */ |
| 418 | int 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 Goswami | f6bc713 | 2017-10-20 22:29:42 -0700 | [diff] [blame] | 433 | wcd9xxx->dev, "%s: No write allowed. dev_up = %d\n", |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 434 | __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, ®); |
| 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); |
| 470 | err: |
| 471 | mutex_unlock(&wcd9xxx->io_lock); |
| 472 | kfree(msgs); |
| 473 | mem_fail: |
| 474 | return ret; |
| 475 | } |
| 476 | EXPORT_SYMBOL(wcd9xxx_slim_bulk_write); |
| 477 | |
| 478 | static 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 | |
| 484 | static 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 | |
| 504 | static 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; |
| 580 | err_irq: |
| 581 | wcd9xxx_irq_exit(&wcd9xxx->core_res); |
| 582 | fail_cdc_fill: |
| 583 | devm_kfree(wcd9xxx->dev, wcd9xxx->codec_type); |
| 584 | wcd9xxx->codec_type = NULL; |
| 585 | err: |
| 586 | wcd9xxx_bringdown(wcd9xxx->dev); |
| 587 | wcd9xxx_core_res_deinit(&wcd9xxx->core_res); |
| 588 | err_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 | |
| 595 | static void wcd9xxx_device_exit(struct wcd9xxx *wcd9xxx) |
| 596 | { |
| 597 | device_init_wakeup(wcd9xxx->dev, false); |
| 598 | wcd9xxx_irq_exit(&wcd9xxx->core_res); |
Laxminath Kasam | 8f7ccc2 | 2017-08-28 17:35:04 +0530 | [diff] [blame] | 599 | mfd_remove_devices(wcd9xxx->dev); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 600 | 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 |
| 612 | struct wcd9xxx *debugCodec; |
| 613 | |
| 614 | static struct dentry *debugfs_wcd9xxx_dent; |
| 615 | static struct dentry *debugfs_peek; |
| 616 | static struct dentry *debugfs_poke; |
| 617 | static struct dentry *debugfs_power_state; |
| 618 | static struct dentry *debugfs_reg_dump; |
| 619 | |
| 620 | static unsigned char read_data; |
| 621 | |
| 622 | static int codec_debug_open(struct inode *inode, struct file *file) |
| 623 | { |
| 624 | file->private_data = inode->i_private; |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | static 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, ¶m1[cnt]) != 0) |
| 643 | return -EINVAL; |
| 644 | |
| 645 | token = strsep(&buf, " "); |
| 646 | } else |
| 647 | return -EINVAL; |
| 648 | } |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static 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 | |
| 675 | copy_err: |
| 676 | return total; |
| 677 | } |
| 678 | |
| 679 | static 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 | |
| 703 | static 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 | |
| 722 | static 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, ¶m, 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 | |
| 761 | error_intf: |
| 762 | return rc; |
| 763 | } |
| 764 | |
| 765 | static 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 | |
| 811 | static 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 | |
| 818 | static 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 | |
| 850 | static 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 | |
| 889 | static 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 = ®_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 | |
| 932 | int 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 | |
| 938 | int 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 | |
| 944 | static 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 | |
| 969 | static 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 | |
| 1143 | err_device_init: |
| 1144 | wcd9xxx_reset_low(wcd9xxx->dev); |
| 1145 | err_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; |
| 1151 | err_codec: |
| 1152 | devm_kfree(&client->dev, wcd9xxx); |
| 1153 | dev_set_drvdata(&client->dev, NULL); |
| 1154 | fail: |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
| 1158 | static 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 | |
| 1172 | static 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 | |
| 1202 | static 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 | |
| 1222 | static 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) { |
| 1251 | dev_info(&slim->dev, "Platform data from device tree\n"); |
| 1252 | 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 Kasam | 38070be | 2017-08-17 18:21:59 +0530 | [diff] [blame] | 1288 | |
| 1289 | if (pdata->has_buck_vsel_gpio) |
| 1290 | msm_cdc_pinctrl_select_active_state(pdata->buck_vsel_ctl_np); |
| 1291 | |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1292 | device_id = slim_get_device_id(slim); |
| 1293 | if (!device_id) { |
| 1294 | dev_err(&slim->dev, "%s: Error, no device id\n", __func__); |
| 1295 | ret = -EINVAL; |
| 1296 | goto err; |
| 1297 | } |
| 1298 | |
| 1299 | wcd9xxx->type = device_id->driver_data; |
| 1300 | dev_info(&slim->dev, "%s: probing for wcd type: %d, name: %s\n", |
| 1301 | __func__, wcd9xxx->type, device_id->name); |
| 1302 | |
| 1303 | /* wcd9xxx members init */ |
| 1304 | wcd9xxx->multi_reg_write = wcd9xxx_slim_multi_reg_write; |
| 1305 | wcd9xxx->slim = slim; |
| 1306 | slim_set_clientdata(slim, wcd9xxx); |
| 1307 | wcd9xxx->reset_gpio = pdata->reset_gpio; |
| 1308 | wcd9xxx->dev = &slim->dev; |
| 1309 | wcd9xxx->mclk_rate = pdata->mclk_rate; |
| 1310 | wcd9xxx->dev_up = true; |
| 1311 | wcd9xxx->wcd_rst_np = pdata->wcd_rst_np; |
| 1312 | |
| 1313 | wcd9xxx->regmap = wcd9xxx_regmap_init(&slim->dev, |
| 1314 | &wcd9xxx_base_regmap_config); |
| 1315 | if (IS_ERR(wcd9xxx->regmap)) { |
| 1316 | ret = PTR_ERR(wcd9xxx->regmap); |
| 1317 | dev_err(&slim->dev, "%s: Failed to allocate register map: %d\n", |
| 1318 | __func__, ret); |
| 1319 | goto err_codec; |
| 1320 | } |
| 1321 | |
| 1322 | if (!wcd9xxx->wcd_rst_np) { |
| 1323 | pdata->use_pinctrl = false; |
| 1324 | dev_err(&slim->dev, "%s: pinctrl not used for rst_n\n", |
| 1325 | __func__); |
| 1326 | goto err_codec; |
| 1327 | } |
| 1328 | |
| 1329 | wcd9xxx->num_of_supplies = pdata->num_supplies; |
| 1330 | ret = msm_cdc_init_supplies(&slim->dev, &wcd9xxx->supplies, |
| 1331 | pdata->regulator, |
| 1332 | pdata->num_supplies); |
| 1333 | if (!wcd9xxx->supplies) { |
| 1334 | dev_err(wcd9xxx->dev, "%s: Cannot init wcd supplies\n", |
| 1335 | __func__); |
| 1336 | goto err_codec; |
| 1337 | } |
| 1338 | ret = msm_cdc_enable_static_supplies(wcd9xxx->dev, |
| 1339 | wcd9xxx->supplies, |
| 1340 | pdata->regulator, |
| 1341 | pdata->num_supplies); |
| 1342 | if (ret) { |
| 1343 | dev_err(wcd9xxx->dev, "%s: wcd static supply enable failed!\n", |
| 1344 | __func__); |
| 1345 | goto err_codec; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * For WCD9335, it takes about 600us for the Vout_A and |
| 1350 | * Vout_D to be ready after BUCK_SIDO is powered up. |
| 1351 | * SYS_RST_N shouldn't be pulled high during this time |
| 1352 | */ |
| 1353 | if (wcd9xxx->type == WCD9335 || wcd9xxx->type == WCD934X) |
| 1354 | usleep_range(600, 650); |
| 1355 | else |
| 1356 | usleep_range(5, 10); |
| 1357 | |
| 1358 | ret = wcd9xxx_reset(&slim->dev); |
| 1359 | if (ret) { |
| 1360 | dev_err(&slim->dev, "%s: Resetting Codec failed\n", __func__); |
| 1361 | goto err_supplies; |
| 1362 | } |
| 1363 | |
| 1364 | ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim, wcd9xxx->slim->e_addr, |
| 1365 | ARRAY_SIZE(wcd9xxx->slim->e_addr), |
| 1366 | &wcd9xxx->slim->laddr); |
| 1367 | if (ret) { |
| 1368 | dev_err(&slim->dev, "%s: failed to get slimbus %s logical address: %d\n", |
| 1369 | __func__, wcd9xxx->slim->name, ret); |
| 1370 | goto err_reset; |
| 1371 | } |
| 1372 | wcd9xxx->read_dev = wcd9xxx_slim_read_device; |
| 1373 | wcd9xxx->write_dev = wcd9xxx_slim_write_device; |
| 1374 | wcd9xxx_pgd_la = wcd9xxx->slim->laddr; |
| 1375 | wcd9xxx->slim_slave = &pdata->slimbus_slave_device; |
| 1376 | if (!wcd9xxx->dev->of_node) |
| 1377 | wcd9xxx_assign_irq(&wcd9xxx->core_res, |
| 1378 | pdata->irq, pdata->irq_base); |
| 1379 | |
| 1380 | ret = slim_add_device(slim->ctrl, wcd9xxx->slim_slave); |
| 1381 | if (ret) { |
| 1382 | dev_err(&slim->dev, "%s: error, adding SLIMBUS device failed\n", |
| 1383 | __func__); |
| 1384 | goto err_reset; |
| 1385 | } |
| 1386 | |
| 1387 | ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim_slave, |
| 1388 | wcd9xxx->slim_slave->e_addr, |
| 1389 | ARRAY_SIZE(wcd9xxx->slim_slave->e_addr), |
| 1390 | &wcd9xxx->slim_slave->laddr); |
| 1391 | if (ret) { |
| 1392 | dev_err(&slim->dev, "%s: failed to get slimbus %s logical address: %d\n", |
| 1393 | __func__, wcd9xxx->slim->name, ret); |
| 1394 | goto err_slim_add; |
| 1395 | } |
| 1396 | wcd9xxx_inf_la = wcd9xxx->slim_slave->laddr; |
| 1397 | wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_SLIMBUS); |
| 1398 | |
| 1399 | ret = wcd9xxx_device_init(wcd9xxx); |
| 1400 | if (ret) { |
| 1401 | dev_err(&slim->dev, "%s: error, initializing device failed (%d)\n", |
| 1402 | __func__, ret); |
| 1403 | goto err_slim_add; |
| 1404 | } |
| 1405 | #ifdef CONFIG_DEBUG_FS |
| 1406 | debugCodec = wcd9xxx; |
| 1407 | |
| 1408 | debugfs_wcd9xxx_dent = debugfs_create_dir |
| 1409 | ("wcd9xxx_core", 0); |
| 1410 | if (!IS_ERR(debugfs_wcd9xxx_dent)) { |
| 1411 | debugfs_peek = debugfs_create_file("slimslave_peek", |
| 1412 | S_IFREG | 0444, debugfs_wcd9xxx_dent, |
| 1413 | (void *) "slimslave_peek", &codec_debug_ops); |
| 1414 | |
| 1415 | debugfs_poke = debugfs_create_file("slimslave_poke", |
| 1416 | S_IFREG | 0444, debugfs_wcd9xxx_dent, |
| 1417 | (void *) "slimslave_poke", &codec_debug_ops); |
| 1418 | |
| 1419 | debugfs_power_state = debugfs_create_file("power_state", |
| 1420 | S_IFREG | 0444, debugfs_wcd9xxx_dent, |
| 1421 | (void *) "power_state", &codec_debug_ops); |
| 1422 | |
| 1423 | debugfs_reg_dump = debugfs_create_file("slimslave_reg_dump", |
| 1424 | S_IFREG | 0444, debugfs_wcd9xxx_dent, |
| 1425 | (void *) "slimslave_reg_dump", &codec_debug_ops); |
| 1426 | } |
| 1427 | #endif |
| 1428 | |
| 1429 | return ret; |
| 1430 | |
| 1431 | err_slim_add: |
| 1432 | slim_remove_device(wcd9xxx->slim_slave); |
| 1433 | err_reset: |
| 1434 | wcd9xxx_reset_low(wcd9xxx->dev); |
| 1435 | err_supplies: |
| 1436 | msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies, |
| 1437 | pdata->regulator, |
| 1438 | pdata->num_supplies); |
| 1439 | err_codec: |
| 1440 | slim_set_clientdata(slim, NULL); |
| 1441 | err: |
| 1442 | devm_kfree(&slim->dev, wcd9xxx); |
| 1443 | return ret; |
| 1444 | } |
| 1445 | static int wcd9xxx_slim_remove(struct slim_device *pdev) |
| 1446 | { |
| 1447 | struct wcd9xxx *wcd9xxx; |
| 1448 | struct wcd9xxx_pdata *pdata = pdev->dev.platform_data; |
| 1449 | |
| 1450 | #ifdef CONFIG_DEBUG_FS |
| 1451 | debugfs_remove_recursive(debugfs_wcd9xxx_dent); |
| 1452 | #endif |
| 1453 | wcd9xxx = slim_get_devicedata(pdev); |
| 1454 | wcd9xxx_deinit_slimslave(wcd9xxx); |
| 1455 | slim_remove_device(wcd9xxx->slim_slave); |
| 1456 | msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies, |
| 1457 | pdata->regulator, |
| 1458 | pdata->num_supplies); |
| 1459 | wcd9xxx_device_exit(wcd9xxx); |
| 1460 | slim_set_clientdata(pdev, NULL); |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | static int wcd9xxx_device_up(struct wcd9xxx *wcd9xxx) |
| 1465 | { |
| 1466 | int ret = 0; |
| 1467 | struct wcd9xxx_core_resource *wcd9xxx_res = &wcd9xxx->core_res; |
| 1468 | |
| 1469 | dev_info(wcd9xxx->dev, "%s: codec bring up\n", __func__); |
| 1470 | wcd9xxx_bringup(wcd9xxx->dev); |
| 1471 | ret = wcd9xxx_irq_init(wcd9xxx_res); |
| 1472 | if (ret) { |
| 1473 | pr_err("%s: wcd9xx_irq_init failed : %d\n", __func__, ret); |
| 1474 | } else { |
| 1475 | if (wcd9xxx->post_reset) |
| 1476 | ret = wcd9xxx->post_reset(wcd9xxx); |
| 1477 | } |
| 1478 | return ret; |
| 1479 | } |
| 1480 | |
| 1481 | static int wcd9xxx_slim_device_reset(struct slim_device *sldev) |
| 1482 | { |
| 1483 | int ret; |
| 1484 | struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev); |
| 1485 | |
| 1486 | if (!wcd9xxx) { |
| 1487 | pr_err("%s: wcd9xxx is NULL\n", __func__); |
| 1488 | return -EINVAL; |
| 1489 | } |
| 1490 | |
Banajit Goswami | f6bc713 | 2017-10-20 22:29:42 -0700 | [diff] [blame] | 1491 | dev_info(wcd9xxx->dev, "%s: device reset, dev_up = %d\n", |
| 1492 | __func__, wcd9xxx->dev_up); |
| 1493 | if (wcd9xxx->dev_up) |
| 1494 | return 0; |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1495 | |
| 1496 | mutex_lock(&wcd9xxx->reset_lock); |
| 1497 | ret = wcd9xxx_reset(wcd9xxx->dev); |
| 1498 | if (ret) |
| 1499 | dev_err(wcd9xxx->dev, "%s: Resetting Codec failed\n", __func__); |
| 1500 | mutex_unlock(&wcd9xxx->reset_lock); |
| 1501 | |
| 1502 | return ret; |
| 1503 | } |
| 1504 | |
| 1505 | static int wcd9xxx_slim_device_up(struct slim_device *sldev) |
| 1506 | { |
| 1507 | struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev); |
| 1508 | int ret = 0; |
| 1509 | |
| 1510 | if (!wcd9xxx) { |
| 1511 | pr_err("%s: wcd9xxx is NULL\n", __func__); |
| 1512 | return -EINVAL; |
| 1513 | } |
Banajit Goswami | f6bc713 | 2017-10-20 22:29:42 -0700 | [diff] [blame] | 1514 | dev_info(wcd9xxx->dev, "%s: slim device up, dev_up = %d\n", |
| 1515 | __func__, wcd9xxx->dev_up); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1516 | if (wcd9xxx->dev_up) |
| 1517 | return 0; |
| 1518 | |
| 1519 | wcd9xxx->dev_up = true; |
| 1520 | |
| 1521 | mutex_lock(&wcd9xxx->reset_lock); |
| 1522 | ret = wcd9xxx_device_up(wcd9xxx); |
| 1523 | mutex_unlock(&wcd9xxx->reset_lock); |
| 1524 | |
| 1525 | return ret; |
| 1526 | } |
| 1527 | |
| 1528 | static int wcd9xxx_slim_device_down(struct slim_device *sldev) |
| 1529 | { |
| 1530 | struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev); |
| 1531 | |
| 1532 | if (!wcd9xxx) { |
| 1533 | pr_err("%s: wcd9xxx is NULL\n", __func__); |
| 1534 | return -EINVAL; |
| 1535 | } |
| 1536 | |
Banajit Goswami | f6bc713 | 2017-10-20 22:29:42 -0700 | [diff] [blame] | 1537 | dev_info(wcd9xxx->dev, "%s: device down, dev_up = %d\n", |
| 1538 | __func__, wcd9xxx->dev_up); |
| 1539 | if (!wcd9xxx->dev_up) |
Asish Bhattacharya | 84f7f73 | 2017-07-25 16:29:27 +0530 | [diff] [blame] | 1540 | return 0; |
Asish Bhattacharya | 84f7f73 | 2017-07-25 16:29:27 +0530 | [diff] [blame] | 1541 | |
Banajit Goswami | f6bc713 | 2017-10-20 22:29:42 -0700 | [diff] [blame] | 1542 | wcd9xxx->dev_up = false; |
| 1543 | |
| 1544 | mutex_lock(&wcd9xxx->reset_lock); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1545 | if (wcd9xxx->dev_down) |
| 1546 | wcd9xxx->dev_down(wcd9xxx); |
| 1547 | wcd9xxx_irq_exit(&wcd9xxx->core_res); |
| 1548 | wcd9xxx_reset_low(wcd9xxx->dev); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1549 | mutex_unlock(&wcd9xxx->reset_lock); |
| 1550 | |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | static int wcd9xxx_slim_resume(struct slim_device *sldev) |
| 1555 | { |
| 1556 | struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev); |
| 1557 | |
| 1558 | return wcd9xxx_core_res_resume(&wcd9xxx->core_res); |
| 1559 | } |
| 1560 | |
| 1561 | static int wcd9xxx_i2c_resume(struct device *dev) |
| 1562 | { |
| 1563 | struct wcd9xxx *wcd9xxx = dev_get_drvdata(dev); |
| 1564 | |
| 1565 | if (wcd9xxx) |
| 1566 | return wcd9xxx_core_res_resume(&wcd9xxx->core_res); |
| 1567 | else |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
| 1571 | static int wcd9xxx_slim_suspend(struct slim_device *sldev, pm_message_t pmesg) |
| 1572 | { |
| 1573 | struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev); |
| 1574 | |
| 1575 | return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg); |
| 1576 | } |
| 1577 | |
| 1578 | static int wcd9xxx_i2c_suspend(struct device *dev) |
| 1579 | { |
| 1580 | struct wcd9xxx *wcd9xxx = dev_get_drvdata(dev); |
| 1581 | pm_message_t pmesg = {0}; |
| 1582 | |
| 1583 | if (wcd9xxx) |
| 1584 | return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg); |
| 1585 | else |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
| 1589 | static const struct slim_device_id wcd_slim_device_id[] = { |
| 1590 | {"sitar-slim", 0}, |
| 1591 | {"sitar1p1-slim", 0}, |
| 1592 | {"tabla-slim", 0}, |
| 1593 | {"tabla2x-slim", 0}, |
| 1594 | {"taiko-slim-pgd", 0}, |
| 1595 | {"tapan-slim-pgd", 0}, |
| 1596 | {"tomtom-slim-pgd", WCD9330}, |
| 1597 | {"tasha-slim-pgd", WCD9335}, |
| 1598 | {"tavil-slim-pgd", WCD934X}, |
| 1599 | {} |
| 1600 | }; |
| 1601 | |
| 1602 | static struct slim_driver wcd_slim_driver = { |
| 1603 | .driver = { |
| 1604 | .name = "wcd-slim", |
| 1605 | .owner = THIS_MODULE, |
| 1606 | }, |
| 1607 | .probe = wcd9xxx_slim_probe, |
| 1608 | .remove = wcd9xxx_slim_remove, |
| 1609 | .id_table = wcd_slim_device_id, |
| 1610 | .resume = wcd9xxx_slim_resume, |
| 1611 | .suspend = wcd9xxx_slim_suspend, |
| 1612 | .device_up = wcd9xxx_slim_device_up, |
| 1613 | .reset_device = wcd9xxx_slim_device_reset, |
| 1614 | .device_down = wcd9xxx_slim_device_down, |
| 1615 | }; |
| 1616 | |
| 1617 | static struct i2c_device_id wcd9xxx_id_table[] = { |
| 1618 | {"wcd9xxx-i2c", WCD9XXX_I2C_TOP_LEVEL}, |
| 1619 | {"wcd9xxx-i2c", WCD9XXX_I2C_ANALOG}, |
| 1620 | {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_1}, |
| 1621 | {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_2}, |
| 1622 | {} |
| 1623 | }; |
| 1624 | |
| 1625 | static struct i2c_device_id tasha_id_table[] = { |
| 1626 | {"tasha-i2c-pgd", WCD9XXX_I2C_TOP_LEVEL}, |
| 1627 | {} |
| 1628 | }; |
| 1629 | |
Karthikeyan Mani | 57d3b66 | 2017-12-11 15:37:06 -0800 | [diff] [blame^] | 1630 | static struct i2c_device_id tavil_id_table[] = { |
| 1631 | {"tavil-i2c", WCD9XXX_I2C_TOP_LEVEL}, |
| 1632 | {} |
| 1633 | }; |
| 1634 | |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1635 | static struct i2c_device_id tabla_id_table[] = { |
| 1636 | {"tabla top level", WCD9XXX_I2C_TOP_LEVEL}, |
| 1637 | {"tabla analog", WCD9XXX_I2C_ANALOG}, |
| 1638 | {"tabla digital1", WCD9XXX_I2C_DIGITAL_1}, |
| 1639 | {"tabla digital2", WCD9XXX_I2C_DIGITAL_2}, |
| 1640 | {} |
| 1641 | }; |
| 1642 | MODULE_DEVICE_TABLE(i2c, tabla_id_table); |
| 1643 | |
| 1644 | static const struct dev_pm_ops wcd9xxx_i2c_pm_ops = { |
| 1645 | .suspend = wcd9xxx_i2c_suspend, |
| 1646 | .resume = wcd9xxx_i2c_resume, |
| 1647 | }; |
| 1648 | |
| 1649 | static struct i2c_driver tabla_i2c_driver = { |
| 1650 | .driver = { |
| 1651 | .owner = THIS_MODULE, |
| 1652 | .name = "tabla-i2c-core", |
| 1653 | .pm = &wcd9xxx_i2c_pm_ops, |
| 1654 | }, |
| 1655 | .id_table = tabla_id_table, |
| 1656 | .probe = wcd9xxx_i2c_probe, |
| 1657 | .remove = wcd9xxx_i2c_remove, |
| 1658 | }; |
| 1659 | |
| 1660 | static struct i2c_driver wcd9xxx_i2c_driver = { |
| 1661 | .driver = { |
| 1662 | .owner = THIS_MODULE, |
| 1663 | .name = "wcd9xxx-i2c-core", |
| 1664 | .pm = &wcd9xxx_i2c_pm_ops, |
| 1665 | }, |
| 1666 | .id_table = wcd9xxx_id_table, |
| 1667 | .probe = wcd9xxx_i2c_probe, |
| 1668 | .remove = wcd9xxx_i2c_remove, |
| 1669 | }; |
| 1670 | |
| 1671 | static struct i2c_driver wcd9335_i2c_driver = { |
| 1672 | .driver = { |
| 1673 | .owner = THIS_MODULE, |
| 1674 | .name = "tasha-i2c-core", |
| 1675 | .pm = &wcd9xxx_i2c_pm_ops, |
| 1676 | }, |
| 1677 | .id_table = tasha_id_table, |
| 1678 | .probe = wcd9xxx_i2c_probe, |
| 1679 | .remove = wcd9xxx_i2c_remove, |
| 1680 | }; |
| 1681 | |
Karthikeyan Mani | 57d3b66 | 2017-12-11 15:37:06 -0800 | [diff] [blame^] | 1682 | static struct i2c_driver wcd934x_i2c_driver = { |
| 1683 | .driver = { |
| 1684 | .owner = THIS_MODULE, |
| 1685 | .name = "tavil-i2c-core", |
| 1686 | .pm = &wcd9xxx_i2c_pm_ops, |
| 1687 | }, |
| 1688 | .id_table = tavil_id_table, |
| 1689 | .probe = wcd9xxx_i2c_probe, |
| 1690 | .remove = wcd9xxx_i2c_remove, |
| 1691 | }; |
| 1692 | |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1693 | int wcd9xxx_init(void) |
| 1694 | { |
| 1695 | int ret[NUM_WCD9XXX_REG_RET] = {0}; |
| 1696 | int i = 0; |
| 1697 | |
| 1698 | wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING); |
| 1699 | |
| 1700 | ret[0] = i2c_add_driver(&tabla_i2c_driver); |
| 1701 | if (ret[0]) |
| 1702 | pr_err("%s: Failed to add the tabla2x I2C driver: %d\n", |
| 1703 | __func__, ret[0]); |
| 1704 | |
| 1705 | ret[1] = i2c_add_driver(&wcd9xxx_i2c_driver); |
| 1706 | if (ret[1]) |
| 1707 | pr_err("%s: Failed to add the wcd9xxx I2C driver: %d\n", |
| 1708 | __func__, ret[1]); |
| 1709 | |
| 1710 | ret[2] = i2c_add_driver(&wcd9335_i2c_driver); |
| 1711 | if (ret[2]) |
| 1712 | pr_err("%s: Failed to add the wcd9335 I2C driver: %d\n", |
| 1713 | __func__, ret[2]); |
| 1714 | |
| 1715 | ret[3] = slim_driver_register(&wcd_slim_driver); |
| 1716 | if (ret[3]) |
| 1717 | pr_err("%s: Failed to register wcd SB driver: %d\n", |
| 1718 | __func__, ret[3]); |
| 1719 | |
Karthikeyan Mani | 57d3b66 | 2017-12-11 15:37:06 -0800 | [diff] [blame^] | 1720 | ret[4] = i2c_add_driver(&wcd934x_i2c_driver); |
| 1721 | if (ret[4]) |
| 1722 | pr_err("%s: Failed to add the wcd934x I2C driver: %d\n", |
| 1723 | __func__, ret[4]); |
| 1724 | |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1725 | for (i = 0; i < NUM_WCD9XXX_REG_RET; i++) { |
| 1726 | if (ret[i]) |
| 1727 | return ret[i]; |
| 1728 | } |
| 1729 | |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
| 1733 | void wcd9xxx_exit(void) |
| 1734 | { |
| 1735 | wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING); |
| 1736 | |
| 1737 | i2c_del_driver(&tabla_i2c_driver); |
| 1738 | i2c_del_driver(&wcd9xxx_i2c_driver); |
| 1739 | i2c_del_driver(&wcd9335_i2c_driver); |
Karthikeyan Mani | 57d3b66 | 2017-12-11 15:37:06 -0800 | [diff] [blame^] | 1740 | i2c_del_driver(&wcd934x_i2c_driver); |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1741 | slim_driver_unregister(&wcd_slim_driver); |
| 1742 | } |
| 1743 | |
| 1744 | MODULE_DESCRIPTION("Codec core driver"); |
| 1745 | MODULE_LICENSE("GPL v2"); |