wcd9xxx-core.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  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. intf_type = wcd9xxx_get_intf_type();
  1132. wcd9xxx = devm_kzalloc(&slim->dev, sizeof(struct wcd9xxx),
  1133. GFP_KERNEL);
  1134. if (!wcd9xxx) {
  1135. ret = -ENOMEM;
  1136. goto err;
  1137. }
  1138. if (!slim) {
  1139. ret = -EINVAL;
  1140. goto err;
  1141. }
  1142. if (intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
  1143. dev_dbg(&slim->dev, "%s:Codec is detected in I2C mode\n",
  1144. __func__);
  1145. ret = -ENODEV;
  1146. goto err;
  1147. }
  1148. if (slim->dev.of_node) {
  1149. dev_dbg(&slim->dev, "Platform data from device tree\n");
  1150. pdata = wcd9xxx_populate_dt_data(&slim->dev);
  1151. if (!pdata) {
  1152. dev_err(&slim->dev,
  1153. "%s: Fail to obtain pdata from device tree\n",
  1154. __func__);
  1155. ret = -EINVAL;
  1156. goto err;
  1157. }
  1158. ret = wcd9xxx_dt_parse_slim_interface_dev_info(&slim->dev,
  1159. &pdata->slimbus_slave_device);
  1160. if (ret) {
  1161. dev_err(&slim->dev, "Error, parsing slim interface\n");
  1162. devm_kfree(&slim->dev, pdata);
  1163. ret = -EINVAL;
  1164. goto err;
  1165. }
  1166. slim->dev.platform_data = pdata;
  1167. } else {
  1168. dev_info(&slim->dev, "Platform data from board file\n");
  1169. pdata = slim->dev.platform_data;
  1170. }
  1171. if (!pdata) {
  1172. dev_err(&slim->dev, "Error, no platform data\n");
  1173. ret = -EINVAL;
  1174. goto err;
  1175. }
  1176. if (!slim->ctrl) {
  1177. dev_err(&slim->dev, "%s: Error, no SLIMBUS control data\n",
  1178. __func__);
  1179. ret = -EINVAL;
  1180. goto err_codec;
  1181. }
  1182. if (pdata->has_buck_vsel_gpio)
  1183. msm_cdc_pinctrl_select_active_state(pdata->buck_vsel_ctl_np);
  1184. if (pdata->has_micb_supply_en_gpio)
  1185. msm_cdc_pinctrl_select_active_state(pdata->micb_en_ctl);
  1186. device_id = slim_get_device_id(slim);
  1187. if (!device_id) {
  1188. dev_err(&slim->dev, "%s: Error, no device id\n", __func__);
  1189. ret = -EINVAL;
  1190. goto err;
  1191. }
  1192. wcd9xxx->type = device_id->driver_data;
  1193. dev_info(&slim->dev, "%s: probing for wcd type: %d, name: %s\n",
  1194. __func__, wcd9xxx->type, device_id->name);
  1195. /* wcd9xxx members init */
  1196. wcd9xxx->multi_reg_write = wcd9xxx_slim_multi_reg_write;
  1197. wcd9xxx->slim = slim;
  1198. slim_set_clientdata(slim, wcd9xxx);
  1199. wcd9xxx->reset_gpio = pdata->reset_gpio;
  1200. wcd9xxx->dev = &slim->dev;
  1201. wcd9xxx->mclk_rate = pdata->mclk_rate;
  1202. wcd9xxx->dev_up = true;
  1203. wcd9xxx->wcd_rst_np = pdata->wcd_rst_np;
  1204. wcd9xxx->regmap = wcd9xxx_regmap_init(&slim->dev,
  1205. &wcd9xxx_base_regmap_config);
  1206. if (IS_ERR(wcd9xxx->regmap)) {
  1207. ret = PTR_ERR(wcd9xxx->regmap);
  1208. dev_err(&slim->dev, "%s: Failed to allocate register map: %d\n",
  1209. __func__, ret);
  1210. goto err_codec;
  1211. }
  1212. if (!wcd9xxx->wcd_rst_np) {
  1213. pdata->use_pinctrl = false;
  1214. dev_err(&slim->dev, "%s: pinctrl not used for rst_n\n",
  1215. __func__);
  1216. goto err_codec;
  1217. }
  1218. wcd9xxx->num_of_supplies = pdata->num_supplies;
  1219. ret = msm_cdc_init_supplies_v2(&slim->dev, &wcd9xxx->supplies,
  1220. pdata->regulator,
  1221. pdata->num_supplies,
  1222. pdata->vote_regulator_on_demand);
  1223. if (!wcd9xxx->supplies) {
  1224. dev_err(wcd9xxx->dev, "%s: Cannot init wcd supplies\n",
  1225. __func__);
  1226. goto err_codec;
  1227. }
  1228. ret = msm_cdc_enable_static_supplies(wcd9xxx->dev,
  1229. wcd9xxx->supplies,
  1230. pdata->regulator,
  1231. pdata->num_supplies);
  1232. if (ret) {
  1233. dev_err(wcd9xxx->dev, "%s: wcd static supply enable failed!\n",
  1234. __func__);
  1235. goto err_codec;
  1236. }
  1237. /*
  1238. * For WCD9335, it takes about 600us for the Vout_A and
  1239. * Vout_D to be ready after BUCK_SIDO is powered up.
  1240. * SYS_RST_N shouldn't be pulled high during this time
  1241. */
  1242. if (wcd9xxx->type == WCD9335 || wcd9xxx->type == WCD934X)
  1243. usleep_range(600, 650);
  1244. else
  1245. usleep_range(5, 10);
  1246. ret = wcd9xxx_reset(&slim->dev);
  1247. if (ret) {
  1248. dev_err(&slim->dev, "%s: Resetting Codec failed\n", __func__);
  1249. goto err_supplies;
  1250. }
  1251. ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim, wcd9xxx->slim->e_addr,
  1252. ARRAY_SIZE(wcd9xxx->slim->e_addr),
  1253. &wcd9xxx->slim->laddr);
  1254. if (ret) {
  1255. dev_err(&slim->dev, "%s: failed to get slimbus %s logical address: %d\n",
  1256. __func__, wcd9xxx->slim->name, ret);
  1257. goto err_reset;
  1258. }
  1259. wcd9xxx->read_dev = wcd9xxx_slim_read_device;
  1260. wcd9xxx->write_dev = wcd9xxx_slim_write_device;
  1261. wcd9xxx_pgd_la = wcd9xxx->slim->laddr;
  1262. wcd9xxx->slim_slave = &pdata->slimbus_slave_device;
  1263. if (!wcd9xxx->dev->of_node)
  1264. wcd9xxx_assign_irq(&wcd9xxx->core_res,
  1265. pdata->irq, pdata->irq_base);
  1266. ret = slim_add_device(slim->ctrl, wcd9xxx->slim_slave);
  1267. if (ret) {
  1268. dev_err(&slim->dev, "%s: error, adding SLIMBUS device failed\n",
  1269. __func__);
  1270. goto err_reset;
  1271. }
  1272. ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim_slave,
  1273. wcd9xxx->slim_slave->e_addr,
  1274. ARRAY_SIZE(wcd9xxx->slim_slave->e_addr),
  1275. &wcd9xxx->slim_slave->laddr);
  1276. if (ret) {
  1277. dev_err(&slim->dev, "%s: failed to get slimbus %s logical address: %d\n",
  1278. __func__, wcd9xxx->slim->name, ret);
  1279. goto err_slim_add;
  1280. }
  1281. wcd9xxx_inf_la = wcd9xxx->slim_slave->laddr;
  1282. wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_SLIMBUS);
  1283. ret = wcd9xxx_device_init(wcd9xxx);
  1284. if (ret) {
  1285. dev_err(&slim->dev, "%s: error, initializing device failed (%d)\n",
  1286. __func__, ret);
  1287. goto err_slim_add;
  1288. }
  1289. #ifdef CONFIG_DEBUG_FS
  1290. debugCodec = wcd9xxx;
  1291. debugfs_wcd9xxx_dent = debugfs_create_dir
  1292. ("wcd9xxx_core", 0);
  1293. if (!IS_ERR(debugfs_wcd9xxx_dent)) {
  1294. debugfs_peek = debugfs_create_file("slimslave_peek",
  1295. S_IFREG | 0444, debugfs_wcd9xxx_dent,
  1296. (void *) "slimslave_peek", &codec_debug_ops);
  1297. debugfs_poke = debugfs_create_file("slimslave_poke",
  1298. S_IFREG | 0444, debugfs_wcd9xxx_dent,
  1299. (void *) "slimslave_poke", &codec_debug_ops);
  1300. debugfs_power_state = debugfs_create_file("power_state",
  1301. S_IFREG | 0444, debugfs_wcd9xxx_dent,
  1302. (void *) "power_state", &codec_debug_ops);
  1303. debugfs_reg_dump = debugfs_create_file("slimslave_reg_dump",
  1304. S_IFREG | 0444, debugfs_wcd9xxx_dent,
  1305. (void *) "slimslave_reg_dump", &codec_debug_ops);
  1306. }
  1307. #endif
  1308. return ret;
  1309. err_slim_add:
  1310. slim_remove_device(wcd9xxx->slim_slave);
  1311. err_reset:
  1312. wcd9xxx_reset_low(wcd9xxx->dev);
  1313. err_supplies:
  1314. msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies,
  1315. pdata->regulator,
  1316. pdata->num_supplies);
  1317. err_codec:
  1318. slim_set_clientdata(slim, NULL);
  1319. err:
  1320. devm_kfree(&slim->dev, wcd9xxx);
  1321. return ret;
  1322. }
  1323. static int wcd9xxx_slim_remove(struct slim_device *pdev)
  1324. {
  1325. struct wcd9xxx *wcd9xxx;
  1326. struct wcd9xxx_pdata *pdata = pdev->dev.platform_data;
  1327. #ifdef CONFIG_DEBUG_FS
  1328. debugfs_remove_recursive(debugfs_wcd9xxx_dent);
  1329. #endif
  1330. wcd9xxx = slim_get_devicedata(pdev);
  1331. wcd9xxx_deinit_slimslave(wcd9xxx);
  1332. slim_remove_device(wcd9xxx->slim_slave);
  1333. msm_cdc_release_supplies(wcd9xxx->dev, wcd9xxx->supplies,
  1334. pdata->regulator,
  1335. pdata->num_supplies);
  1336. wcd9xxx_device_exit(wcd9xxx);
  1337. slim_set_clientdata(pdev, NULL);
  1338. return 0;
  1339. }
  1340. static int wcd9xxx_device_up(struct wcd9xxx *wcd9xxx)
  1341. {
  1342. int ret = 0;
  1343. struct wcd9xxx_core_resource *wcd9xxx_res = &wcd9xxx->core_res;
  1344. dev_info(wcd9xxx->dev, "%s: codec bring up\n", __func__);
  1345. wcd9xxx_bringup(wcd9xxx->dev);
  1346. ret = wcd9xxx_irq_init(wcd9xxx_res);
  1347. if (ret) {
  1348. pr_err("%s: wcd9xx_irq_init failed : %d\n", __func__, ret);
  1349. } else {
  1350. if (wcd9xxx->post_reset)
  1351. ret = wcd9xxx->post_reset(wcd9xxx);
  1352. }
  1353. return ret;
  1354. }
  1355. static int wcd9xxx_slim_device_reset(struct slim_device *sldev)
  1356. {
  1357. int ret;
  1358. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1359. if (!wcd9xxx) {
  1360. pr_err("%s: wcd9xxx is NULL\n", __func__);
  1361. return -EINVAL;
  1362. }
  1363. dev_info(wcd9xxx->dev, "%s: device reset, dev_up = %d\n",
  1364. __func__, wcd9xxx->dev_up);
  1365. if (wcd9xxx->dev_up)
  1366. return 0;
  1367. mutex_lock(&wcd9xxx->reset_lock);
  1368. ret = wcd9xxx_reset(wcd9xxx->dev);
  1369. if (ret)
  1370. dev_err(wcd9xxx->dev, "%s: Resetting Codec failed\n", __func__);
  1371. mutex_unlock(&wcd9xxx->reset_lock);
  1372. return ret;
  1373. }
  1374. static int wcd9xxx_slim_device_up(struct slim_device *sldev)
  1375. {
  1376. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1377. int ret = 0;
  1378. if (!wcd9xxx) {
  1379. pr_err("%s: wcd9xxx is NULL\n", __func__);
  1380. return -EINVAL;
  1381. }
  1382. dev_info(wcd9xxx->dev, "%s: slim device up, dev_up = %d\n",
  1383. __func__, wcd9xxx->dev_up);
  1384. if (wcd9xxx->dev_up)
  1385. return 0;
  1386. wcd9xxx->dev_up = true;
  1387. mutex_lock(&wcd9xxx->reset_lock);
  1388. ret = wcd9xxx_device_up(wcd9xxx);
  1389. mutex_unlock(&wcd9xxx->reset_lock);
  1390. return ret;
  1391. }
  1392. static int wcd9xxx_slim_device_down(struct slim_device *sldev)
  1393. {
  1394. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1395. if (!wcd9xxx) {
  1396. pr_err("%s: wcd9xxx is NULL\n", __func__);
  1397. return -EINVAL;
  1398. }
  1399. dev_info(wcd9xxx->dev, "%s: device down, dev_up = %d\n",
  1400. __func__, wcd9xxx->dev_up);
  1401. if (!wcd9xxx->dev_up)
  1402. return 0;
  1403. wcd9xxx->dev_up = false;
  1404. mutex_lock(&wcd9xxx->reset_lock);
  1405. if (wcd9xxx->dev_down)
  1406. wcd9xxx->dev_down(wcd9xxx);
  1407. wcd9xxx_irq_exit(&wcd9xxx->core_res);
  1408. wcd9xxx_reset_low(wcd9xxx->dev);
  1409. mutex_unlock(&wcd9xxx->reset_lock);
  1410. return 0;
  1411. }
  1412. static int wcd9xxx_slim_resume(struct slim_device *sldev)
  1413. {
  1414. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1415. return wcd9xxx_core_res_resume(&wcd9xxx->core_res);
  1416. }
  1417. static int wcd9xxx_i2c_resume(struct device *dev)
  1418. {
  1419. struct wcd9xxx *wcd9xxx = dev_get_drvdata(dev);
  1420. if (wcd9xxx)
  1421. return wcd9xxx_core_res_resume(&wcd9xxx->core_res);
  1422. else
  1423. return 0;
  1424. }
  1425. static int wcd9xxx_slim_suspend(struct slim_device *sldev, pm_message_t pmesg)
  1426. {
  1427. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1428. return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg);
  1429. }
  1430. static int wcd9xxx_i2c_suspend(struct device *dev)
  1431. {
  1432. struct wcd9xxx *wcd9xxx = dev_get_drvdata(dev);
  1433. pm_message_t pmesg = {0};
  1434. if (wcd9xxx)
  1435. return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg);
  1436. else
  1437. return 0;
  1438. }
  1439. static const struct slim_device_id wcd_slim_device_id[] = {
  1440. {"sitar-slim", 0},
  1441. {"sitar1p1-slim", 0},
  1442. {"tabla-slim", 0},
  1443. {"tabla2x-slim", 0},
  1444. {"taiko-slim-pgd", 0},
  1445. {"tapan-slim-pgd", 0},
  1446. {"tomtom-slim-pgd", WCD9330},
  1447. {"tasha-slim-pgd", WCD9335},
  1448. {"tavil-slim-pgd", WCD934X},
  1449. {}
  1450. };
  1451. static struct slim_driver wcd_slim_driver = {
  1452. .driver = {
  1453. .name = "wcd-slim",
  1454. .owner = THIS_MODULE,
  1455. },
  1456. .probe = wcd9xxx_slim_probe,
  1457. .remove = wcd9xxx_slim_remove,
  1458. .id_table = wcd_slim_device_id,
  1459. .resume = wcd9xxx_slim_resume,
  1460. .suspend = wcd9xxx_slim_suspend,
  1461. .device_up = wcd9xxx_slim_device_up,
  1462. .reset_device = wcd9xxx_slim_device_reset,
  1463. .device_down = wcd9xxx_slim_device_down,
  1464. };
  1465. static struct i2c_device_id wcd9xxx_id_table[] = {
  1466. {"wcd9xxx-i2c", WCD9XXX_I2C_TOP_LEVEL},
  1467. {"wcd9xxx-i2c", WCD9XXX_I2C_ANALOG},
  1468. {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_1},
  1469. {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_2},
  1470. {}
  1471. };
  1472. static struct i2c_device_id tasha_id_table[] = {
  1473. {"tasha-i2c-pgd", WCD9XXX_I2C_TOP_LEVEL},
  1474. {}
  1475. };
  1476. static struct i2c_device_id tavil_id_table[] = {
  1477. {"tavil-i2c", WCD9XXX_I2C_TOP_LEVEL},
  1478. {}
  1479. };
  1480. static struct i2c_device_id tabla_id_table[] = {
  1481. {"tabla top level", WCD9XXX_I2C_TOP_LEVEL},
  1482. {"tabla analog", WCD9XXX_I2C_ANALOG},
  1483. {"tabla digital1", WCD9XXX_I2C_DIGITAL_1},
  1484. {"tabla digital2", WCD9XXX_I2C_DIGITAL_2},
  1485. {}
  1486. };
  1487. MODULE_DEVICE_TABLE(i2c, tabla_id_table);
  1488. static const struct dev_pm_ops wcd9xxx_i2c_pm_ops = {
  1489. .suspend = wcd9xxx_i2c_suspend,
  1490. .resume = wcd9xxx_i2c_resume,
  1491. };
  1492. static struct i2c_driver tabla_i2c_driver = {
  1493. .driver = {
  1494. .owner = THIS_MODULE,
  1495. .name = "tabla-i2c-core",
  1496. .pm = &wcd9xxx_i2c_pm_ops,
  1497. },
  1498. .id_table = tabla_id_table,
  1499. .probe = wcd9xxx_i2c_probe,
  1500. .remove = wcd9xxx_i2c_remove,
  1501. };
  1502. static struct i2c_driver wcd9xxx_i2c_driver = {
  1503. .driver = {
  1504. .owner = THIS_MODULE,
  1505. .name = "wcd9xxx-i2c-core",
  1506. .pm = &wcd9xxx_i2c_pm_ops,
  1507. },
  1508. .id_table = wcd9xxx_id_table,
  1509. .probe = wcd9xxx_i2c_probe,
  1510. .remove = wcd9xxx_i2c_remove,
  1511. };
  1512. static struct i2c_driver wcd9335_i2c_driver = {
  1513. .driver = {
  1514. .owner = THIS_MODULE,
  1515. .name = "tasha-i2c-core",
  1516. .pm = &wcd9xxx_i2c_pm_ops,
  1517. },
  1518. .id_table = tasha_id_table,
  1519. .probe = wcd9xxx_i2c_probe,
  1520. .remove = wcd9xxx_i2c_remove,
  1521. };
  1522. static struct i2c_driver wcd934x_i2c_driver = {
  1523. .driver = {
  1524. .owner = THIS_MODULE,
  1525. .name = "tavil-i2c-core",
  1526. .pm = &wcd9xxx_i2c_pm_ops,
  1527. },
  1528. .id_table = tavil_id_table,
  1529. .probe = wcd9xxx_i2c_probe,
  1530. .remove = wcd9xxx_i2c_remove,
  1531. };
  1532. int wcd9xxx_init(void)
  1533. {
  1534. int ret[NUM_WCD9XXX_REG_RET] = {0};
  1535. int i = 0;
  1536. wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING);
  1537. ret[0] = i2c_add_driver(&tabla_i2c_driver);
  1538. if (ret[0])
  1539. pr_err("%s: Failed to add the tabla2x I2C driver: %d\n",
  1540. __func__, ret[0]);
  1541. ret[1] = i2c_add_driver(&wcd9xxx_i2c_driver);
  1542. if (ret[1])
  1543. pr_err("%s: Failed to add the wcd9xxx I2C driver: %d\n",
  1544. __func__, ret[1]);
  1545. ret[2] = i2c_add_driver(&wcd9335_i2c_driver);
  1546. if (ret[2])
  1547. pr_err("%s: Failed to add the wcd9335 I2C driver: %d\n",
  1548. __func__, ret[2]);
  1549. ret[3] = slim_driver_register(&wcd_slim_driver);
  1550. if (ret[3])
  1551. pr_err("%s: Failed to register wcd SB driver: %d\n",
  1552. __func__, ret[3]);
  1553. ret[4] = i2c_add_driver(&wcd934x_i2c_driver);
  1554. if (ret[4])
  1555. pr_err("%s: Failed to add the wcd934x I2C driver: %d\n",
  1556. __func__, ret[4]);
  1557. for (i = 0; i < NUM_WCD9XXX_REG_RET; i++) {
  1558. if (ret[i])
  1559. return ret[i];
  1560. }
  1561. return 0;
  1562. }
  1563. void wcd9xxx_exit(void)
  1564. {
  1565. wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING);
  1566. i2c_del_driver(&tabla_i2c_driver);
  1567. i2c_del_driver(&wcd9xxx_i2c_driver);
  1568. i2c_del_driver(&wcd9335_i2c_driver);
  1569. i2c_del_driver(&wcd934x_i2c_driver);
  1570. slim_driver_unregister(&wcd_slim_driver);
  1571. }
  1572. MODULE_DESCRIPTION("Codec core driver");
  1573. MODULE_LICENSE("GPL v2");