wcd9xxx-core.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. /* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/slab.h>
  17. #include <linux/ratelimit.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/mfd/wcd9xxx/wcd9xxx-slimslave.h>
  20. #include <linux/mfd/wcd9xxx/core.h>
  21. #include <linux/mfd/wcd9xxx/core-resource.h>
  22. #include <linux/mfd/wcd9xxx/pdata.h>
  23. #include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
  24. #include <linux/delay.h>
  25. #include <linux/gpio.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/i2c.h>
  29. #include <sound/soc.h>
  30. #define WCD9XXX_REGISTER_START_OFFSET 0x800
  31. #define WCD9XXX_SLIM_RW_MAX_TRIES 3
  32. #define SLIMBUS_PRESENT_TIMEOUT 100
  33. #define MAX_WCD9XXX_DEVICE 4
  34. #define CODEC_DT_MAX_PROP_SIZE 40
  35. #define WCD9XXX_I2C_GSBI_SLAVE_ID "3-000d"
  36. #define WCD9XXX_I2C_TOP_SLAVE_ADDR 0x0d
  37. #define WCD9XXX_ANALOG_I2C_SLAVE_ADDR 0x77
  38. #define WCD9XXX_DIGITAL1_I2C_SLAVE_ADDR 0x66
  39. #define WCD9XXX_DIGITAL2_I2C_SLAVE_ADDR 0x55
  40. #define WCD9XXX_I2C_TOP_LEVEL 0
  41. #define WCD9XXX_I2C_ANALOG 1
  42. #define WCD9XXX_I2C_DIGITAL_1 2
  43. #define WCD9XXX_I2C_DIGITAL_2 3
  44. #define ONDEMAND_REGULATOR true
  45. #define STATIC_REGULATOR (!ONDEMAND_REGULATOR)
  46. /* Number of return values needs to be checked for each
  47. * registration of Slimbus of I2C bus for each codec
  48. */
  49. #define NUM_WCD9XXX_REG_RET 8
  50. struct wcd9xxx_i2c {
  51. struct i2c_client *client;
  52. struct i2c_msg xfer_msg[2];
  53. struct mutex xfer_lock;
  54. int mod_id;
  55. };
  56. static int wcd9xxx_dt_parse_vreg_info(struct device *dev,
  57. struct wcd9xxx_regulator *vreg,
  58. const char *vreg_name, bool ondemand);
  59. static int wcd9xxx_dt_parse_micbias_info(struct device *dev,
  60. struct wcd9xxx_micbias_setting *micbias);
  61. static struct wcd9xxx_pdata *wcd9xxx_populate_dt_pdata(struct device *dev);
  62. struct wcd9xxx_i2c wcd9xxx_modules[MAX_WCD9XXX_DEVICE];
  63. static int wcd9xxx_read(struct wcd9xxx *wcd9xxx, unsigned short reg,
  64. int bytes, void *dest, bool interface_reg)
  65. {
  66. int i, ret;
  67. if (bytes <= 0) {
  68. dev_err(wcd9xxx->dev, "Invalid byte read length %d\n", bytes);
  69. return -EINVAL;
  70. }
  71. ret = wcd9xxx->read_dev(wcd9xxx, reg, bytes, dest, interface_reg);
  72. if (ret < 0) {
  73. dev_err(wcd9xxx->dev, "Codec read failed\n");
  74. return ret;
  75. } else {
  76. for (i = 0; i < bytes; i++)
  77. dev_dbg(wcd9xxx->dev, "Read 0x%02x from 0x%x\n",
  78. ((u8 *)dest)[i], reg + i);
  79. }
  80. return 0;
  81. }
  82. static int __wcd9xxx_reg_read(
  83. struct wcd9xxx *wcd9xxx,
  84. unsigned short reg)
  85. {
  86. u8 val;
  87. int ret;
  88. mutex_lock(&wcd9xxx->io_lock);
  89. ret = wcd9xxx_read(wcd9xxx, reg, 1, &val, false);
  90. mutex_unlock(&wcd9xxx->io_lock);
  91. if (ret < 0)
  92. return ret;
  93. else
  94. return val;
  95. }
  96. int wcd9xxx_reg_read(
  97. struct wcd9xxx_core_resource *core_res,
  98. unsigned short reg)
  99. {
  100. struct wcd9xxx *wcd9xxx = (struct wcd9xxx *) core_res->parent;
  101. return __wcd9xxx_reg_read(wcd9xxx, reg);
  102. }
  103. EXPORT_SYMBOL(wcd9xxx_reg_read);
  104. static int wcd9xxx_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
  105. int bytes, void *src, bool interface_reg)
  106. {
  107. int i;
  108. if (bytes <= 0) {
  109. pr_err("%s: Error, invalid write length\n", __func__);
  110. return -EINVAL;
  111. }
  112. for (i = 0; i < bytes; i++)
  113. dev_dbg(wcd9xxx->dev, "Write %02x to 0x%x\n", ((u8 *)src)[i],
  114. reg + i);
  115. return wcd9xxx->write_dev(wcd9xxx, reg, bytes, src, interface_reg);
  116. }
  117. static int __wcd9xxx_reg_write(
  118. struct wcd9xxx *wcd9xxx,
  119. unsigned short reg, u8 val)
  120. {
  121. int ret;
  122. mutex_lock(&wcd9xxx->io_lock);
  123. ret = wcd9xxx_write(wcd9xxx, reg, 1, &val, false);
  124. mutex_unlock(&wcd9xxx->io_lock);
  125. return ret;
  126. }
  127. int wcd9xxx_reg_write(
  128. struct wcd9xxx_core_resource *core_res,
  129. unsigned short reg, u8 val)
  130. {
  131. struct wcd9xxx *wcd9xxx = (struct wcd9xxx *) core_res->parent;
  132. return __wcd9xxx_reg_write(wcd9xxx, reg, val);
  133. }
  134. EXPORT_SYMBOL(wcd9xxx_reg_write);
  135. static u8 wcd9xxx_pgd_la;
  136. static u8 wcd9xxx_inf_la;
  137. int wcd9xxx_interface_reg_read(struct wcd9xxx *wcd9xxx, unsigned short reg)
  138. {
  139. u8 val;
  140. int ret;
  141. mutex_lock(&wcd9xxx->io_lock);
  142. ret = wcd9xxx_read(wcd9xxx, reg, 1, &val, true);
  143. mutex_unlock(&wcd9xxx->io_lock);
  144. if (ret < 0)
  145. return ret;
  146. else
  147. return val;
  148. }
  149. EXPORT_SYMBOL(wcd9xxx_interface_reg_read);
  150. int wcd9xxx_interface_reg_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
  151. u8 val)
  152. {
  153. int ret;
  154. mutex_lock(&wcd9xxx->io_lock);
  155. ret = wcd9xxx_write(wcd9xxx, reg, 1, &val, true);
  156. mutex_unlock(&wcd9xxx->io_lock);
  157. return ret;
  158. }
  159. EXPORT_SYMBOL(wcd9xxx_interface_reg_write);
  160. static int __wcd9xxx_bulk_read(
  161. struct wcd9xxx *wcd9xxx,
  162. unsigned short reg,
  163. int count, u8 *buf)
  164. {
  165. int ret;
  166. mutex_lock(&wcd9xxx->io_lock);
  167. ret = wcd9xxx_read(wcd9xxx, reg, count, buf, false);
  168. mutex_unlock(&wcd9xxx->io_lock);
  169. return ret;
  170. }
  171. int wcd9xxx_bulk_read(
  172. struct wcd9xxx_core_resource *core_res,
  173. unsigned short reg,
  174. int count, u8 *buf)
  175. {
  176. struct wcd9xxx *wcd9xxx =
  177. (struct wcd9xxx *) core_res->parent;
  178. return __wcd9xxx_bulk_read(wcd9xxx, reg, count, buf);
  179. }
  180. EXPORT_SYMBOL(wcd9xxx_bulk_read);
  181. static int __wcd9xxx_bulk_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
  182. int count, u8 *buf)
  183. {
  184. int ret;
  185. mutex_lock(&wcd9xxx->io_lock);
  186. ret = wcd9xxx_write(wcd9xxx, reg, count, buf, false);
  187. mutex_unlock(&wcd9xxx->io_lock);
  188. return ret;
  189. }
  190. int wcd9xxx_bulk_write(
  191. struct wcd9xxx_core_resource *core_res,
  192. unsigned short reg, int count, u8 *buf)
  193. {
  194. struct wcd9xxx *wcd9xxx =
  195. (struct wcd9xxx *) core_res->parent;
  196. return __wcd9xxx_bulk_write(wcd9xxx, reg, count, buf);
  197. }
  198. EXPORT_SYMBOL(wcd9xxx_bulk_write);
  199. static int wcd9xxx_slim_read_device(struct wcd9xxx *wcd9xxx, unsigned short reg,
  200. int bytes, void *dest, bool interface)
  201. {
  202. int ret;
  203. struct slim_ele_access msg;
  204. int slim_read_tries = WCD9XXX_SLIM_RW_MAX_TRIES;
  205. msg.start_offset = WCD9XXX_REGISTER_START_OFFSET + reg;
  206. msg.num_bytes = bytes;
  207. msg.comp = NULL;
  208. while (1) {
  209. mutex_lock(&wcd9xxx->xfer_lock);
  210. ret = slim_request_val_element(interface ?
  211. wcd9xxx->slim_slave : wcd9xxx->slim,
  212. &msg, dest, bytes);
  213. mutex_unlock(&wcd9xxx->xfer_lock);
  214. if (likely(ret == 0) || (--slim_read_tries == 0))
  215. break;
  216. usleep_range(5000, 5000);
  217. }
  218. if (ret)
  219. pr_err("%s: Error, Codec read failed (%d)\n", __func__, ret);
  220. return ret;
  221. }
  222. /* Interface specifies whether the write is to the interface or general
  223. * registers.
  224. */
  225. static int wcd9xxx_slim_write_device(struct wcd9xxx *wcd9xxx,
  226. unsigned short reg, int bytes, void *src, bool interface)
  227. {
  228. int ret;
  229. struct slim_ele_access msg;
  230. int slim_write_tries = WCD9XXX_SLIM_RW_MAX_TRIES;
  231. msg.start_offset = WCD9XXX_REGISTER_START_OFFSET + reg;
  232. msg.num_bytes = bytes;
  233. msg.comp = NULL;
  234. while (1) {
  235. mutex_lock(&wcd9xxx->xfer_lock);
  236. ret = slim_change_val_element(interface ?
  237. wcd9xxx->slim_slave : wcd9xxx->slim,
  238. &msg, src, bytes);
  239. mutex_unlock(&wcd9xxx->xfer_lock);
  240. if (likely(ret == 0) || (--slim_write_tries == 0))
  241. break;
  242. usleep_range(5000, 5000);
  243. }
  244. if (ret)
  245. pr_err("%s: Error, Codec write failed (%d)\n", __func__, ret);
  246. return ret;
  247. }
  248. static struct mfd_cell tabla1x_devs[] = {
  249. {
  250. .name = "tabla1x_codec",
  251. },
  252. };
  253. static struct mfd_cell tabla_devs[] = {
  254. {
  255. .name = "tabla_codec",
  256. },
  257. };
  258. static struct mfd_cell sitar_devs[] = {
  259. {
  260. .name = "sitar_codec",
  261. },
  262. };
  263. static struct mfd_cell taiko_devs[] = {
  264. {
  265. .name = "taiko_codec",
  266. },
  267. };
  268. static struct mfd_cell tapan_devs[] = {
  269. {
  270. .name = "tapan_codec",
  271. },
  272. };
  273. static const struct wcd9xxx_codec_type wcd9xxx_codecs[] = {
  274. {
  275. TABLA_MAJOR, cpu_to_le16(0x1), tabla1x_devs,
  276. ARRAY_SIZE(tabla1x_devs), TABLA_NUM_IRQS, -1,
  277. WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x03,
  278. },
  279. {
  280. TABLA_MAJOR, cpu_to_le16(0x2), tabla_devs,
  281. ARRAY_SIZE(tabla_devs), TABLA_NUM_IRQS, -1,
  282. WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x03
  283. },
  284. {
  285. /* Siter version 1 has same major chip id with Tabla */
  286. TABLA_MAJOR, cpu_to_le16(0x0), sitar_devs,
  287. ARRAY_SIZE(sitar_devs), SITAR_NUM_IRQS, -1,
  288. WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x01
  289. },
  290. {
  291. SITAR_MAJOR, cpu_to_le16(0x1), sitar_devs,
  292. ARRAY_SIZE(sitar_devs), SITAR_NUM_IRQS, -1,
  293. WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x01
  294. },
  295. {
  296. SITAR_MAJOR, cpu_to_le16(0x2), sitar_devs,
  297. ARRAY_SIZE(sitar_devs), SITAR_NUM_IRQS, -1,
  298. WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TABLA, 0x01
  299. },
  300. {
  301. TAIKO_MAJOR, cpu_to_le16(0x0), taiko_devs,
  302. ARRAY_SIZE(taiko_devs), TAIKO_NUM_IRQS, 1,
  303. WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TAIKO, 0x01
  304. },
  305. {
  306. TAIKO_MAJOR, cpu_to_le16(0x1), taiko_devs,
  307. ARRAY_SIZE(taiko_devs), TAIKO_NUM_IRQS, 2,
  308. WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TAIKO, 0x01
  309. },
  310. {
  311. TAPAN_MAJOR, cpu_to_le16(0x0), tapan_devs,
  312. ARRAY_SIZE(tapan_devs), TAPAN_NUM_IRQS, -1,
  313. WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TAIKO, 0x03
  314. },
  315. {
  316. TAPAN_MAJOR, cpu_to_le16(0x1), tapan_devs,
  317. ARRAY_SIZE(tapan_devs), TAPAN_NUM_IRQS, -1,
  318. WCD9XXX_SLIM_SLAVE_ADDR_TYPE_TAIKO, 0x03
  319. },
  320. };
  321. static void wcd9xxx_bring_up(struct wcd9xxx *wcd9xxx)
  322. {
  323. __wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x4);
  324. __wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_CDC_CTL, 0);
  325. usleep_range(5000, 5000);
  326. __wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_CDC_CTL, 3);
  327. __wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 3);
  328. }
  329. static void wcd9xxx_bring_down(struct wcd9xxx *wcd9xxx)
  330. {
  331. __wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x7);
  332. __wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x6);
  333. __wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0xe);
  334. __wcd9xxx_reg_write(wcd9xxx, WCD9XXX_A_LEAKAGE_CTL, 0x8);
  335. }
  336. static int wcd9xxx_reset(struct wcd9xxx *wcd9xxx)
  337. {
  338. int ret;
  339. if (wcd9xxx->reset_gpio && wcd9xxx->slim_device_bootup) {
  340. ret = gpio_request(wcd9xxx->reset_gpio, "CDC_RESET");
  341. if (ret) {
  342. pr_err("%s: Failed to request gpio %d\n", __func__,
  343. wcd9xxx->reset_gpio);
  344. wcd9xxx->reset_gpio = 0;
  345. return ret;
  346. }
  347. }
  348. if (wcd9xxx->reset_gpio) {
  349. gpio_direction_output(wcd9xxx->reset_gpio, 0);
  350. msleep(20);
  351. gpio_direction_output(wcd9xxx->reset_gpio, 1);
  352. msleep(20);
  353. }
  354. return 0;
  355. }
  356. static void wcd9xxx_free_reset(struct wcd9xxx *wcd9xxx)
  357. {
  358. if (wcd9xxx->reset_gpio) {
  359. gpio_free(wcd9xxx->reset_gpio);
  360. wcd9xxx->reset_gpio = 0;
  361. }
  362. }
  363. static const struct wcd9xxx_codec_type
  364. *wcd9xxx_check_codec_type(struct wcd9xxx *wcd9xxx, u8 *version)
  365. {
  366. int i, rc;
  367. const struct wcd9xxx_codec_type *c, *d = NULL;
  368. rc = __wcd9xxx_bulk_read(wcd9xxx, WCD9XXX_A_CHIP_ID_BYTE_0,
  369. sizeof(wcd9xxx->id_minor),
  370. (u8 *)&wcd9xxx->id_minor);
  371. if (rc < 0)
  372. goto exit;
  373. rc = __wcd9xxx_bulk_read(wcd9xxx, WCD9XXX_A_CHIP_ID_BYTE_2,
  374. sizeof(wcd9xxx->id_major),
  375. (u8 *)&wcd9xxx->id_major);
  376. if (rc < 0)
  377. goto exit;
  378. dev_dbg(wcd9xxx->dev, "%s: wcd9xxx chip id major 0x%x, minor 0x%x\n",
  379. __func__, wcd9xxx->id_major, wcd9xxx->id_minor);
  380. for (i = 0, c = &wcd9xxx_codecs[0]; i < ARRAY_SIZE(wcd9xxx_codecs);
  381. i++, c++) {
  382. if (c->id_major == wcd9xxx->id_major) {
  383. if (c->id_minor == wcd9xxx->id_minor) {
  384. d = c;
  385. dev_dbg(wcd9xxx->dev,
  386. "%s: exact match %s\n", __func__,
  387. d->dev->name);
  388. break;
  389. } else if (!d) {
  390. d = c;
  391. } else {
  392. if ((d->id_minor < c->id_minor) ||
  393. (d->id_minor == c->id_minor &&
  394. d->version < c->version))
  395. d = c;
  396. }
  397. dev_dbg(wcd9xxx->dev,
  398. "%s: best match %s, major 0x%x, minor 0x%x\n",
  399. __func__, d->dev->name, d->id_major,
  400. d->id_minor);
  401. }
  402. }
  403. if (!d) {
  404. dev_warn(wcd9xxx->dev,
  405. "%s: driver for id major 0x%x, minor 0x%x not found\n",
  406. __func__, wcd9xxx->id_major, wcd9xxx->id_minor);
  407. } else {
  408. if (d->version > -1) {
  409. *version = d->version;
  410. } else {
  411. rc = __wcd9xxx_reg_read(wcd9xxx,
  412. WCD9XXX_A_CHIP_VERSION);
  413. if (rc < 0) {
  414. d = NULL;
  415. goto exit;
  416. }
  417. *version = (u8)rc & 0x1F;
  418. }
  419. dev_info(wcd9xxx->dev,
  420. "%s: detected %s, major 0x%x, minor 0x%x, ver 0x%x\n",
  421. __func__, d->dev->name, d->id_major, d->id_minor,
  422. *version);
  423. }
  424. exit:
  425. return d;
  426. }
  427. static int wcd9xxx_num_irq_regs(const struct wcd9xxx *wcd9xxx)
  428. {
  429. return (wcd9xxx->codec_type->num_irqs / 8) +
  430. ((wcd9xxx->codec_type->num_irqs % 8) ? 1 : 0);
  431. }
  432. /*
  433. * Interrupt table for v1 corresponds to newer version
  434. * codecs (wcd9304 and wcd9310)
  435. */
  436. static const struct intr_data intr_tbl_v1[] = {
  437. {WCD9XXX_IRQ_SLIMBUS, false},
  438. {WCD9XXX_IRQ_MBHC_INSERTION, true},
  439. {WCD9XXX_IRQ_MBHC_POTENTIAL, true},
  440. {WCD9XXX_IRQ_MBHC_RELEASE, true},
  441. {WCD9XXX_IRQ_MBHC_PRESS, true},
  442. {WCD9XXX_IRQ_MBHC_SHORT_TERM, true},
  443. {WCD9XXX_IRQ_MBHC_REMOVAL, true},
  444. {WCD9XXX_IRQ_BG_PRECHARGE, false},
  445. {WCD9XXX_IRQ_PA1_STARTUP, false},
  446. {WCD9XXX_IRQ_PA2_STARTUP, false},
  447. {WCD9XXX_IRQ_PA3_STARTUP, false},
  448. {WCD9XXX_IRQ_PA4_STARTUP, false},
  449. {WCD9XXX_IRQ_PA5_STARTUP, false},
  450. {WCD9XXX_IRQ_MICBIAS1_PRECHARGE, false},
  451. {WCD9XXX_IRQ_MICBIAS2_PRECHARGE, false},
  452. {WCD9XXX_IRQ_MICBIAS3_PRECHARGE, false},
  453. {WCD9XXX_IRQ_HPH_PA_OCPL_FAULT, false},
  454. {WCD9XXX_IRQ_HPH_PA_OCPR_FAULT, false},
  455. {WCD9XXX_IRQ_EAR_PA_OCPL_FAULT, false},
  456. {WCD9XXX_IRQ_HPH_L_PA_STARTUP, false},
  457. {WCD9XXX_IRQ_HPH_R_PA_STARTUP, false},
  458. {WCD9320_IRQ_EAR_PA_STARTUP, false},
  459. {WCD9XXX_IRQ_RESERVED_0, false},
  460. {WCD9XXX_IRQ_RESERVED_1, false},
  461. };
  462. /*
  463. * Interrupt table for v2 corresponds to newer version
  464. * codecs (wcd9320 and wcd9306)
  465. */
  466. static const struct intr_data intr_tbl_v2[] = {
  467. {WCD9XXX_IRQ_SLIMBUS, false},
  468. {WCD9XXX_IRQ_MBHC_INSERTION, true},
  469. {WCD9XXX_IRQ_MBHC_POTENTIAL, true},
  470. {WCD9XXX_IRQ_MBHC_RELEASE, true},
  471. {WCD9XXX_IRQ_MBHC_PRESS, true},
  472. {WCD9XXX_IRQ_MBHC_SHORT_TERM, true},
  473. {WCD9XXX_IRQ_MBHC_REMOVAL, true},
  474. {WCD9320_IRQ_MBHC_JACK_SWITCH, true},
  475. {WCD9306_IRQ_MBHC_JACK_SWITCH, true},
  476. {WCD9XXX_IRQ_BG_PRECHARGE, false},
  477. {WCD9XXX_IRQ_PA1_STARTUP, false},
  478. {WCD9XXX_IRQ_PA2_STARTUP, false},
  479. {WCD9XXX_IRQ_PA3_STARTUP, false},
  480. {WCD9XXX_IRQ_PA4_STARTUP, false},
  481. {WCD9306_IRQ_HPH_PA_OCPR_FAULT, false},
  482. {WCD9XXX_IRQ_PA5_STARTUP, false},
  483. {WCD9XXX_IRQ_MICBIAS1_PRECHARGE, false},
  484. {WCD9306_IRQ_HPH_PA_OCPL_FAULT, false},
  485. {WCD9XXX_IRQ_MICBIAS2_PRECHARGE, false},
  486. {WCD9XXX_IRQ_MICBIAS3_PRECHARGE, false},
  487. {WCD9XXX_IRQ_HPH_PA_OCPL_FAULT, false},
  488. {WCD9XXX_IRQ_HPH_PA_OCPR_FAULT, false},
  489. {WCD9XXX_IRQ_EAR_PA_OCPL_FAULT, false},
  490. {WCD9XXX_IRQ_HPH_L_PA_STARTUP, false},
  491. {WCD9XXX_IRQ_HPH_R_PA_STARTUP, false},
  492. {WCD9XXX_IRQ_RESERVED_0, false},
  493. {WCD9XXX_IRQ_RESERVED_1, false},
  494. {WCD9XXX_IRQ_MAD_AUDIO, false},
  495. {WCD9XXX_IRQ_MAD_BEACON, false},
  496. {WCD9XXX_IRQ_MAD_ULTRASOUND, false},
  497. {WCD9XXX_IRQ_SPEAKER_CLIPPING, false},
  498. {WCD9XXX_IRQ_VBAT_MONITOR_ATTACK, false},
  499. {WCD9XXX_IRQ_VBAT_MONITOR_RELEASE, false},
  500. {WCD9XXX_IRQ_RESERVED_2, false},
  501. };
  502. static int wcd9xxx_device_init(struct wcd9xxx *wcd9xxx)
  503. {
  504. int ret = 0;
  505. u8 version;
  506. const struct wcd9xxx_codec_type *found;
  507. struct wcd9xxx_core_resource *core_res = &wcd9xxx->core_res;
  508. mutex_init(&wcd9xxx->io_lock);
  509. mutex_init(&wcd9xxx->xfer_lock);
  510. dev_set_drvdata(wcd9xxx->dev, wcd9xxx);
  511. wcd9xxx_bring_up(wcd9xxx);
  512. found = wcd9xxx_check_codec_type(wcd9xxx, &version);
  513. if (!found) {
  514. ret = -ENODEV;
  515. goto err;
  516. } else {
  517. wcd9xxx->codec_type = found;
  518. wcd9xxx->version = version;
  519. }
  520. core_res->parent = wcd9xxx;
  521. core_res->dev = wcd9xxx->dev;
  522. if (wcd9xxx->codec_type->id_major == TABLA_MAJOR
  523. || wcd9xxx->codec_type->id_major == SITAR_MAJOR) {
  524. core_res->intr_table = intr_tbl_v1;
  525. core_res->intr_table_size = ARRAY_SIZE(intr_tbl_v1);
  526. } else {
  527. core_res->intr_table = intr_tbl_v2;
  528. core_res->intr_table_size = ARRAY_SIZE(intr_tbl_v2);
  529. }
  530. wcd9xxx_core_res_init(&wcd9xxx->core_res,
  531. wcd9xxx->codec_type->num_irqs,
  532. wcd9xxx_num_irq_regs(wcd9xxx),
  533. wcd9xxx_reg_read, wcd9xxx_reg_write,
  534. wcd9xxx_bulk_read, wcd9xxx_bulk_write);
  535. if (wcd9xxx_core_irq_init(&wcd9xxx->core_res))
  536. goto err;
  537. ret = mfd_add_devices(wcd9xxx->dev, -1, found->dev, found->size,
  538. NULL, 0);
  539. if (ret != 0) {
  540. dev_err(wcd9xxx->dev, "Failed to add children: %d\n", ret);
  541. goto err_irq;
  542. }
  543. ret = device_init_wakeup(wcd9xxx->dev, true);
  544. if (ret) {
  545. dev_err(wcd9xxx->dev, "Device wakeup init failed: %d\n", ret);
  546. goto err_irq;
  547. }
  548. return ret;
  549. err_irq:
  550. wcd9xxx_irq_exit(&wcd9xxx->core_res);
  551. err:
  552. wcd9xxx_bring_down(wcd9xxx);
  553. wcd9xxx_core_res_deinit(&wcd9xxx->core_res);
  554. mutex_destroy(&wcd9xxx->io_lock);
  555. mutex_destroy(&wcd9xxx->xfer_lock);
  556. return ret;
  557. }
  558. static void wcd9xxx_device_exit(struct wcd9xxx *wcd9xxx)
  559. {
  560. device_init_wakeup(wcd9xxx->dev, false);
  561. wcd9xxx_irq_exit(&wcd9xxx->core_res);
  562. wcd9xxx_bring_down(wcd9xxx);
  563. wcd9xxx_free_reset(wcd9xxx);
  564. wcd9xxx_core_res_deinit(&wcd9xxx->core_res);
  565. mutex_destroy(&wcd9xxx->io_lock);
  566. mutex_destroy(&wcd9xxx->xfer_lock);
  567. if (wcd9xxx_get_intf_type() == WCD9XXX_INTERFACE_TYPE_SLIMBUS)
  568. slim_remove_device(wcd9xxx->slim_slave);
  569. kfree(wcd9xxx);
  570. }
  571. #ifdef CONFIG_DEBUG_FS
  572. struct wcd9xxx *debugCodec;
  573. static struct dentry *debugfs_wcd9xxx_dent;
  574. static struct dentry *debugfs_peek;
  575. static struct dentry *debugfs_poke;
  576. static unsigned char read_data;
  577. static int codec_debug_open(struct inode *inode, struct file *file)
  578. {
  579. file->private_data = inode->i_private;
  580. return 0;
  581. }
  582. static int get_parameters(char *buf, long int *param1, int num_of_par)
  583. {
  584. char *token;
  585. int base, cnt;
  586. token = strsep(&buf, " ");
  587. for (cnt = 0; cnt < num_of_par; cnt++) {
  588. if (token != NULL) {
  589. if ((token[1] == 'x') || (token[1] == 'X'))
  590. base = 16;
  591. else
  592. base = 10;
  593. if (strict_strtoul(token, base, &param1[cnt]) != 0)
  594. return -EINVAL;
  595. token = strsep(&buf, " ");
  596. } else
  597. return -EINVAL;
  598. }
  599. return 0;
  600. }
  601. static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
  602. size_t count, loff_t *ppos)
  603. {
  604. char lbuf[8];
  605. snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
  606. return simple_read_from_buffer(ubuf, count, ppos, lbuf,
  607. strnlen(lbuf, 7));
  608. }
  609. static ssize_t codec_debug_write(struct file *filp,
  610. const char __user *ubuf, size_t cnt, loff_t *ppos)
  611. {
  612. char *access_str = filp->private_data;
  613. char lbuf[32];
  614. int rc;
  615. long int param[5];
  616. if (cnt > sizeof(lbuf) - 1)
  617. return -EINVAL;
  618. rc = copy_from_user(lbuf, ubuf, cnt);
  619. if (rc)
  620. return -EFAULT;
  621. lbuf[cnt] = '\0';
  622. if (!strncmp(access_str, "poke", 6)) {
  623. /* write */
  624. rc = get_parameters(lbuf, param, 2);
  625. if ((param[0] <= 0x3FF) && (param[1] <= 0xFF) &&
  626. (rc == 0))
  627. wcd9xxx_interface_reg_write(debugCodec, param[0],
  628. param[1]);
  629. else
  630. rc = -EINVAL;
  631. } else if (!strncmp(access_str, "peek", 6)) {
  632. /* read */
  633. rc = get_parameters(lbuf, param, 1);
  634. if ((param[0] <= 0x3FF) && (rc == 0))
  635. read_data = wcd9xxx_interface_reg_read(debugCodec,
  636. param[0]);
  637. else
  638. rc = -EINVAL;
  639. }
  640. if (rc == 0)
  641. rc = cnt;
  642. else
  643. pr_err("%s: rc = %d\n", __func__, rc);
  644. return rc;
  645. }
  646. static const struct file_operations codec_debug_ops = {
  647. .open = codec_debug_open,
  648. .write = codec_debug_write,
  649. .read = codec_debug_read
  650. };
  651. #endif
  652. static int wcd9xxx_init_supplies(struct wcd9xxx *wcd9xxx,
  653. struct wcd9xxx_pdata *pdata)
  654. {
  655. int ret;
  656. int i;
  657. wcd9xxx->supplies = kzalloc(sizeof(struct regulator_bulk_data) *
  658. ARRAY_SIZE(pdata->regulator),
  659. GFP_KERNEL);
  660. if (!wcd9xxx->supplies) {
  661. ret = -ENOMEM;
  662. goto err;
  663. }
  664. wcd9xxx->num_of_supplies = 0;
  665. if (ARRAY_SIZE(pdata->regulator) > WCD9XXX_MAX_REGULATOR) {
  666. pr_err("%s: Array Size out of bound\n", __func__);
  667. ret = -EINVAL;
  668. goto err;
  669. }
  670. for (i = 0; i < ARRAY_SIZE(pdata->regulator); i++) {
  671. if (pdata->regulator[i].name) {
  672. wcd9xxx->supplies[i].supply = pdata->regulator[i].name;
  673. wcd9xxx->num_of_supplies++;
  674. }
  675. }
  676. ret = regulator_bulk_get(wcd9xxx->dev, wcd9xxx->num_of_supplies,
  677. wcd9xxx->supplies);
  678. if (ret != 0) {
  679. dev_err(wcd9xxx->dev, "Failed to get supplies: err = %d\n",
  680. ret);
  681. goto err_supplies;
  682. }
  683. for (i = 0; i < wcd9xxx->num_of_supplies; i++) {
  684. if (regulator_count_voltages(wcd9xxx->supplies[i].consumer) <=
  685. 0)
  686. continue;
  687. ret = regulator_set_voltage(wcd9xxx->supplies[i].consumer,
  688. pdata->regulator[i].min_uV,
  689. pdata->regulator[i].max_uV);
  690. if (ret) {
  691. pr_err("%s: Setting regulator voltage failed for "
  692. "regulator %s err = %d\n", __func__,
  693. wcd9xxx->supplies[i].supply, ret);
  694. goto err_get;
  695. }
  696. /* Enabling Codec Buck Voltage to avoid voltage swing from 1.8 - 2.1V during sleep */
  697. if(strcmp("cdc-vdd-buck",wcd9xxx->supplies[i].supply) == 0)
  698. {
  699. ret = regulator_enable(wcd9xxx->supplies[i].consumer);
  700. if (ret) {
  701. pr_err("%s: Setting regulator voltage failed for "
  702. "regulator %s err = %d\n", __func__,
  703. wcd9xxx->supplies[i].supply, ret);
  704. goto err_get;
  705. } else {
  706. pr_err("%s: Setting regulator voltage success for "
  707. "regulator %s err = %d\n", __func__,
  708. wcd9xxx->supplies[i].supply, ret);
  709. }
  710. }
  711. ret = regulator_set_optimum_mode(wcd9xxx->supplies[i].consumer,
  712. pdata->regulator[i].optimum_uA);
  713. if (ret < 0) {
  714. pr_err("%s: Setting regulator optimum mode failed for "
  715. "regulator %s err = %d\n", __func__,
  716. wcd9xxx->supplies[i].supply, ret);
  717. goto err_get;
  718. } else {
  719. ret = 0;
  720. }
  721. }
  722. return ret;
  723. err_get:
  724. regulator_bulk_free(wcd9xxx->num_of_supplies, wcd9xxx->supplies);
  725. err_supplies:
  726. kfree(wcd9xxx->supplies);
  727. err:
  728. return ret;
  729. }
  730. static int wcd9xxx_enable_static_supplies(struct wcd9xxx *wcd9xxx,
  731. struct wcd9xxx_pdata *pdata)
  732. {
  733. int i;
  734. int ret = 0;
  735. for (i = 0; i < wcd9xxx->num_of_supplies; i++) {
  736. if (pdata->regulator[i].ondemand)
  737. continue;
  738. ret = regulator_enable(wcd9xxx->supplies[i].consumer);
  739. if (ret) {
  740. pr_err("%s: Failed to enable %s\n", __func__,
  741. wcd9xxx->supplies[i].supply);
  742. break;
  743. } else {
  744. pr_debug("%s: Enabled regulator %s\n", __func__,
  745. wcd9xxx->supplies[i].supply);
  746. }
  747. }
  748. while (ret && --i)
  749. if (!pdata->regulator[i].ondemand)
  750. regulator_disable(wcd9xxx->supplies[i].consumer);
  751. return ret;
  752. }
  753. static void wcd9xxx_disable_supplies(struct wcd9xxx *wcd9xxx,
  754. struct wcd9xxx_pdata *pdata)
  755. {
  756. int i;
  757. regulator_bulk_disable(wcd9xxx->num_of_supplies,
  758. wcd9xxx->supplies);
  759. for (i = 0; i < wcd9xxx->num_of_supplies; i++) {
  760. if (regulator_count_voltages(wcd9xxx->supplies[i].consumer) <=
  761. 0)
  762. continue;
  763. regulator_set_voltage(wcd9xxx->supplies[i].consumer, 0,
  764. pdata->regulator[i].max_uV);
  765. regulator_set_optimum_mode(wcd9xxx->supplies[i].consumer, 0);
  766. }
  767. regulator_bulk_free(wcd9xxx->num_of_supplies, wcd9xxx->supplies);
  768. kfree(wcd9xxx->supplies);
  769. }
  770. struct wcd9xxx_i2c *get_i2c_wcd9xxx_device_info(u16 reg)
  771. {
  772. u16 mask = 0x0f00;
  773. int value = 0;
  774. struct wcd9xxx_i2c *wcd9xxx = NULL;
  775. value = ((reg & mask) >> 8) & 0x000f;
  776. switch (value) {
  777. case 0:
  778. wcd9xxx = &wcd9xxx_modules[0];
  779. break;
  780. case 1:
  781. wcd9xxx = &wcd9xxx_modules[1];
  782. break;
  783. case 2:
  784. wcd9xxx = &wcd9xxx_modules[2];
  785. break;
  786. case 3:
  787. wcd9xxx = &wcd9xxx_modules[3];
  788. break;
  789. default:
  790. break;
  791. }
  792. return wcd9xxx;
  793. }
  794. int wcd9xxx_i2c_write_device(u16 reg, u8 *value,
  795. u32 bytes)
  796. {
  797. struct i2c_msg *msg;
  798. int ret = 0;
  799. u8 reg_addr = 0;
  800. u8 data[bytes + 1];
  801. struct wcd9xxx_i2c *wcd9xxx;
  802. wcd9xxx = get_i2c_wcd9xxx_device_info(reg);
  803. if (wcd9xxx == NULL || wcd9xxx->client == NULL) {
  804. pr_err("failed to get device info\n");
  805. return -ENODEV;
  806. }
  807. reg_addr = (u8)reg;
  808. msg = &wcd9xxx->xfer_msg[0];
  809. msg->addr = wcd9xxx->client->addr;
  810. msg->len = bytes + 1;
  811. msg->flags = 0;
  812. data[0] = reg;
  813. data[1] = *value;
  814. msg->buf = data;
  815. ret = i2c_transfer(wcd9xxx->client->adapter, wcd9xxx->xfer_msg, 1);
  816. /* Try again if the write fails */
  817. if (ret != 1) {
  818. ret = i2c_transfer(wcd9xxx->client->adapter,
  819. wcd9xxx->xfer_msg, 1);
  820. if (ret != 1) {
  821. pr_err("failed to write the device\n");
  822. return ret;
  823. }
  824. }
  825. pr_debug("write sucess register = %x val = %x\n", reg, data[1]);
  826. return 0;
  827. }
  828. int wcd9xxx_i2c_read_device(unsigned short reg,
  829. int bytes, unsigned char *dest)
  830. {
  831. struct i2c_msg *msg;
  832. int ret = 0;
  833. u8 reg_addr = 0;
  834. struct wcd9xxx_i2c *wcd9xxx;
  835. u8 i = 0;
  836. wcd9xxx = get_i2c_wcd9xxx_device_info(reg);
  837. if (wcd9xxx == NULL || wcd9xxx->client == NULL) {
  838. pr_err("failed to get device info\n");
  839. return -ENODEV;
  840. }
  841. for (i = 0; i < bytes; i++) {
  842. reg_addr = (u8)reg++;
  843. msg = &wcd9xxx->xfer_msg[0];
  844. msg->addr = wcd9xxx->client->addr;
  845. msg->len = 1;
  846. msg->flags = 0;
  847. msg->buf = &reg_addr;
  848. msg = &wcd9xxx->xfer_msg[1];
  849. msg->addr = wcd9xxx->client->addr;
  850. msg->len = 1;
  851. msg->flags = I2C_M_RD;
  852. msg->buf = dest++;
  853. ret = i2c_transfer(wcd9xxx->client->adapter,
  854. wcd9xxx->xfer_msg, 2);
  855. /* Try again if read fails first time */
  856. if (ret != 2) {
  857. ret = i2c_transfer(wcd9xxx->client->adapter,
  858. wcd9xxx->xfer_msg, 2);
  859. if (ret != 2) {
  860. pr_err("failed to read wcd9xxx register\n");
  861. return ret;
  862. }
  863. }
  864. }
  865. return 0;
  866. }
  867. int wcd9xxx_i2c_read(struct wcd9xxx *wcd9xxx, unsigned short reg,
  868. int bytes, void *dest, bool interface_reg)
  869. {
  870. return wcd9xxx_i2c_read_device(reg, bytes, dest);
  871. }
  872. int wcd9xxx_i2c_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
  873. int bytes, void *src, bool interface_reg)
  874. {
  875. return wcd9xxx_i2c_write_device(reg, src, bytes);
  876. }
  877. static int wcd9xxx_i2c_get_client_index(struct i2c_client *client,
  878. int *wcd9xx_index)
  879. {
  880. int ret = 0;
  881. switch (client->addr) {
  882. case WCD9XXX_I2C_TOP_SLAVE_ADDR:
  883. *wcd9xx_index = WCD9XXX_I2C_TOP_LEVEL;
  884. break;
  885. case WCD9XXX_ANALOG_I2C_SLAVE_ADDR:
  886. *wcd9xx_index = WCD9XXX_I2C_ANALOG;
  887. break;
  888. case WCD9XXX_DIGITAL1_I2C_SLAVE_ADDR:
  889. *wcd9xx_index = WCD9XXX_I2C_DIGITAL_1;
  890. break;
  891. case WCD9XXX_DIGITAL2_I2C_SLAVE_ADDR:
  892. *wcd9xx_index = WCD9XXX_I2C_DIGITAL_2;
  893. break;
  894. default:
  895. ret = -EINVAL;
  896. break;
  897. }
  898. return ret;
  899. }
  900. static int __devinit wcd9xxx_i2c_probe(struct i2c_client *client,
  901. const struct i2c_device_id *id)
  902. {
  903. struct wcd9xxx *wcd9xxx = NULL;
  904. struct wcd9xxx_pdata *pdata = NULL;
  905. int val = 0;
  906. int ret = 0;
  907. int wcd9xx_index = 0;
  908. struct device *dev;
  909. int intf_type;
  910. intf_type = wcd9xxx_get_intf_type();
  911. pr_debug("%s: interface status %d\n", __func__, intf_type);
  912. if (intf_type == WCD9XXX_INTERFACE_TYPE_SLIMBUS) {
  913. dev_dbg(&client->dev, "%s:Codec is detected in slimbus mode\n",
  914. __func__);
  915. return -ENODEV;
  916. } else if (intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
  917. ret = wcd9xxx_i2c_get_client_index(client, &wcd9xx_index);
  918. if (ret != 0)
  919. dev_err(&client->dev, "%s: I2C set codec I2C\n"
  920. "client failed\n", __func__);
  921. else {
  922. dev_err(&client->dev, "%s:probe for other slaves\n"
  923. "devices of codec I2C slave Addr = %x\n",
  924. __func__, client->addr);
  925. wcd9xxx_modules[wcd9xx_index].client = client;
  926. }
  927. return ret;
  928. } else if (intf_type == WCD9XXX_INTERFACE_TYPE_PROBING) {
  929. dev = &client->dev;
  930. if (client->dev.of_node) {
  931. dev_dbg(&client->dev, "%s:Platform data\n"
  932. "from device tree\n", __func__);
  933. pdata = wcd9xxx_populate_dt_pdata(&client->dev);
  934. client->dev.platform_data = pdata;
  935. } else {
  936. dev_dbg(&client->dev, "%s:Platform data from\n"
  937. "board file\n", __func__);
  938. pdata = client->dev.platform_data;
  939. }
  940. wcd9xxx = kzalloc(sizeof(struct wcd9xxx), GFP_KERNEL);
  941. if (wcd9xxx == NULL) {
  942. pr_err("%s: error, allocation failed\n", __func__);
  943. ret = -ENOMEM;
  944. goto fail;
  945. }
  946. if (!pdata) {
  947. dev_dbg(&client->dev, "no platform data?\n");
  948. ret = -EINVAL;
  949. goto err_codec;
  950. }
  951. if (i2c_check_functionality(client->adapter,
  952. I2C_FUNC_I2C) == 0) {
  953. dev_dbg(&client->dev, "can't talk I2C?\n");
  954. ret = -EIO;
  955. goto err_codec;
  956. }
  957. dev_set_drvdata(&client->dev, wcd9xxx);
  958. wcd9xxx->dev = &client->dev;
  959. wcd9xxx->reset_gpio = pdata->reset_gpio;
  960. wcd9xxx->slim_device_bootup = true;
  961. if (client->dev.of_node)
  962. wcd9xxx->mclk_rate = pdata->mclk_rate;
  963. ret = wcd9xxx_init_supplies(wcd9xxx, pdata);
  964. if (ret) {
  965. pr_err("%s: Fail to enable Codec supplies\n",
  966. __func__);
  967. goto err_codec;
  968. }
  969. ret = wcd9xxx_enable_static_supplies(wcd9xxx, pdata);
  970. if (ret) {
  971. pr_err("%s: Fail to enable Codec pre-reset supplies\n",
  972. __func__);
  973. goto err_codec;
  974. }
  975. usleep_range(5, 5);
  976. ret = wcd9xxx_reset(wcd9xxx);
  977. if (ret) {
  978. pr_err("%s: Resetting Codec failed\n", __func__);
  979. goto err_supplies;
  980. }
  981. ret = wcd9xxx_i2c_get_client_index(client, &wcd9xx_index);
  982. if (ret != 0) {
  983. pr_err("%s:Set codec I2C client failed\n", __func__);
  984. goto err_supplies;
  985. }
  986. wcd9xxx_modules[wcd9xx_index].client = client;
  987. wcd9xxx->read_dev = wcd9xxx_i2c_read;
  988. wcd9xxx->write_dev = wcd9xxx_i2c_write;
  989. if (!wcd9xxx->dev->of_node)
  990. wcd9xxx_initialize_irq(&wcd9xxx->core_res,
  991. pdata->irq, pdata->irq_base);
  992. ret = wcd9xxx_device_init(wcd9xxx);
  993. if (ret) {
  994. pr_err("%s: error, initializing device failed\n",
  995. __func__);
  996. goto err_device_init;
  997. }
  998. ret = wcd9xxx_read(wcd9xxx, WCD9XXX_A_CHIP_STATUS, 1, &val, 0);
  999. if (ret < 0)
  1000. pr_err("%s: failed to read the wcd9xxx status (%d)\n",
  1001. __func__, ret);
  1002. if (val != wcd9xxx->codec_type->i2c_chip_status)
  1003. pr_err("%s: unknown chip status 0x%x\n", __func__, val);
  1004. wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_I2C);
  1005. return ret;
  1006. } else
  1007. pr_err("%s: I2C probe in wrong state\n", __func__);
  1008. err_device_init:
  1009. wcd9xxx_free_reset(wcd9xxx);
  1010. err_supplies:
  1011. wcd9xxx_disable_supplies(wcd9xxx, pdata);
  1012. err_codec:
  1013. kfree(wcd9xxx);
  1014. fail:
  1015. return ret;
  1016. }
  1017. static int __devexit wcd9xxx_i2c_remove(struct i2c_client *client)
  1018. {
  1019. struct wcd9xxx *wcd9xxx;
  1020. struct wcd9xxx_pdata *pdata = client->dev.platform_data;
  1021. pr_debug("exit\n");
  1022. wcd9xxx = dev_get_drvdata(&client->dev);
  1023. wcd9xxx_disable_supplies(wcd9xxx, pdata);
  1024. wcd9xxx_device_exit(wcd9xxx);
  1025. return 0;
  1026. }
  1027. static int wcd9xxx_dt_parse_vreg_info(struct device *dev,
  1028. struct wcd9xxx_regulator *vreg,
  1029. const char *vreg_name,
  1030. bool ondemand)
  1031. {
  1032. int len, ret = 0;
  1033. const __be32 *prop;
  1034. char prop_name[CODEC_DT_MAX_PROP_SIZE];
  1035. struct device_node *regnode = NULL;
  1036. u32 prop_val;
  1037. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "%s-supply",
  1038. vreg_name);
  1039. regnode = of_parse_phandle(dev->of_node, prop_name, 0);
  1040. if (!regnode) {
  1041. dev_err(dev, "Looking up %s property in node %s failed",
  1042. prop_name, dev->of_node->full_name);
  1043. return -ENODEV;
  1044. }
  1045. vreg->name = vreg_name;
  1046. vreg->ondemand = ondemand;
  1047. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE,
  1048. "qcom,%s-voltage", vreg_name);
  1049. prop = of_get_property(dev->of_node, prop_name, &len);
  1050. if (!prop || (len != (2 * sizeof(__be32)))) {
  1051. dev_err(dev, "%s %s property\n",
  1052. prop ? "invalid format" : "no", prop_name);
  1053. return -EINVAL;
  1054. } else {
  1055. vreg->min_uV = be32_to_cpup(&prop[0]);
  1056. vreg->max_uV = be32_to_cpup(&prop[1]);
  1057. }
  1058. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE,
  1059. "qcom,%s-current", vreg_name);
  1060. ret = of_property_read_u32(dev->of_node, prop_name, &prop_val);
  1061. if (ret) {
  1062. dev_err(dev, "Looking up %s property in node %s failed",
  1063. prop_name, dev->of_node->full_name);
  1064. return -EFAULT;
  1065. }
  1066. vreg->optimum_uA = prop_val;
  1067. dev_info(dev, "%s: vol=[%d %d]uV, curr=[%d]uA, ond %d\n", vreg->name,
  1068. vreg->min_uV, vreg->max_uV, vreg->optimum_uA, vreg->ondemand);
  1069. return 0;
  1070. }
  1071. static int wcd9xxx_read_of_property_u32(struct device *dev,
  1072. const char *name, u32 *val)
  1073. {
  1074. int ret = 0;
  1075. ret = of_property_read_u32(dev->of_node, name, val);
  1076. if (ret)
  1077. dev_err(dev, "Looking up %s property in node %s failed",
  1078. name, dev->of_node->full_name);
  1079. return ret;
  1080. }
  1081. static int wcd9xxx_dt_parse_micbias_info(struct device *dev,
  1082. struct wcd9xxx_micbias_setting *micbias)
  1083. {
  1084. u32 prop_val;
  1085. if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias-ldoh-v",
  1086. &prop_val)))
  1087. micbias->ldoh_v = (u8)prop_val;
  1088. wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias-cfilt1-mv",
  1089. &micbias->cfilt1_mv);
  1090. wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias-cfilt2-mv",
  1091. &micbias->cfilt2_mv);
  1092. wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias-cfilt3-mv",
  1093. &micbias->cfilt3_mv);
  1094. /* Read micbias values for codec. Does not matter even if a few
  1095. * micbias values are not defined in the Device Tree. Codec will
  1096. * anyway not use those values
  1097. */
  1098. if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias1-cfilt-sel",
  1099. &prop_val)))
  1100. micbias->bias1_cfilt_sel = (u8)prop_val;
  1101. if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias2-cfilt-sel",
  1102. &prop_val)))
  1103. micbias->bias2_cfilt_sel = (u8)prop_val;
  1104. if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias3-cfilt-sel",
  1105. &prop_val)))
  1106. micbias->bias3_cfilt_sel = (u8)prop_val;
  1107. if (!(wcd9xxx_read_of_property_u32(dev, "qcom,cdc-micbias4-cfilt-sel",
  1108. &prop_val)))
  1109. micbias->bias4_cfilt_sel = (u8)prop_val;
  1110. /* micbias external cap */
  1111. micbias->bias1_cap_mode =
  1112. (of_property_read_bool(dev->of_node, "qcom,cdc-micbias1-ext-cap") ?
  1113. MICBIAS_EXT_BYP_CAP : MICBIAS_NO_EXT_BYP_CAP);
  1114. micbias->bias2_cap_mode =
  1115. (of_property_read_bool(dev->of_node, "qcom,cdc-micbias2-ext-cap") ?
  1116. MICBIAS_EXT_BYP_CAP : MICBIAS_NO_EXT_BYP_CAP);
  1117. micbias->bias3_cap_mode =
  1118. (of_property_read_bool(dev->of_node, "qcom,cdc-micbias3-ext-cap") ?
  1119. MICBIAS_EXT_BYP_CAP : MICBIAS_NO_EXT_BYP_CAP);
  1120. micbias->bias4_cap_mode =
  1121. (of_property_read_bool(dev->of_node, "qcom,cdc-micbias4-ext-cap") ?
  1122. MICBIAS_EXT_BYP_CAP : MICBIAS_NO_EXT_BYP_CAP);
  1123. micbias->bias2_is_headset_only =
  1124. of_property_read_bool(dev->of_node,
  1125. "qcom,cdc-micbias2-headset-only");
  1126. dev_dbg(dev, "ldoh_v %u cfilt1_mv %u cfilt2_mv %u cfilt3_mv %u",
  1127. (u32)micbias->ldoh_v, (u32)micbias->cfilt1_mv,
  1128. (u32)micbias->cfilt2_mv, (u32)micbias->cfilt3_mv);
  1129. dev_dbg(dev, "bias1_cfilt_sel %u bias2_cfilt_sel %u\n",
  1130. (u32)micbias->bias1_cfilt_sel, (u32)micbias->bias2_cfilt_sel);
  1131. dev_dbg(dev, "bias3_cfilt_sel %u bias4_cfilt_sel %u\n",
  1132. (u32)micbias->bias3_cfilt_sel, (u32)micbias->bias4_cfilt_sel);
  1133. dev_dbg(dev, "bias1_ext_cap %d bias2_ext_cap %d\n",
  1134. micbias->bias1_cap_mode, micbias->bias2_cap_mode);
  1135. dev_dbg(dev, "bias3_ext_cap %d bias4_ext_cap %d\n",
  1136. micbias->bias3_cap_mode, micbias->bias4_cap_mode);
  1137. dev_dbg(dev, "bias2_is_headset_only %d\n",
  1138. micbias->bias2_is_headset_only);
  1139. return 0;
  1140. }
  1141. static int wcd9xxx_dt_parse_slim_interface_dev_info(struct device *dev,
  1142. struct slim_device *slim_ifd)
  1143. {
  1144. int ret = 0;
  1145. struct property *prop;
  1146. ret = of_property_read_string(dev->of_node, "qcom,cdc-slim-ifd",
  1147. &slim_ifd->name);
  1148. if (ret) {
  1149. dev_err(dev, "Looking up %s property in node %s failed",
  1150. "qcom,cdc-slim-ifd-dev", dev->of_node->full_name);
  1151. return -ENODEV;
  1152. }
  1153. prop = of_find_property(dev->of_node,
  1154. "qcom,cdc-slim-ifd-elemental-addr", NULL);
  1155. if (!prop) {
  1156. dev_err(dev, "Looking up %s property in node %s failed",
  1157. "qcom,cdc-slim-ifd-elemental-addr",
  1158. dev->of_node->full_name);
  1159. return -ENODEV;
  1160. } else if (prop->length != 6) {
  1161. dev_err(dev, "invalid codec slim ifd addr. addr length = %d\n",
  1162. prop->length);
  1163. return -ENODEV;
  1164. }
  1165. memcpy(slim_ifd->e_addr, prop->value, 6);
  1166. return 0;
  1167. }
  1168. static int wcd9xxx_process_supplies(struct device *dev,
  1169. struct wcd9xxx_pdata *pdata, const char *supply_list,
  1170. int supply_cnt, bool is_ondemand, int index)
  1171. {
  1172. int idx, ret = 0;
  1173. const char *name;
  1174. if (supply_cnt == 0) {
  1175. dev_dbg(dev, "%s: no supplies defined for %s\n", __func__,
  1176. supply_list);
  1177. return 0;
  1178. }
  1179. for (idx = 0; idx < supply_cnt; idx++) {
  1180. ret = of_property_read_string_index(dev->of_node,
  1181. supply_list, idx,
  1182. &name);
  1183. if (ret) {
  1184. dev_err(dev, "%s: of read string %s idx %d error %d\n",
  1185. __func__, supply_list, idx, ret);
  1186. goto err;
  1187. }
  1188. dev_dbg(dev, "%s: Found cdc supply %s as part of %s\n",
  1189. __func__, name, supply_list);
  1190. ret = wcd9xxx_dt_parse_vreg_info(dev,
  1191. &pdata->regulator[index + idx],
  1192. name, is_ondemand);
  1193. if (ret)
  1194. goto err;
  1195. }
  1196. return 0;
  1197. err:
  1198. return ret;
  1199. }
  1200. static struct wcd9xxx_pdata *wcd9xxx_populate_dt_pdata(struct device *dev)
  1201. {
  1202. struct wcd9xxx_pdata *pdata;
  1203. int ret, static_cnt, ond_cnt, cp_supplies_cnt;
  1204. u32 mclk_rate = 0;
  1205. u32 dmic_sample_rate = 0;
  1206. const char *static_prop_name = "qcom,cdc-static-supplies";
  1207. const char *ond_prop_name = "qcom,cdc-on-demand-supplies";
  1208. const char *cp_supplies_name = "qcom,cdc-cp-supplies";
  1209. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1210. if (!pdata) {
  1211. dev_err(dev, "could not allocate memory for platform data\n");
  1212. return NULL;
  1213. }
  1214. static_cnt = of_property_count_strings(dev->of_node, static_prop_name);
  1215. if (IS_ERR_VALUE(static_cnt)) {
  1216. dev_err(dev, "%s: Failed to get static supplies %d\n", __func__,
  1217. static_cnt);
  1218. goto err;
  1219. }
  1220. /* On-demand supply list is an optional property */
  1221. ond_cnt = of_property_count_strings(dev->of_node, ond_prop_name);
  1222. if (IS_ERR_VALUE(ond_cnt))
  1223. ond_cnt = 0;
  1224. /* cp-supplies list is an optional property */
  1225. cp_supplies_cnt = of_property_count_strings(dev->of_node,
  1226. cp_supplies_name);
  1227. if (IS_ERR_VALUE(cp_supplies_cnt))
  1228. cp_supplies_cnt = 0;
  1229. BUG_ON(static_cnt <= 0 || ond_cnt < 0 || cp_supplies_cnt < 0);
  1230. if ((static_cnt + ond_cnt + cp_supplies_cnt)
  1231. > ARRAY_SIZE(pdata->regulator)) {
  1232. dev_err(dev, "%s: Num of supplies %u > max supported %u\n",
  1233. __func__, static_cnt, ARRAY_SIZE(pdata->regulator));
  1234. goto err;
  1235. }
  1236. ret = wcd9xxx_process_supplies(dev, pdata, static_prop_name,
  1237. static_cnt, STATIC_REGULATOR, 0);
  1238. if (ret)
  1239. goto err;
  1240. ret = wcd9xxx_process_supplies(dev, pdata, ond_prop_name,
  1241. ond_cnt, ONDEMAND_REGULATOR, static_cnt);
  1242. if (ret)
  1243. goto err;
  1244. ret = wcd9xxx_process_supplies(dev, pdata, cp_supplies_name,
  1245. cp_supplies_cnt, ONDEMAND_REGULATOR,
  1246. static_cnt + ond_cnt);
  1247. if (ret)
  1248. goto err;
  1249. ret = wcd9xxx_dt_parse_micbias_info(dev, &pdata->micbias);
  1250. if (ret)
  1251. goto err;
  1252. pdata->reset_gpio = of_get_named_gpio(dev->of_node,
  1253. "qcom,cdc-reset-gpio", 0);
  1254. if (pdata->reset_gpio < 0) {
  1255. dev_err(dev, "Looking up %s property in node %s failed %d\n",
  1256. "qcom, cdc-reset-gpio", dev->of_node->full_name,
  1257. pdata->reset_gpio);
  1258. goto err;
  1259. }
  1260. dev_dbg(dev, "%s: reset gpio %d", __func__, pdata->reset_gpio);
  1261. ret = of_property_read_u32(dev->of_node,
  1262. "qcom,cdc-mclk-clk-rate",
  1263. &mclk_rate);
  1264. if (ret) {
  1265. dev_err(dev, "Looking up %s property in\n"
  1266. "node %s failed",
  1267. "qcom,cdc-mclk-clk-rate",
  1268. dev->of_node->full_name);
  1269. devm_kfree(dev, pdata);
  1270. ret = -EINVAL;
  1271. goto err;
  1272. }
  1273. pdata->mclk_rate = mclk_rate;
  1274. ret = of_property_read_u32(dev->of_node,
  1275. "qcom,cdc-dmic-sample-rate",
  1276. &dmic_sample_rate);
  1277. if (ret) {
  1278. dev_err(dev, "Looking up %s property in node %s failed",
  1279. "qcom,cdc-dmic-sample-rate",
  1280. dev->of_node->full_name);
  1281. dmic_sample_rate = WCD9XXX_DMIC_SAMPLE_RATE_UNDEFINED;
  1282. }
  1283. if (pdata->mclk_rate == WCD9XXX_MCLK_CLK_9P6HZ) {
  1284. if ((dmic_sample_rate != WCD9XXX_DMIC_SAMPLE_RATE_2P4MHZ) &&
  1285. (dmic_sample_rate != WCD9XXX_DMIC_SAMPLE_RATE_3P2MHZ) &&
  1286. (dmic_sample_rate != WCD9XXX_DMIC_SAMPLE_RATE_4P8MHZ) &&
  1287. (dmic_sample_rate != WCD9XXX_DMIC_SAMPLE_RATE_UNDEFINED)) {
  1288. dev_err(dev, "Invalid dmic rate %d for mclk %d\n",
  1289. dmic_sample_rate, pdata->mclk_rate);
  1290. ret = -EINVAL;
  1291. goto err;
  1292. }
  1293. } else if (pdata->mclk_rate == WCD9XXX_MCLK_CLK_12P288MHZ) {
  1294. if ((dmic_sample_rate != WCD9XXX_DMIC_SAMPLE_RATE_3P072MHZ) &&
  1295. (dmic_sample_rate != WCD9XXX_DMIC_SAMPLE_RATE_4P096MHZ) &&
  1296. (dmic_sample_rate != WCD9XXX_DMIC_SAMPLE_RATE_6P144MHZ) &&
  1297. (dmic_sample_rate != WCD9XXX_DMIC_SAMPLE_RATE_UNDEFINED)) {
  1298. dev_err(dev, "Invalid dmic rate %d for mclk %d\n",
  1299. dmic_sample_rate, pdata->mclk_rate);
  1300. ret = -EINVAL;
  1301. goto err;
  1302. }
  1303. }
  1304. pdata->dmic_sample_rate = dmic_sample_rate;
  1305. return pdata;
  1306. err:
  1307. devm_kfree(dev, pdata);
  1308. return NULL;
  1309. }
  1310. static int wcd9xxx_slim_get_laddr(struct slim_device *sb,
  1311. const u8 *e_addr, u8 e_len, u8 *laddr)
  1312. {
  1313. int ret;
  1314. const unsigned long timeout = jiffies +
  1315. msecs_to_jiffies(SLIMBUS_PRESENT_TIMEOUT);
  1316. do {
  1317. ret = slim_get_logical_addr(sb, e_addr, e_len, laddr);
  1318. if (!ret)
  1319. break;
  1320. /* Give SLIMBUS time to report present and be ready. */
  1321. usleep_range(1000, 1000);
  1322. pr_debug_ratelimited("%s: retyring get logical addr\n",
  1323. __func__);
  1324. } while time_before(jiffies, timeout);
  1325. return ret;
  1326. }
  1327. static int wcd9xxx_slim_probe(struct slim_device *slim)
  1328. {
  1329. struct wcd9xxx *wcd9xxx;
  1330. struct wcd9xxx_pdata *pdata;
  1331. int ret = 0;
  1332. int intf_type;
  1333. intf_type = wcd9xxx_get_intf_type();
  1334. if (intf_type == WCD9XXX_INTERFACE_TYPE_I2C) {
  1335. dev_dbg(&slim->dev, "%s:Codec is detected in I2C mode\n",
  1336. __func__);
  1337. return -ENODEV;
  1338. }
  1339. if (slim->dev.of_node) {
  1340. dev_info(&slim->dev, "Platform data from device tree\n");
  1341. pdata = wcd9xxx_populate_dt_pdata(&slim->dev);
  1342. ret = wcd9xxx_dt_parse_slim_interface_dev_info(&slim->dev,
  1343. &pdata->slimbus_slave_device);
  1344. if (ret) {
  1345. dev_err(&slim->dev, "Error, parsing slim interface\n");
  1346. devm_kfree(&slim->dev, pdata);
  1347. ret = -EINVAL;
  1348. goto err;
  1349. }
  1350. slim->dev.platform_data = pdata;
  1351. } else {
  1352. dev_info(&slim->dev, "Platform data from board file\n");
  1353. pdata = slim->dev.platform_data;
  1354. }
  1355. if (!pdata) {
  1356. dev_err(&slim->dev, "Error, no platform data\n");
  1357. ret = -EINVAL;
  1358. goto err;
  1359. }
  1360. wcd9xxx = kzalloc(sizeof(struct wcd9xxx), GFP_KERNEL);
  1361. if (wcd9xxx == NULL) {
  1362. pr_err("%s: error, allocation failed\n", __func__);
  1363. ret = -ENOMEM;
  1364. goto err;
  1365. }
  1366. if (!slim->ctrl) {
  1367. pr_err("Error, no SLIMBUS control data\n");
  1368. ret = -EINVAL;
  1369. goto err_codec;
  1370. }
  1371. wcd9xxx->slim = slim;
  1372. slim_set_clientdata(slim, wcd9xxx);
  1373. wcd9xxx->reset_gpio = pdata->reset_gpio;
  1374. wcd9xxx->dev = &slim->dev;
  1375. wcd9xxx->mclk_rate = pdata->mclk_rate;
  1376. wcd9xxx->slim_device_bootup = true;
  1377. ret = wcd9xxx_init_supplies(wcd9xxx, pdata);
  1378. if (ret) {
  1379. pr_err("%s: Fail to init Codec supplies %d\n", __func__, ret);
  1380. goto err_codec;
  1381. }
  1382. ret = wcd9xxx_enable_static_supplies(wcd9xxx, pdata);
  1383. if (ret) {
  1384. pr_err("%s: Fail to enable Codec pre-reset supplies\n",
  1385. __func__);
  1386. goto err_codec;
  1387. }
  1388. usleep_range(5, 5);
  1389. ret = wcd9xxx_reset(wcd9xxx);
  1390. if (ret) {
  1391. pr_err("%s: Resetting Codec failed\n", __func__);
  1392. goto err_supplies;
  1393. }
  1394. ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim, wcd9xxx->slim->e_addr,
  1395. ARRAY_SIZE(wcd9xxx->slim->e_addr),
  1396. &wcd9xxx->slim->laddr);
  1397. if (ret) {
  1398. pr_err("%s: failed to get slimbus %s logical address: %d\n",
  1399. __func__, wcd9xxx->slim->name, ret);
  1400. goto err_reset;
  1401. }
  1402. wcd9xxx->read_dev = wcd9xxx_slim_read_device;
  1403. wcd9xxx->write_dev = wcd9xxx_slim_write_device;
  1404. wcd9xxx_pgd_la = wcd9xxx->slim->laddr;
  1405. wcd9xxx->slim_slave = &pdata->slimbus_slave_device;
  1406. if (!wcd9xxx->dev->of_node)
  1407. wcd9xxx_initialize_irq(&wcd9xxx->core_res,
  1408. pdata->irq, pdata->irq_base);
  1409. ret = slim_add_device(slim->ctrl, wcd9xxx->slim_slave);
  1410. if (ret) {
  1411. pr_err("%s: error, adding SLIMBUS device failed\n", __func__);
  1412. goto err_reset;
  1413. }
  1414. ret = wcd9xxx_slim_get_laddr(wcd9xxx->slim_slave,
  1415. wcd9xxx->slim_slave->e_addr,
  1416. ARRAY_SIZE(wcd9xxx->slim_slave->e_addr),
  1417. &wcd9xxx->slim_slave->laddr);
  1418. if (ret) {
  1419. pr_err("%s: failed to get slimbus %s logical address: %d\n",
  1420. __func__, wcd9xxx->slim->name, ret);
  1421. goto err_slim_add;
  1422. }
  1423. wcd9xxx_inf_la = wcd9xxx->slim_slave->laddr;
  1424. wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_SLIMBUS);
  1425. ret = wcd9xxx_device_init(wcd9xxx);
  1426. if (ret) {
  1427. pr_err("%s: error, initializing device failed\n", __func__);
  1428. goto err_slim_add;
  1429. }
  1430. #ifdef CONFIG_DEBUG_FS
  1431. debugCodec = wcd9xxx;
  1432. debugfs_wcd9xxx_dent = debugfs_create_dir
  1433. ("wcd9310_slimbus_interface_device", 0);
  1434. if (!IS_ERR(debugfs_wcd9xxx_dent)) {
  1435. debugfs_peek = debugfs_create_file("peek",
  1436. S_IFREG | S_IRUSR, debugfs_wcd9xxx_dent,
  1437. (void *) "peek", &codec_debug_ops);
  1438. debugfs_poke = debugfs_create_file("poke",
  1439. S_IFREG | S_IRUSR, debugfs_wcd9xxx_dent,
  1440. (void *) "poke", &codec_debug_ops);
  1441. }
  1442. #endif
  1443. return ret;
  1444. err_slim_add:
  1445. slim_remove_device(wcd9xxx->slim_slave);
  1446. err_reset:
  1447. wcd9xxx_free_reset(wcd9xxx);
  1448. err_supplies:
  1449. wcd9xxx_disable_supplies(wcd9xxx, pdata);
  1450. err_codec:
  1451. kfree(wcd9xxx);
  1452. err:
  1453. return ret;
  1454. }
  1455. static int wcd9xxx_slim_remove(struct slim_device *pdev)
  1456. {
  1457. struct wcd9xxx *wcd9xxx;
  1458. struct wcd9xxx_pdata *pdata = pdev->dev.platform_data;
  1459. #ifdef CONFIG_DEBUG_FS
  1460. debugfs_remove(debugfs_peek);
  1461. debugfs_remove(debugfs_poke);
  1462. debugfs_remove(debugfs_wcd9xxx_dent);
  1463. #endif
  1464. wcd9xxx = slim_get_devicedata(pdev);
  1465. wcd9xxx_deinit_slimslave(wcd9xxx);
  1466. slim_remove_device(wcd9xxx->slim_slave);
  1467. wcd9xxx_disable_supplies(wcd9xxx, pdata);
  1468. wcd9xxx_device_exit(wcd9xxx);
  1469. return 0;
  1470. }
  1471. static int wcd9xxx_device_up(struct wcd9xxx *wcd9xxx)
  1472. {
  1473. int ret = 0;
  1474. struct wcd9xxx_core_resource *wcd9xxx_res = &wcd9xxx->core_res;
  1475. if (wcd9xxx->slim_device_bootup) {
  1476. wcd9xxx->slim_device_bootup = false;
  1477. return 0;
  1478. }
  1479. dev_info(wcd9xxx->dev, "%s: codec bring up\n", __func__);
  1480. wcd9xxx_bring_up(wcd9xxx);
  1481. ret = wcd9xxx_irq_init(wcd9xxx_res);
  1482. if (ret) {
  1483. pr_err("%s: wcd9xx_irq_init failed : %d\n", __func__, ret);
  1484. } else {
  1485. if (wcd9xxx->post_reset)
  1486. ret = wcd9xxx->post_reset(wcd9xxx);
  1487. }
  1488. return ret;
  1489. }
  1490. static int wcd9xxx_slim_device_reset(struct slim_device *sldev)
  1491. {
  1492. int ret;
  1493. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1494. if (!wcd9xxx) {
  1495. pr_err("%s: wcd9xxx is NULL\n", __func__);
  1496. return -EINVAL;
  1497. }
  1498. dev_info(wcd9xxx->dev, "%s: device reset\n", __func__);
  1499. if (wcd9xxx->slim_device_bootup)
  1500. return 0;
  1501. ret = wcd9xxx_reset(wcd9xxx);
  1502. if (ret)
  1503. dev_err(wcd9xxx->dev, "%s: Resetting Codec failed\n", __func__);
  1504. return ret;
  1505. }
  1506. static int wcd9xxx_slim_device_up(struct slim_device *sldev)
  1507. {
  1508. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1509. if (!wcd9xxx) {
  1510. pr_err("%s: wcd9xxx is NULL\n", __func__);
  1511. return -EINVAL;
  1512. }
  1513. dev_info(wcd9xxx->dev, "%s: slim device up\n", __func__);
  1514. return wcd9xxx_device_up(wcd9xxx);
  1515. }
  1516. static int wcd9xxx_slim_device_down(struct slim_device *sldev)
  1517. {
  1518. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1519. if (!wcd9xxx) {
  1520. pr_err("%s: wcd9xxx is NULL\n", __func__);
  1521. return -EINVAL;
  1522. }
  1523. wcd9xxx_irq_exit(&wcd9xxx->core_res);
  1524. if (wcd9xxx->dev_down)
  1525. wcd9xxx->dev_down(wcd9xxx);
  1526. dev_dbg(wcd9xxx->dev, "%s: device down\n", __func__);
  1527. return 0;
  1528. }
  1529. static int wcd9xxx_slim_resume(struct slim_device *sldev)
  1530. {
  1531. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1532. return wcd9xxx_core_res_resume(&wcd9xxx->core_res);
  1533. }
  1534. static int wcd9xxx_i2c_resume(struct i2c_client *i2cdev)
  1535. {
  1536. struct wcd9xxx *wcd9xxx = dev_get_drvdata(&i2cdev->dev);
  1537. if (wcd9xxx)
  1538. return wcd9xxx_core_res_resume(&wcd9xxx->core_res);
  1539. else
  1540. return 0;
  1541. }
  1542. static int wcd9xxx_slim_suspend(struct slim_device *sldev, pm_message_t pmesg)
  1543. {
  1544. struct wcd9xxx *wcd9xxx = slim_get_devicedata(sldev);
  1545. return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg);
  1546. }
  1547. static int wcd9xxx_i2c_suspend(struct i2c_client *i2cdev, pm_message_t pmesg)
  1548. {
  1549. struct wcd9xxx *wcd9xxx = dev_get_drvdata(&i2cdev->dev);
  1550. if (wcd9xxx)
  1551. return wcd9xxx_core_res_suspend(&wcd9xxx->core_res, pmesg);
  1552. else
  1553. return 0;
  1554. }
  1555. static const struct slim_device_id sitar_slimtest_id[] = {
  1556. {"sitar-slim", 0},
  1557. {}
  1558. };
  1559. static struct slim_driver sitar_slim_driver = {
  1560. .driver = {
  1561. .name = "sitar-slim",
  1562. .owner = THIS_MODULE,
  1563. },
  1564. .probe = wcd9xxx_slim_probe,
  1565. .remove = wcd9xxx_slim_remove,
  1566. .id_table = sitar_slimtest_id,
  1567. .resume = wcd9xxx_slim_resume,
  1568. .suspend = wcd9xxx_slim_suspend,
  1569. };
  1570. static const struct slim_device_id sitar1p1_slimtest_id[] = {
  1571. {"sitar1p1-slim", 0},
  1572. {}
  1573. };
  1574. static struct slim_driver sitar1p1_slim_driver = {
  1575. .driver = {
  1576. .name = "sitar1p1-slim",
  1577. .owner = THIS_MODULE,
  1578. },
  1579. .probe = wcd9xxx_slim_probe,
  1580. .remove = wcd9xxx_slim_remove,
  1581. .id_table = sitar1p1_slimtest_id,
  1582. .resume = wcd9xxx_slim_resume,
  1583. .suspend = wcd9xxx_slim_suspend,
  1584. };
  1585. static const struct slim_device_id slimtest_id[] = {
  1586. {"tabla-slim", 0},
  1587. {}
  1588. };
  1589. static struct slim_driver tabla_slim_driver = {
  1590. .driver = {
  1591. .name = "tabla-slim",
  1592. .owner = THIS_MODULE,
  1593. },
  1594. .probe = wcd9xxx_slim_probe,
  1595. .remove = wcd9xxx_slim_remove,
  1596. .id_table = slimtest_id,
  1597. .resume = wcd9xxx_slim_resume,
  1598. .suspend = wcd9xxx_slim_suspend,
  1599. };
  1600. static const struct slim_device_id slimtest2x_id[] = {
  1601. {"tabla2x-slim", 0},
  1602. {}
  1603. };
  1604. static struct slim_driver tabla2x_slim_driver = {
  1605. .driver = {
  1606. .name = "tabla2x-slim",
  1607. .owner = THIS_MODULE,
  1608. },
  1609. .probe = wcd9xxx_slim_probe,
  1610. .remove = wcd9xxx_slim_remove,
  1611. .id_table = slimtest2x_id,
  1612. .resume = wcd9xxx_slim_resume,
  1613. .suspend = wcd9xxx_slim_suspend,
  1614. };
  1615. static const struct slim_device_id taiko_slimtest_id[] = {
  1616. {"taiko-slim-pgd", 0},
  1617. {}
  1618. };
  1619. static struct slim_driver taiko_slim_driver = {
  1620. .driver = {
  1621. .name = "taiko-slim",
  1622. .owner = THIS_MODULE,
  1623. },
  1624. .probe = wcd9xxx_slim_probe,
  1625. .remove = wcd9xxx_slim_remove,
  1626. .id_table = taiko_slimtest_id,
  1627. .resume = wcd9xxx_slim_resume,
  1628. .suspend = wcd9xxx_slim_suspend,
  1629. .device_up = wcd9xxx_slim_device_up,
  1630. .reset_device = wcd9xxx_slim_device_reset,
  1631. .device_down = wcd9xxx_slim_device_down,
  1632. };
  1633. static const struct slim_device_id tapan_slimtest_id[] = {
  1634. {"tapan-slim-pgd", 0},
  1635. {}
  1636. };
  1637. static struct slim_driver tapan_slim_driver = {
  1638. .driver = {
  1639. .name = "tapan-slim",
  1640. .owner = THIS_MODULE,
  1641. },
  1642. .probe = wcd9xxx_slim_probe,
  1643. .remove = wcd9xxx_slim_remove,
  1644. .id_table = tapan_slimtest_id,
  1645. .resume = wcd9xxx_slim_resume,
  1646. .suspend = wcd9xxx_slim_suspend,
  1647. .device_up = wcd9xxx_slim_device_up,
  1648. .reset_device = wcd9xxx_slim_device_reset,
  1649. .device_down = wcd9xxx_slim_device_down,
  1650. };
  1651. static struct i2c_device_id wcd9xxx_id_table[] = {
  1652. {"wcd9xxx-i2c", WCD9XXX_I2C_TOP_LEVEL},
  1653. {"wcd9xxx-i2c", WCD9XXX_I2C_ANALOG},
  1654. {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_1},
  1655. {"wcd9xxx-i2c", WCD9XXX_I2C_DIGITAL_2},
  1656. {}
  1657. };
  1658. static struct i2c_device_id tabla_id_table[] = {
  1659. {"tabla top level", WCD9XXX_I2C_TOP_LEVEL},
  1660. {"tabla analog", WCD9XXX_I2C_ANALOG},
  1661. {"tabla digital1", WCD9XXX_I2C_DIGITAL_1},
  1662. {"tabla digital2", WCD9XXX_I2C_DIGITAL_2},
  1663. {}
  1664. };
  1665. MODULE_DEVICE_TABLE(i2c, tabla_id_table);
  1666. static struct i2c_driver tabla_i2c_driver = {
  1667. .driver = {
  1668. .owner = THIS_MODULE,
  1669. .name = "tabla-i2c-core",
  1670. },
  1671. .id_table = tabla_id_table,
  1672. .probe = wcd9xxx_i2c_probe,
  1673. .remove = __devexit_p(wcd9xxx_i2c_remove),
  1674. .resume = wcd9xxx_i2c_resume,
  1675. .suspend = wcd9xxx_i2c_suspend,
  1676. };
  1677. static struct i2c_driver wcd9xxx_i2c_driver = {
  1678. .driver = {
  1679. .owner = THIS_MODULE,
  1680. .name = "wcd9xxx-i2c-core",
  1681. },
  1682. .id_table = wcd9xxx_id_table,
  1683. .probe = wcd9xxx_i2c_probe,
  1684. .remove = __devexit_p(wcd9xxx_i2c_remove),
  1685. .resume = wcd9xxx_i2c_resume,
  1686. .suspend = wcd9xxx_i2c_suspend,
  1687. };
  1688. static int __init wcd9xxx_init(void)
  1689. {
  1690. int ret[NUM_WCD9XXX_REG_RET];
  1691. int i = 0;
  1692. wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING);
  1693. ret[0] = slim_driver_register(&tabla_slim_driver);
  1694. if (ret[0])
  1695. pr_err("Failed to register tabla SB driver: %d\n", ret[0]);
  1696. ret[1] = slim_driver_register(&tabla2x_slim_driver);
  1697. if (ret[1])
  1698. pr_err("Failed to register tabla2x SB driver: %d\n", ret[1]);
  1699. ret[2] = i2c_add_driver(&tabla_i2c_driver);
  1700. if (ret[2])
  1701. pr_err("failed to add the tabla2x I2C driver: %d\n", ret[2]);
  1702. ret[3] = slim_driver_register(&sitar_slim_driver);
  1703. if (ret[3])
  1704. pr_err("Failed to register sitar SB driver: %d\n", ret[3]);
  1705. ret[4] = slim_driver_register(&sitar1p1_slim_driver);
  1706. if (ret[4])
  1707. pr_err("Failed to register sitar SB driver: %d\n", ret[4]);
  1708. ret[5] = slim_driver_register(&taiko_slim_driver);
  1709. if (ret[5])
  1710. pr_err("Failed to register taiko SB driver: %d\n", ret[5]);
  1711. ret[6] = i2c_add_driver(&wcd9xxx_i2c_driver);
  1712. if (ret[6])
  1713. pr_err("failed to add the wcd9xxx I2C driver: %d\n", ret[6]);
  1714. ret[7] = slim_driver_register(&tapan_slim_driver);
  1715. if (ret[7])
  1716. pr_err("Failed to register tapan SB driver: %d\n", ret[7]);
  1717. for (i = 0; i < NUM_WCD9XXX_REG_RET; i++) {
  1718. if (ret[i])
  1719. return ret[i];
  1720. }
  1721. return 0;
  1722. }
  1723. module_init(wcd9xxx_init);
  1724. static void __exit wcd9xxx_exit(void)
  1725. {
  1726. wcd9xxx_set_intf_type(WCD9XXX_INTERFACE_TYPE_PROBING);
  1727. }
  1728. module_exit(wcd9xxx_exit);
  1729. MODULE_DESCRIPTION("Codec core driver");
  1730. MODULE_VERSION("1.0");
  1731. MODULE_LICENSE("GPL v2");