twl6040.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*
  2. * MFD driver for TWL6040 audio device
  3. *
  4. * Authors: Misael Lopez Cruz <misael.lopez@ti.com>
  5. * Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
  6. * Peter Ujfalusi <peter.ujfalusi@ti.com>
  7. *
  8. * Copyright: (C) 2011 Texas Instruments, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/kernel.h>
  29. #include <linux/err.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/of.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/gpio.h>
  36. #include <linux/delay.h>
  37. #include <linux/i2c.h>
  38. #include <linux/regmap.h>
  39. #include <linux/mfd/core.h>
  40. #include <linux/mfd/twl6040.h>
  41. #include <linux/regulator/consumer.h>
  42. #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
  43. #define TWL6040_NUM_SUPPLIES (2)
  44. static const struct reg_default twl6040_defaults[] = {
  45. { 0x01, 0x4B }, /* REG_ASICID (ro) */
  46. { 0x02, 0x00 }, /* REG_ASICREV (ro) */
  47. { 0x03, 0x00 }, /* REG_INTID */
  48. { 0x04, 0x00 }, /* REG_INTMR */
  49. { 0x05, 0x00 }, /* REG_NCPCTRL */
  50. { 0x06, 0x00 }, /* REG_LDOCTL */
  51. { 0x07, 0x60 }, /* REG_HPPLLCTL */
  52. { 0x08, 0x00 }, /* REG_LPPLLCTL */
  53. { 0x09, 0x4A }, /* REG_LPPLLDIV */
  54. { 0x0A, 0x00 }, /* REG_AMICBCTL */
  55. { 0x0B, 0x00 }, /* REG_DMICBCTL */
  56. { 0x0C, 0x00 }, /* REG_MICLCTL */
  57. { 0x0D, 0x00 }, /* REG_MICRCTL */
  58. { 0x0E, 0x00 }, /* REG_MICGAIN */
  59. { 0x0F, 0x1B }, /* REG_LINEGAIN */
  60. { 0x10, 0x00 }, /* REG_HSLCTL */
  61. { 0x11, 0x00 }, /* REG_HSRCTL */
  62. { 0x12, 0x00 }, /* REG_HSGAIN */
  63. { 0x13, 0x00 }, /* REG_EARCTL */
  64. { 0x14, 0x00 }, /* REG_HFLCTL */
  65. { 0x15, 0x00 }, /* REG_HFLGAIN */
  66. { 0x16, 0x00 }, /* REG_HFRCTL */
  67. { 0x17, 0x00 }, /* REG_HFRGAIN */
  68. { 0x18, 0x00 }, /* REG_VIBCTLL */
  69. { 0x19, 0x00 }, /* REG_VIBDATL */
  70. { 0x1A, 0x00 }, /* REG_VIBCTLR */
  71. { 0x1B, 0x00 }, /* REG_VIBDATR */
  72. { 0x1C, 0x00 }, /* REG_HKCTL1 */
  73. { 0x1D, 0x00 }, /* REG_HKCTL2 */
  74. { 0x1E, 0x00 }, /* REG_GPOCTL */
  75. { 0x1F, 0x00 }, /* REG_ALB */
  76. { 0x20, 0x00 }, /* REG_DLB */
  77. /* 0x28, REG_TRIM1 */
  78. /* 0x29, REG_TRIM2 */
  79. /* 0x2A, REG_TRIM3 */
  80. /* 0x2B, REG_HSOTRIM */
  81. /* 0x2C, REG_HFOTRIM */
  82. { 0x2D, 0x08 }, /* REG_ACCCTL */
  83. { 0x2E, 0x00 }, /* REG_STATUS (ro) */
  84. };
  85. static struct reg_sequence twl6040_patch[] = {
  86. /*
  87. * Select I2C bus access to dual access registers
  88. * Interrupt register is cleared on read
  89. * Select fast mode for i2c (400KHz)
  90. */
  91. { TWL6040_REG_ACCCTL,
  92. TWL6040_I2CSEL | TWL6040_INTCLRMODE | TWL6040_I2CMODE(1) },
  93. };
  94. static bool twl6040_has_vibra(struct device_node *parent)
  95. {
  96. struct device_node *node;
  97. node = of_get_child_by_name(parent, "vibra");
  98. if (node) {
  99. of_node_put(node);
  100. return true;
  101. }
  102. return false;
  103. }
  104. int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
  105. {
  106. int ret;
  107. unsigned int val;
  108. ret = regmap_read(twl6040->regmap, reg, &val);
  109. if (ret < 0)
  110. return ret;
  111. return val;
  112. }
  113. EXPORT_SYMBOL(twl6040_reg_read);
  114. int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
  115. {
  116. int ret;
  117. ret = regmap_write(twl6040->regmap, reg, val);
  118. return ret;
  119. }
  120. EXPORT_SYMBOL(twl6040_reg_write);
  121. int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
  122. {
  123. return regmap_update_bits(twl6040->regmap, reg, mask, mask);
  124. }
  125. EXPORT_SYMBOL(twl6040_set_bits);
  126. int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
  127. {
  128. return regmap_update_bits(twl6040->regmap, reg, mask, 0);
  129. }
  130. EXPORT_SYMBOL(twl6040_clear_bits);
  131. /* twl6040 codec manual power-up sequence */
  132. static int twl6040_power_up_manual(struct twl6040 *twl6040)
  133. {
  134. u8 ldoctl, ncpctl, lppllctl;
  135. int ret;
  136. /* enable high-side LDO, reference system and internal oscillator */
  137. ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
  138. ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
  139. if (ret)
  140. return ret;
  141. usleep_range(10000, 10500);
  142. /* enable negative charge pump */
  143. ncpctl = TWL6040_NCPENA;
  144. ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
  145. if (ret)
  146. goto ncp_err;
  147. usleep_range(1000, 1500);
  148. /* enable low-side LDO */
  149. ldoctl |= TWL6040_LSLDOENA;
  150. ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
  151. if (ret)
  152. goto lsldo_err;
  153. usleep_range(1000, 1500);
  154. /* enable low-power PLL */
  155. lppllctl = TWL6040_LPLLENA;
  156. ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
  157. if (ret)
  158. goto lppll_err;
  159. usleep_range(5000, 5500);
  160. /* disable internal oscillator */
  161. ldoctl &= ~TWL6040_OSCENA;
  162. ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
  163. if (ret)
  164. goto osc_err;
  165. return 0;
  166. osc_err:
  167. lppllctl &= ~TWL6040_LPLLENA;
  168. twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
  169. lppll_err:
  170. ldoctl &= ~TWL6040_LSLDOENA;
  171. twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
  172. lsldo_err:
  173. ncpctl &= ~TWL6040_NCPENA;
  174. twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
  175. ncp_err:
  176. ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
  177. twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
  178. dev_err(twl6040->dev, "manual power-up failed\n");
  179. return ret;
  180. }
  181. /* twl6040 manual power-down sequence */
  182. static void twl6040_power_down_manual(struct twl6040 *twl6040)
  183. {
  184. u8 ncpctl, ldoctl, lppllctl;
  185. ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
  186. ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
  187. lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
  188. /* enable internal oscillator */
  189. ldoctl |= TWL6040_OSCENA;
  190. twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
  191. usleep_range(1000, 1500);
  192. /* disable low-power PLL */
  193. lppllctl &= ~TWL6040_LPLLENA;
  194. twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
  195. /* disable low-side LDO */
  196. ldoctl &= ~TWL6040_LSLDOENA;
  197. twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
  198. /* disable negative charge pump */
  199. ncpctl &= ~TWL6040_NCPENA;
  200. twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
  201. /* disable high-side LDO, reference system and internal oscillator */
  202. ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
  203. twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
  204. }
  205. static irqreturn_t twl6040_readyint_handler(int irq, void *data)
  206. {
  207. struct twl6040 *twl6040 = data;
  208. complete(&twl6040->ready);
  209. return IRQ_HANDLED;
  210. }
  211. static irqreturn_t twl6040_thint_handler(int irq, void *data)
  212. {
  213. struct twl6040 *twl6040 = data;
  214. u8 status;
  215. status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
  216. if (status & TWL6040_TSHUTDET) {
  217. dev_warn(twl6040->dev, "Thermal shutdown, powering-off");
  218. twl6040_power(twl6040, 0);
  219. } else {
  220. dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on");
  221. twl6040_power(twl6040, 1);
  222. }
  223. return IRQ_HANDLED;
  224. }
  225. static int twl6040_power_up_automatic(struct twl6040 *twl6040)
  226. {
  227. int time_left;
  228. gpio_set_value(twl6040->audpwron, 1);
  229. time_left = wait_for_completion_timeout(&twl6040->ready,
  230. msecs_to_jiffies(144));
  231. if (!time_left) {
  232. u8 intid;
  233. dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
  234. intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
  235. if (!(intid & TWL6040_READYINT)) {
  236. dev_err(twl6040->dev, "automatic power-up failed\n");
  237. gpio_set_value(twl6040->audpwron, 0);
  238. return -ETIMEDOUT;
  239. }
  240. }
  241. return 0;
  242. }
  243. int twl6040_power(struct twl6040 *twl6040, int on)
  244. {
  245. int ret = 0;
  246. mutex_lock(&twl6040->mutex);
  247. if (on) {
  248. /* already powered-up */
  249. if (twl6040->power_count++)
  250. goto out;
  251. ret = clk_prepare_enable(twl6040->clk32k);
  252. if (ret) {
  253. twl6040->power_count = 0;
  254. goto out;
  255. }
  256. /* Allow writes to the chip */
  257. regcache_cache_only(twl6040->regmap, false);
  258. if (gpio_is_valid(twl6040->audpwron)) {
  259. /* use automatic power-up sequence */
  260. ret = twl6040_power_up_automatic(twl6040);
  261. if (ret) {
  262. clk_disable_unprepare(twl6040->clk32k);
  263. twl6040->power_count = 0;
  264. goto out;
  265. }
  266. } else {
  267. /* use manual power-up sequence */
  268. ret = twl6040_power_up_manual(twl6040);
  269. if (ret) {
  270. clk_disable_unprepare(twl6040->clk32k);
  271. twl6040->power_count = 0;
  272. goto out;
  273. }
  274. }
  275. /* Sync with the HW */
  276. regcache_sync(twl6040->regmap);
  277. /* Default PLL configuration after power up */
  278. twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
  279. twl6040->sysclk_rate = 19200000;
  280. } else {
  281. /* already powered-down */
  282. if (!twl6040->power_count) {
  283. dev_err(twl6040->dev,
  284. "device is already powered-off\n");
  285. ret = -EPERM;
  286. goto out;
  287. }
  288. if (--twl6040->power_count)
  289. goto out;
  290. if (gpio_is_valid(twl6040->audpwron)) {
  291. /* use AUDPWRON line */
  292. gpio_set_value(twl6040->audpwron, 0);
  293. /* power-down sequence latency */
  294. usleep_range(500, 700);
  295. } else {
  296. /* use manual power-down sequence */
  297. twl6040_power_down_manual(twl6040);
  298. }
  299. /* Set regmap to cache only and mark it as dirty */
  300. regcache_cache_only(twl6040->regmap, true);
  301. regcache_mark_dirty(twl6040->regmap);
  302. twl6040->sysclk_rate = 0;
  303. if (twl6040->pll == TWL6040_SYSCLK_SEL_HPPLL) {
  304. clk_disable_unprepare(twl6040->mclk);
  305. twl6040->mclk_rate = 0;
  306. }
  307. clk_disable_unprepare(twl6040->clk32k);
  308. }
  309. out:
  310. mutex_unlock(&twl6040->mutex);
  311. return ret;
  312. }
  313. EXPORT_SYMBOL(twl6040_power);
  314. int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
  315. unsigned int freq_in, unsigned int freq_out)
  316. {
  317. u8 hppllctl, lppllctl;
  318. int ret = 0;
  319. mutex_lock(&twl6040->mutex);
  320. hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
  321. lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
  322. /* Force full reconfiguration when switching between PLL */
  323. if (pll_id != twl6040->pll) {
  324. twl6040->sysclk_rate = 0;
  325. twl6040->mclk_rate = 0;
  326. }
  327. switch (pll_id) {
  328. case TWL6040_SYSCLK_SEL_LPPLL:
  329. /* low-power PLL divider */
  330. /* Change the sysclk configuration only if it has been canged */
  331. if (twl6040->sysclk_rate != freq_out) {
  332. switch (freq_out) {
  333. case 17640000:
  334. lppllctl |= TWL6040_LPLLFIN;
  335. break;
  336. case 19200000:
  337. lppllctl &= ~TWL6040_LPLLFIN;
  338. break;
  339. default:
  340. dev_err(twl6040->dev,
  341. "freq_out %d not supported\n",
  342. freq_out);
  343. ret = -EINVAL;
  344. goto pll_out;
  345. }
  346. twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
  347. lppllctl);
  348. }
  349. /* The PLL in use has not been change, we can exit */
  350. if (twl6040->pll == pll_id)
  351. break;
  352. switch (freq_in) {
  353. case 32768:
  354. lppllctl |= TWL6040_LPLLENA;
  355. twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
  356. lppllctl);
  357. mdelay(5);
  358. lppllctl &= ~TWL6040_HPLLSEL;
  359. twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
  360. lppllctl);
  361. hppllctl &= ~TWL6040_HPLLENA;
  362. twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
  363. hppllctl);
  364. break;
  365. default:
  366. dev_err(twl6040->dev,
  367. "freq_in %d not supported\n", freq_in);
  368. ret = -EINVAL;
  369. goto pll_out;
  370. }
  371. clk_disable_unprepare(twl6040->mclk);
  372. break;
  373. case TWL6040_SYSCLK_SEL_HPPLL:
  374. /* high-performance PLL can provide only 19.2 MHz */
  375. if (freq_out != 19200000) {
  376. dev_err(twl6040->dev,
  377. "freq_out %d not supported\n", freq_out);
  378. ret = -EINVAL;
  379. goto pll_out;
  380. }
  381. if (twl6040->mclk_rate != freq_in) {
  382. hppllctl &= ~TWL6040_MCLK_MSK;
  383. switch (freq_in) {
  384. case 12000000:
  385. /* PLL enabled, active mode */
  386. hppllctl |= TWL6040_MCLK_12000KHZ |
  387. TWL6040_HPLLENA;
  388. break;
  389. case 19200000:
  390. /* PLL enabled, bypass mode */
  391. hppllctl |= TWL6040_MCLK_19200KHZ |
  392. TWL6040_HPLLBP | TWL6040_HPLLENA;
  393. break;
  394. case 26000000:
  395. /* PLL enabled, active mode */
  396. hppllctl |= TWL6040_MCLK_26000KHZ |
  397. TWL6040_HPLLENA;
  398. break;
  399. case 38400000:
  400. /* PLL enabled, bypass mode */
  401. hppllctl |= TWL6040_MCLK_38400KHZ |
  402. TWL6040_HPLLBP | TWL6040_HPLLENA;
  403. break;
  404. default:
  405. dev_err(twl6040->dev,
  406. "freq_in %d not supported\n", freq_in);
  407. ret = -EINVAL;
  408. goto pll_out;
  409. }
  410. /* When switching to HPPLL, enable the mclk first */
  411. if (pll_id != twl6040->pll)
  412. clk_prepare_enable(twl6040->mclk);
  413. /*
  414. * enable clock slicer to ensure input waveform is
  415. * square
  416. */
  417. hppllctl |= TWL6040_HPLLSQRENA;
  418. twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
  419. hppllctl);
  420. usleep_range(500, 700);
  421. lppllctl |= TWL6040_HPLLSEL;
  422. twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
  423. lppllctl);
  424. lppllctl &= ~TWL6040_LPLLENA;
  425. twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
  426. lppllctl);
  427. twl6040->mclk_rate = freq_in;
  428. }
  429. break;
  430. default:
  431. dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
  432. ret = -EINVAL;
  433. goto pll_out;
  434. }
  435. twl6040->sysclk_rate = freq_out;
  436. twl6040->pll = pll_id;
  437. pll_out:
  438. mutex_unlock(&twl6040->mutex);
  439. return ret;
  440. }
  441. EXPORT_SYMBOL(twl6040_set_pll);
  442. int twl6040_get_pll(struct twl6040 *twl6040)
  443. {
  444. if (twl6040->power_count)
  445. return twl6040->pll;
  446. else
  447. return -ENODEV;
  448. }
  449. EXPORT_SYMBOL(twl6040_get_pll);
  450. unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
  451. {
  452. return twl6040->sysclk_rate;
  453. }
  454. EXPORT_SYMBOL(twl6040_get_sysclk);
  455. /* Get the combined status of the vibra control register */
  456. int twl6040_get_vibralr_status(struct twl6040 *twl6040)
  457. {
  458. unsigned int reg;
  459. int ret;
  460. u8 status;
  461. ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLL, &reg);
  462. if (ret != 0)
  463. return ret;
  464. status = reg;
  465. ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLR, &reg);
  466. if (ret != 0)
  467. return ret;
  468. status |= reg;
  469. status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
  470. return status;
  471. }
  472. EXPORT_SYMBOL(twl6040_get_vibralr_status);
  473. static struct resource twl6040_vibra_rsrc[] = {
  474. {
  475. .flags = IORESOURCE_IRQ,
  476. },
  477. };
  478. static struct resource twl6040_codec_rsrc[] = {
  479. {
  480. .flags = IORESOURCE_IRQ,
  481. },
  482. };
  483. static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
  484. {
  485. /* Register 0 is not readable */
  486. if (!reg)
  487. return false;
  488. return true;
  489. }
  490. static bool twl6040_volatile_reg(struct device *dev, unsigned int reg)
  491. {
  492. switch (reg) {
  493. case TWL6040_REG_ASICID:
  494. case TWL6040_REG_ASICREV:
  495. case TWL6040_REG_INTID:
  496. case TWL6040_REG_LPPLLCTL:
  497. case TWL6040_REG_HPPLLCTL:
  498. case TWL6040_REG_STATUS:
  499. return true;
  500. default:
  501. return false;
  502. }
  503. }
  504. static bool twl6040_writeable_reg(struct device *dev, unsigned int reg)
  505. {
  506. switch (reg) {
  507. case TWL6040_REG_ASICID:
  508. case TWL6040_REG_ASICREV:
  509. case TWL6040_REG_STATUS:
  510. return false;
  511. default:
  512. return true;
  513. }
  514. }
  515. static const struct regmap_config twl6040_regmap_config = {
  516. .reg_bits = 8,
  517. .val_bits = 8,
  518. .reg_defaults = twl6040_defaults,
  519. .num_reg_defaults = ARRAY_SIZE(twl6040_defaults),
  520. .max_register = TWL6040_REG_STATUS, /* 0x2e */
  521. .readable_reg = twl6040_readable_reg,
  522. .volatile_reg = twl6040_volatile_reg,
  523. .writeable_reg = twl6040_writeable_reg,
  524. .cache_type = REGCACHE_RBTREE,
  525. .use_single_rw = true,
  526. };
  527. static const struct regmap_irq twl6040_irqs[] = {
  528. { .reg_offset = 0, .mask = TWL6040_THINT, },
  529. { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
  530. { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
  531. { .reg_offset = 0, .mask = TWL6040_HFINT, },
  532. { .reg_offset = 0, .mask = TWL6040_VIBINT, },
  533. { .reg_offset = 0, .mask = TWL6040_READYINT, },
  534. };
  535. static struct regmap_irq_chip twl6040_irq_chip = {
  536. .name = "twl6040",
  537. .irqs = twl6040_irqs,
  538. .num_irqs = ARRAY_SIZE(twl6040_irqs),
  539. .num_regs = 1,
  540. .status_base = TWL6040_REG_INTID,
  541. .mask_base = TWL6040_REG_INTMR,
  542. };
  543. static int twl6040_probe(struct i2c_client *client,
  544. const struct i2c_device_id *id)
  545. {
  546. struct device_node *node = client->dev.of_node;
  547. struct twl6040 *twl6040;
  548. struct mfd_cell *cell = NULL;
  549. int irq, ret, children = 0;
  550. if (!node) {
  551. dev_err(&client->dev, "of node is missing\n");
  552. return -EINVAL;
  553. }
  554. /* In order to operate correctly we need valid interrupt config */
  555. if (!client->irq) {
  556. dev_err(&client->dev, "Invalid IRQ configuration\n");
  557. return -EINVAL;
  558. }
  559. twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
  560. GFP_KERNEL);
  561. if (!twl6040)
  562. return -ENOMEM;
  563. twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
  564. if (IS_ERR(twl6040->regmap))
  565. return PTR_ERR(twl6040->regmap);
  566. i2c_set_clientdata(client, twl6040);
  567. twl6040->clk32k = devm_clk_get(&client->dev, "clk32k");
  568. if (IS_ERR(twl6040->clk32k)) {
  569. if (PTR_ERR(twl6040->clk32k) == -EPROBE_DEFER)
  570. return -EPROBE_DEFER;
  571. dev_dbg(&client->dev, "clk32k is not handled\n");
  572. twl6040->clk32k = NULL;
  573. }
  574. twl6040->mclk = devm_clk_get(&client->dev, "mclk");
  575. if (IS_ERR(twl6040->mclk)) {
  576. if (PTR_ERR(twl6040->mclk) == -EPROBE_DEFER)
  577. return -EPROBE_DEFER;
  578. dev_dbg(&client->dev, "mclk is not handled\n");
  579. twl6040->mclk = NULL;
  580. }
  581. twl6040->supplies[0].supply = "vio";
  582. twl6040->supplies[1].supply = "v2v1";
  583. ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
  584. twl6040->supplies);
  585. if (ret != 0) {
  586. dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
  587. return ret;
  588. }
  589. ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
  590. if (ret != 0) {
  591. dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
  592. return ret;
  593. }
  594. twl6040->dev = &client->dev;
  595. twl6040->irq = client->irq;
  596. mutex_init(&twl6040->mutex);
  597. init_completion(&twl6040->ready);
  598. regmap_register_patch(twl6040->regmap, twl6040_patch,
  599. ARRAY_SIZE(twl6040_patch));
  600. twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
  601. if (twl6040->rev < 0) {
  602. dev_err(&client->dev, "Failed to read revision register: %d\n",
  603. twl6040->rev);
  604. ret = twl6040->rev;
  605. goto gpio_err;
  606. }
  607. /* ERRATA: Automatic power-up is not possible in ES1.0 */
  608. if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
  609. twl6040->audpwron = of_get_named_gpio(node,
  610. "ti,audpwron-gpio", 0);
  611. else
  612. twl6040->audpwron = -EINVAL;
  613. if (gpio_is_valid(twl6040->audpwron)) {
  614. ret = devm_gpio_request_one(&client->dev, twl6040->audpwron,
  615. GPIOF_OUT_INIT_LOW, "audpwron");
  616. if (ret)
  617. goto gpio_err;
  618. /* Clear any pending interrupt */
  619. twl6040_reg_read(twl6040, TWL6040_REG_INTID);
  620. }
  621. ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT,
  622. 0, &twl6040_irq_chip, &twl6040->irq_data);
  623. if (ret < 0)
  624. goto gpio_err;
  625. twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
  626. TWL6040_IRQ_READY);
  627. twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
  628. TWL6040_IRQ_TH);
  629. ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
  630. twl6040_readyint_handler, IRQF_ONESHOT,
  631. "twl6040_irq_ready", twl6040);
  632. if (ret) {
  633. dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
  634. goto readyirq_err;
  635. }
  636. ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
  637. twl6040_thint_handler, IRQF_ONESHOT,
  638. "twl6040_irq_th", twl6040);
  639. if (ret) {
  640. dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
  641. goto readyirq_err;
  642. }
  643. /*
  644. * The main functionality of twl6040 to provide audio on OMAP4+ systems.
  645. * We can add the ASoC codec child whenever this driver has been loaded.
  646. */
  647. irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
  648. cell = &twl6040->cells[children];
  649. cell->name = "twl6040-codec";
  650. twl6040_codec_rsrc[0].start = irq;
  651. twl6040_codec_rsrc[0].end = irq;
  652. cell->resources = twl6040_codec_rsrc;
  653. cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
  654. children++;
  655. /* Vibra input driver support */
  656. if (twl6040_has_vibra(node)) {
  657. irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
  658. cell = &twl6040->cells[children];
  659. cell->name = "twl6040-vibra";
  660. twl6040_vibra_rsrc[0].start = irq;
  661. twl6040_vibra_rsrc[0].end = irq;
  662. cell->resources = twl6040_vibra_rsrc;
  663. cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
  664. children++;
  665. }
  666. /* GPO support */
  667. cell = &twl6040->cells[children];
  668. cell->name = "twl6040-gpo";
  669. children++;
  670. /* PDM clock support */
  671. cell = &twl6040->cells[children];
  672. cell->name = "twl6040-pdmclk";
  673. children++;
  674. /* The chip is powered down so mark regmap to cache only and dirty */
  675. regcache_cache_only(twl6040->regmap, true);
  676. regcache_mark_dirty(twl6040->regmap);
  677. ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
  678. NULL, 0, NULL);
  679. if (ret)
  680. goto readyirq_err;
  681. return 0;
  682. readyirq_err:
  683. regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
  684. gpio_err:
  685. regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
  686. return ret;
  687. }
  688. static int twl6040_remove(struct i2c_client *client)
  689. {
  690. struct twl6040 *twl6040 = i2c_get_clientdata(client);
  691. if (twl6040->power_count)
  692. twl6040_power(twl6040, 0);
  693. regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
  694. mfd_remove_devices(&client->dev);
  695. regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
  696. return 0;
  697. }
  698. static const struct i2c_device_id twl6040_i2c_id[] = {
  699. { "twl6040", 0, },
  700. { "twl6041", 0, },
  701. { },
  702. };
  703. MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
  704. static struct i2c_driver twl6040_driver = {
  705. .driver = {
  706. .name = "twl6040",
  707. },
  708. .probe = twl6040_probe,
  709. .remove = twl6040_remove,
  710. .id_table = twl6040_i2c_id,
  711. };
  712. module_i2c_driver(twl6040_driver);
  713. MODULE_DESCRIPTION("TWL6040 MFD");
  714. MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
  715. MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
  716. MODULE_LICENSE("GPL");