wcd9xxx-core.c 45 KB

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