twl-core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
  4. * and audio CODEC devices
  5. *
  6. * Copyright (C) 2005-2006 Texas Instruments, Inc.
  7. *
  8. * Modifications to defer interrupt handling to a kernel thread:
  9. * Copyright (C) 2006 MontaVista Software, Inc.
  10. *
  11. * Based on tlv320aic23.c:
  12. * Copyright (c) by Kai Svahn <[email protected]>
  13. *
  14. * Code cleanup and modifications to IRQ handler.
  15. * by syed khasim <[email protected]>
  16. */
  17. #include <linux/init.h>
  18. #include <linux/mutex.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/clk.h>
  22. #include <linux/err.h>
  23. #include <linux/device.h>
  24. #include <linux/of.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/irq.h>
  28. #include <linux/irqdomain.h>
  29. #include <linux/regulator/machine.h>
  30. #include <linux/i2c.h>
  31. #include <linux/mfd/twl.h>
  32. /* Register descriptions for audio */
  33. #include <linux/mfd/twl4030-audio.h>
  34. #include "twl-core.h"
  35. /*
  36. * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
  37. * Management and System Companion Device" chips originally designed for
  38. * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
  39. * often at around 3 Mbit/sec, including for interrupt handling.
  40. *
  41. * This driver core provides genirq support for the interrupts emitted,
  42. * by the various modules, and exports register access primitives.
  43. *
  44. * FIXME this driver currently requires use of the first interrupt line
  45. * (and associated registers).
  46. */
  47. #define DRIVER_NAME "twl"
  48. /* Triton Core internal information (BEGIN) */
  49. /* Base Address defns for twl4030_map[] */
  50. /* subchip/slave 0 - USB ID */
  51. #define TWL4030_BASEADD_USB 0x0000
  52. /* subchip/slave 1 - AUD ID */
  53. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  54. #define TWL4030_BASEADD_GPIO 0x0098
  55. #define TWL4030_BASEADD_INTBR 0x0085
  56. #define TWL4030_BASEADD_PIH 0x0080
  57. #define TWL4030_BASEADD_TEST 0x004C
  58. /* subchip/slave 2 - AUX ID */
  59. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  60. #define TWL4030_BASEADD_LED 0x00EE
  61. #define TWL4030_BASEADD_MADC 0x0000
  62. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  63. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  64. #define TWL4030_BASEADD_PWM 0x00F8
  65. #define TWL4030_BASEADD_KEYPAD 0x00D2
  66. #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
  67. #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
  68. one */
  69. /* subchip/slave 3 - POWER ID */
  70. #define TWL4030_BASEADD_BACKUP 0x0014
  71. #define TWL4030_BASEADD_INT 0x002E
  72. #define TWL4030_BASEADD_PM_MASTER 0x0036
  73. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  74. #define TWL4030_DCDC_GLOBAL_CFG 0x06
  75. #define SMARTREFLEX_ENABLE BIT(3)
  76. #define TWL4030_BASEADD_RTC 0x001C
  77. #define TWL4030_BASEADD_SECURED_REG 0x0000
  78. /* Triton Core internal information (END) */
  79. /* subchip/slave 0 0x48 - POWER */
  80. #define TWL6030_BASEADD_RTC 0x0000
  81. #define TWL6030_BASEADD_SECURED_REG 0x0017
  82. #define TWL6030_BASEADD_PM_MASTER 0x001F
  83. #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
  84. #define TWL6030_BASEADD_PM_MISC 0x00E2
  85. #define TWL6030_BASEADD_PM_PUPD 0x00F0
  86. /* subchip/slave 1 0x49 - FEATURE */
  87. #define TWL6030_BASEADD_USB 0x0000
  88. #define TWL6030_BASEADD_GPADC_CTRL 0x002E
  89. #define TWL6030_BASEADD_AUX 0x0090
  90. #define TWL6030_BASEADD_PWM 0x00BA
  91. #define TWL6030_BASEADD_GASGAUGE 0x00C0
  92. #define TWL6030_BASEADD_PIH 0x00D0
  93. #define TWL6030_BASEADD_CHARGER 0x00E0
  94. #define TWL6032_BASEADD_CHARGER 0x00DA
  95. #define TWL6030_BASEADD_LED 0x00F4
  96. /* subchip/slave 2 0x4A - DFT */
  97. #define TWL6030_BASEADD_DIEID 0x00C0
  98. /* subchip/slave 3 0x4B - AUDIO */
  99. #define TWL6030_BASEADD_AUDIO 0x0000
  100. #define TWL6030_BASEADD_RSV 0x0000
  101. #define TWL6030_BASEADD_ZERO 0x0000
  102. /* Few power values */
  103. #define R_CFG_BOOT 0x05
  104. /* some fields in R_CFG_BOOT */
  105. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  106. #define HFCLK_FREQ_26_MHZ (2 << 0)
  107. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  108. #define HIGH_PERF_SQ (1 << 3)
  109. #define CK32K_LOWPWR_EN (1 << 7)
  110. /*----------------------------------------------------------------------*/
  111. /* Structure for each TWL4030/TWL6030 Slave */
  112. struct twl_client {
  113. struct i2c_client *client;
  114. struct regmap *regmap;
  115. };
  116. /* mapping the module id to slave id and base address */
  117. struct twl_mapping {
  118. unsigned char sid; /* Slave ID */
  119. unsigned char base; /* base address */
  120. };
  121. struct twl_private {
  122. bool ready; /* The core driver is ready to be used */
  123. u32 twl_idcode; /* TWL IDCODE Register value */
  124. unsigned int twl_id;
  125. struct twl_mapping *twl_map;
  126. struct twl_client *twl_modules;
  127. };
  128. static struct twl_private *twl_priv;
  129. static struct twl_mapping twl4030_map[] = {
  130. /*
  131. * NOTE: don't change this table without updating the
  132. * <linux/mfd/twl.h> defines for TWL4030_MODULE_*
  133. * so they continue to match the order in this table.
  134. */
  135. /* Common IPs */
  136. { 0, TWL4030_BASEADD_USB },
  137. { 1, TWL4030_BASEADD_PIH },
  138. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  139. { 3, TWL4030_BASEADD_PM_MASTER },
  140. { 3, TWL4030_BASEADD_PM_RECEIVER },
  141. { 3, TWL4030_BASEADD_RTC },
  142. { 2, TWL4030_BASEADD_PWM },
  143. { 2, TWL4030_BASEADD_LED },
  144. { 3, TWL4030_BASEADD_SECURED_REG },
  145. /* TWL4030 specific IPs */
  146. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  147. { 1, TWL4030_BASEADD_GPIO },
  148. { 1, TWL4030_BASEADD_INTBR },
  149. { 1, TWL4030_BASEADD_TEST },
  150. { 2, TWL4030_BASEADD_KEYPAD },
  151. { 2, TWL4030_BASEADD_MADC },
  152. { 2, TWL4030_BASEADD_INTERRUPTS },
  153. { 2, TWL4030_BASEADD_PRECHARGE },
  154. { 3, TWL4030_BASEADD_BACKUP },
  155. { 3, TWL4030_BASEADD_INT },
  156. { 2, TWL5031_BASEADD_ACCESSORY },
  157. { 2, TWL5031_BASEADD_INTERRUPTS },
  158. };
  159. static const struct reg_default twl4030_49_defaults[] = {
  160. /* Audio Registers */
  161. { 0x01, 0x00}, /* CODEC_MODE */
  162. { 0x02, 0x00}, /* OPTION */
  163. /* 0x03 Unused */
  164. { 0x04, 0x00}, /* MICBIAS_CTL */
  165. { 0x05, 0x00}, /* ANAMICL */
  166. { 0x06, 0x00}, /* ANAMICR */
  167. { 0x07, 0x00}, /* AVADC_CTL */
  168. { 0x08, 0x00}, /* ADCMICSEL */
  169. { 0x09, 0x00}, /* DIGMIXING */
  170. { 0x0a, 0x0f}, /* ATXL1PGA */
  171. { 0x0b, 0x0f}, /* ATXR1PGA */
  172. { 0x0c, 0x0f}, /* AVTXL2PGA */
  173. { 0x0d, 0x0f}, /* AVTXR2PGA */
  174. { 0x0e, 0x00}, /* AUDIO_IF */
  175. { 0x0f, 0x00}, /* VOICE_IF */
  176. { 0x10, 0x3f}, /* ARXR1PGA */
  177. { 0x11, 0x3f}, /* ARXL1PGA */
  178. { 0x12, 0x3f}, /* ARXR2PGA */
  179. { 0x13, 0x3f}, /* ARXL2PGA */
  180. { 0x14, 0x25}, /* VRXPGA */
  181. { 0x15, 0x00}, /* VSTPGA */
  182. { 0x16, 0x00}, /* VRX2ARXPGA */
  183. { 0x17, 0x00}, /* AVDAC_CTL */
  184. { 0x18, 0x00}, /* ARX2VTXPGA */
  185. { 0x19, 0x32}, /* ARXL1_APGA_CTL*/
  186. { 0x1a, 0x32}, /* ARXR1_APGA_CTL*/
  187. { 0x1b, 0x32}, /* ARXL2_APGA_CTL*/
  188. { 0x1c, 0x32}, /* ARXR2_APGA_CTL*/
  189. { 0x1d, 0x00}, /* ATX2ARXPGA */
  190. { 0x1e, 0x00}, /* BT_IF */
  191. { 0x1f, 0x55}, /* BTPGA */
  192. { 0x20, 0x00}, /* BTSTPGA */
  193. { 0x21, 0x00}, /* EAR_CTL */
  194. { 0x22, 0x00}, /* HS_SEL */
  195. { 0x23, 0x00}, /* HS_GAIN_SET */
  196. { 0x24, 0x00}, /* HS_POPN_SET */
  197. { 0x25, 0x00}, /* PREDL_CTL */
  198. { 0x26, 0x00}, /* PREDR_CTL */
  199. { 0x27, 0x00}, /* PRECKL_CTL */
  200. { 0x28, 0x00}, /* PRECKR_CTL */
  201. { 0x29, 0x00}, /* HFL_CTL */
  202. { 0x2a, 0x00}, /* HFR_CTL */
  203. { 0x2b, 0x05}, /* ALC_CTL */
  204. { 0x2c, 0x00}, /* ALC_SET1 */
  205. { 0x2d, 0x00}, /* ALC_SET2 */
  206. { 0x2e, 0x00}, /* BOOST_CTL */
  207. { 0x2f, 0x00}, /* SOFTVOL_CTL */
  208. { 0x30, 0x13}, /* DTMF_FREQSEL */
  209. { 0x31, 0x00}, /* DTMF_TONEXT1H */
  210. { 0x32, 0x00}, /* DTMF_TONEXT1L */
  211. { 0x33, 0x00}, /* DTMF_TONEXT2H */
  212. { 0x34, 0x00}, /* DTMF_TONEXT2L */
  213. { 0x35, 0x79}, /* DTMF_TONOFF */
  214. { 0x36, 0x11}, /* DTMF_WANONOFF */
  215. { 0x37, 0x00}, /* I2S_RX_SCRAMBLE_H */
  216. { 0x38, 0x00}, /* I2S_RX_SCRAMBLE_M */
  217. { 0x39, 0x00}, /* I2S_RX_SCRAMBLE_L */
  218. { 0x3a, 0x06}, /* APLL_CTL */
  219. { 0x3b, 0x00}, /* DTMF_CTL */
  220. { 0x3c, 0x44}, /* DTMF_PGA_CTL2 (0x3C) */
  221. { 0x3d, 0x69}, /* DTMF_PGA_CTL1 (0x3D) */
  222. { 0x3e, 0x00}, /* MISC_SET_1 */
  223. { 0x3f, 0x00}, /* PCMBTMUX */
  224. /* 0x40 - 0x42 Unused */
  225. { 0x43, 0x00}, /* RX_PATH_SEL */
  226. { 0x44, 0x32}, /* VDL_APGA_CTL */
  227. { 0x45, 0x00}, /* VIBRA_CTL */
  228. { 0x46, 0x00}, /* VIBRA_SET */
  229. { 0x47, 0x00}, /* VIBRA_PWM_SET */
  230. { 0x48, 0x00}, /* ANAMIC_GAIN */
  231. { 0x49, 0x00}, /* MISC_SET_2 */
  232. /* End of Audio Registers */
  233. };
  234. static bool twl4030_49_nop_reg(struct device *dev, unsigned int reg)
  235. {
  236. switch (reg) {
  237. case 0x00:
  238. case 0x03:
  239. case 0x40:
  240. case 0x41:
  241. case 0x42:
  242. return false;
  243. default:
  244. return true;
  245. }
  246. }
  247. static const struct regmap_range twl4030_49_volatile_ranges[] = {
  248. regmap_reg_range(TWL4030_BASEADD_TEST, 0xff),
  249. };
  250. static const struct regmap_access_table twl4030_49_volatile_table = {
  251. .yes_ranges = twl4030_49_volatile_ranges,
  252. .n_yes_ranges = ARRAY_SIZE(twl4030_49_volatile_ranges),
  253. };
  254. static const struct regmap_config twl4030_regmap_config[4] = {
  255. {
  256. /* Address 0x48 */
  257. .reg_bits = 8,
  258. .val_bits = 8,
  259. .max_register = 0xff,
  260. },
  261. {
  262. /* Address 0x49 */
  263. .reg_bits = 8,
  264. .val_bits = 8,
  265. .max_register = 0xff,
  266. .readable_reg = twl4030_49_nop_reg,
  267. .writeable_reg = twl4030_49_nop_reg,
  268. .volatile_table = &twl4030_49_volatile_table,
  269. .reg_defaults = twl4030_49_defaults,
  270. .num_reg_defaults = ARRAY_SIZE(twl4030_49_defaults),
  271. .cache_type = REGCACHE_RBTREE,
  272. },
  273. {
  274. /* Address 0x4a */
  275. .reg_bits = 8,
  276. .val_bits = 8,
  277. .max_register = 0xff,
  278. },
  279. {
  280. /* Address 0x4b */
  281. .reg_bits = 8,
  282. .val_bits = 8,
  283. .max_register = 0xff,
  284. },
  285. };
  286. static struct twl_mapping twl6030_map[] = {
  287. /*
  288. * NOTE: don't change this table without updating the
  289. * <linux/mfd/twl.h> defines for TWL4030_MODULE_*
  290. * so they continue to match the order in this table.
  291. */
  292. /* Common IPs */
  293. { 1, TWL6030_BASEADD_USB },
  294. { 1, TWL6030_BASEADD_PIH },
  295. { 1, TWL6030_BASEADD_CHARGER },
  296. { 0, TWL6030_BASEADD_PM_MASTER },
  297. { 0, TWL6030_BASEADD_PM_SLAVE_MISC },
  298. { 0, TWL6030_BASEADD_RTC },
  299. { 1, TWL6030_BASEADD_PWM },
  300. { 1, TWL6030_BASEADD_LED },
  301. { 0, TWL6030_BASEADD_SECURED_REG },
  302. /* TWL6030 specific IPs */
  303. { 0, TWL6030_BASEADD_ZERO },
  304. { 1, TWL6030_BASEADD_ZERO },
  305. { 2, TWL6030_BASEADD_ZERO },
  306. { 1, TWL6030_BASEADD_GPADC_CTRL },
  307. { 1, TWL6030_BASEADD_GASGAUGE },
  308. };
  309. static const struct regmap_config twl6030_regmap_config[3] = {
  310. {
  311. /* Address 0x48 */
  312. .reg_bits = 8,
  313. .val_bits = 8,
  314. .max_register = 0xff,
  315. },
  316. {
  317. /* Address 0x49 */
  318. .reg_bits = 8,
  319. .val_bits = 8,
  320. .max_register = 0xff,
  321. },
  322. {
  323. /* Address 0x4a */
  324. .reg_bits = 8,
  325. .val_bits = 8,
  326. .max_register = 0xff,
  327. },
  328. };
  329. /*----------------------------------------------------------------------*/
  330. static inline int twl_get_num_slaves(void)
  331. {
  332. if (twl_class_is_4030())
  333. return 4; /* TWL4030 class have four slave address */
  334. else
  335. return 3; /* TWL6030 class have three slave address */
  336. }
  337. static inline int twl_get_last_module(void)
  338. {
  339. if (twl_class_is_4030())
  340. return TWL4030_MODULE_LAST;
  341. else
  342. return TWL6030_MODULE_LAST;
  343. }
  344. /* Exported Functions */
  345. unsigned int twl_rev(void)
  346. {
  347. return twl_priv ? twl_priv->twl_id : 0;
  348. }
  349. EXPORT_SYMBOL(twl_rev);
  350. /**
  351. * twl_get_regmap - Get the regmap associated with the given module
  352. * @mod_no: module number
  353. *
  354. * Returns the regmap pointer or NULL in case of failure.
  355. */
  356. static struct regmap *twl_get_regmap(u8 mod_no)
  357. {
  358. int sid;
  359. struct twl_client *twl;
  360. if (unlikely(!twl_priv || !twl_priv->ready)) {
  361. pr_err("%s: not initialized\n", DRIVER_NAME);
  362. return NULL;
  363. }
  364. if (unlikely(mod_no >= twl_get_last_module())) {
  365. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  366. return NULL;
  367. }
  368. sid = twl_priv->twl_map[mod_no].sid;
  369. twl = &twl_priv->twl_modules[sid];
  370. return twl->regmap;
  371. }
  372. /**
  373. * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
  374. * @mod_no: module number
  375. * @value: an array of num_bytes+1 containing data to write
  376. * @reg: register address (just offset will do)
  377. * @num_bytes: number of bytes to transfer
  378. *
  379. * Returns 0 on success or else a negative error code.
  380. */
  381. int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  382. {
  383. struct regmap *regmap = twl_get_regmap(mod_no);
  384. int ret;
  385. if (!regmap)
  386. return -EPERM;
  387. ret = regmap_bulk_write(regmap, twl_priv->twl_map[mod_no].base + reg,
  388. value, num_bytes);
  389. if (ret)
  390. pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n",
  391. DRIVER_NAME, mod_no, reg, num_bytes);
  392. return ret;
  393. }
  394. EXPORT_SYMBOL(twl_i2c_write);
  395. /**
  396. * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
  397. * @mod_no: module number
  398. * @value: an array of num_bytes containing data to be read
  399. * @reg: register address (just offset will do)
  400. * @num_bytes: number of bytes to transfer
  401. *
  402. * Returns 0 on success or else a negative error code.
  403. */
  404. int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  405. {
  406. struct regmap *regmap = twl_get_regmap(mod_no);
  407. int ret;
  408. if (!regmap)
  409. return -EPERM;
  410. ret = regmap_bulk_read(regmap, twl_priv->twl_map[mod_no].base + reg,
  411. value, num_bytes);
  412. if (ret)
  413. pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n",
  414. DRIVER_NAME, mod_no, reg, num_bytes);
  415. return ret;
  416. }
  417. EXPORT_SYMBOL(twl_i2c_read);
  418. /**
  419. * twl_set_regcache_bypass - Configure the regcache bypass for the regmap associated
  420. * with the module
  421. * @mod_no: module number
  422. * @enable: Regcache bypass state
  423. *
  424. * Returns 0 else failure.
  425. */
  426. int twl_set_regcache_bypass(u8 mod_no, bool enable)
  427. {
  428. struct regmap *regmap = twl_get_regmap(mod_no);
  429. if (!regmap)
  430. return -EPERM;
  431. regcache_cache_bypass(regmap, enable);
  432. return 0;
  433. }
  434. EXPORT_SYMBOL(twl_set_regcache_bypass);
  435. /*----------------------------------------------------------------------*/
  436. /**
  437. * twl_read_idcode_register - API to read the IDCODE register.
  438. *
  439. * Unlocks the IDCODE register and read the 32 bit value.
  440. */
  441. static int twl_read_idcode_register(void)
  442. {
  443. int err;
  444. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
  445. REG_UNLOCK_TEST_REG);
  446. if (err) {
  447. pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
  448. goto fail;
  449. }
  450. err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_priv->twl_idcode),
  451. REG_IDCODE_7_0, 4);
  452. if (err) {
  453. pr_err("TWL4030: unable to read IDCODE -%d\n", err);
  454. goto fail;
  455. }
  456. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
  457. if (err)
  458. pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
  459. fail:
  460. return err;
  461. }
  462. /**
  463. * twl_get_type - API to get TWL Si type.
  464. *
  465. * Api to get the TWL Si type from IDCODE value.
  466. */
  467. int twl_get_type(void)
  468. {
  469. return TWL_SIL_TYPE(twl_priv->twl_idcode);
  470. }
  471. EXPORT_SYMBOL_GPL(twl_get_type);
  472. /**
  473. * twl_get_version - API to get TWL Si version.
  474. *
  475. * Api to get the TWL Si version from IDCODE value.
  476. */
  477. int twl_get_version(void)
  478. {
  479. return TWL_SIL_REV(twl_priv->twl_idcode);
  480. }
  481. EXPORT_SYMBOL_GPL(twl_get_version);
  482. /**
  483. * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate.
  484. *
  485. * Api to get the TWL HFCLK rate based on BOOT_CFG register.
  486. */
  487. int twl_get_hfclk_rate(void)
  488. {
  489. u8 ctrl;
  490. int rate;
  491. twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &ctrl, R_CFG_BOOT);
  492. switch (ctrl & 0x3) {
  493. case HFCLK_FREQ_19p2_MHZ:
  494. rate = 19200000;
  495. break;
  496. case HFCLK_FREQ_26_MHZ:
  497. rate = 26000000;
  498. break;
  499. case HFCLK_FREQ_38p4_MHZ:
  500. rate = 38400000;
  501. break;
  502. default:
  503. pr_err("TWL4030: HFCLK is not configured\n");
  504. rate = -EINVAL;
  505. break;
  506. }
  507. return rate;
  508. }
  509. EXPORT_SYMBOL_GPL(twl_get_hfclk_rate);
  510. static struct device *
  511. add_numbered_child(unsigned mod_no, const char *name, int num,
  512. void *pdata, unsigned pdata_len,
  513. bool can_wakeup, int irq0, int irq1)
  514. {
  515. struct platform_device *pdev;
  516. struct twl_client *twl;
  517. int status, sid;
  518. if (unlikely(mod_no >= twl_get_last_module())) {
  519. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  520. return ERR_PTR(-EPERM);
  521. }
  522. sid = twl_priv->twl_map[mod_no].sid;
  523. twl = &twl_priv->twl_modules[sid];
  524. pdev = platform_device_alloc(name, num);
  525. if (!pdev)
  526. return ERR_PTR(-ENOMEM);
  527. pdev->dev.parent = &twl->client->dev;
  528. if (pdata) {
  529. status = platform_device_add_data(pdev, pdata, pdata_len);
  530. if (status < 0) {
  531. dev_dbg(&pdev->dev, "can't add platform_data\n");
  532. goto put_device;
  533. }
  534. }
  535. if (irq0) {
  536. struct resource r[2] = {
  537. { .start = irq0, .flags = IORESOURCE_IRQ, },
  538. { .start = irq1, .flags = IORESOURCE_IRQ, },
  539. };
  540. status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
  541. if (status < 0) {
  542. dev_dbg(&pdev->dev, "can't add irqs\n");
  543. goto put_device;
  544. }
  545. }
  546. status = platform_device_add(pdev);
  547. if (status)
  548. goto put_device;
  549. device_init_wakeup(&pdev->dev, can_wakeup);
  550. return &pdev->dev;
  551. put_device:
  552. platform_device_put(pdev);
  553. dev_err(&twl->client->dev, "failed to add device %s\n", name);
  554. return ERR_PTR(status);
  555. }
  556. static inline struct device *add_child(unsigned mod_no, const char *name,
  557. void *pdata, unsigned pdata_len,
  558. bool can_wakeup, int irq0, int irq1)
  559. {
  560. return add_numbered_child(mod_no, name, -1, pdata, pdata_len,
  561. can_wakeup, irq0, irq1);
  562. }
  563. /*----------------------------------------------------------------------*/
  564. /*
  565. * These three functions initialize the on-chip clock framework,
  566. * letting it generate the right frequencies for USB, MADC, and
  567. * other purposes.
  568. */
  569. static inline int protect_pm_master(void)
  570. {
  571. int e = 0;
  572. e = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
  573. TWL4030_PM_MASTER_PROTECT_KEY);
  574. return e;
  575. }
  576. static inline int unprotect_pm_master(void)
  577. {
  578. int e = 0;
  579. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
  580. TWL4030_PM_MASTER_PROTECT_KEY);
  581. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG2,
  582. TWL4030_PM_MASTER_PROTECT_KEY);
  583. return e;
  584. }
  585. static void clocks_init(struct device *dev)
  586. {
  587. int e = 0;
  588. struct clk *osc;
  589. u32 rate;
  590. u8 ctrl = HFCLK_FREQ_26_MHZ;
  591. osc = clk_get(dev, "fck");
  592. if (IS_ERR(osc)) {
  593. printk(KERN_WARNING "Skipping twl internal clock init and "
  594. "using bootloader value (unknown osc rate)\n");
  595. return;
  596. }
  597. rate = clk_get_rate(osc);
  598. clk_put(osc);
  599. switch (rate) {
  600. case 19200000:
  601. ctrl = HFCLK_FREQ_19p2_MHZ;
  602. break;
  603. case 26000000:
  604. ctrl = HFCLK_FREQ_26_MHZ;
  605. break;
  606. case 38400000:
  607. ctrl = HFCLK_FREQ_38p4_MHZ;
  608. break;
  609. }
  610. ctrl |= HIGH_PERF_SQ;
  611. e |= unprotect_pm_master();
  612. /* effect->MADC+USB ck en */
  613. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  614. e |= protect_pm_master();
  615. if (e < 0)
  616. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  617. }
  618. /*----------------------------------------------------------------------*/
  619. static void twl_remove(struct i2c_client *client)
  620. {
  621. unsigned i, num_slaves;
  622. if (twl_class_is_4030())
  623. twl4030_exit_irq();
  624. else
  625. twl6030_exit_irq();
  626. num_slaves = twl_get_num_slaves();
  627. for (i = 0; i < num_slaves; i++) {
  628. struct twl_client *twl = &twl_priv->twl_modules[i];
  629. if (twl->client && twl->client != client)
  630. i2c_unregister_device(twl->client);
  631. twl->client = NULL;
  632. }
  633. twl_priv->ready = false;
  634. }
  635. static struct of_dev_auxdata twl_auxdata_lookup[] = {
  636. OF_DEV_AUXDATA("ti,twl4030-gpio", 0, "twl4030-gpio", NULL),
  637. { /* sentinel */ },
  638. };
  639. /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
  640. static int
  641. twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
  642. {
  643. struct device_node *node = client->dev.of_node;
  644. struct platform_device *pdev;
  645. const struct regmap_config *twl_regmap_config;
  646. int irq_base = 0;
  647. int status;
  648. unsigned i, num_slaves;
  649. if (!node) {
  650. dev_err(&client->dev, "no platform data\n");
  651. return -EINVAL;
  652. }
  653. if (twl_priv) {
  654. dev_dbg(&client->dev, "only one instance of %s allowed\n",
  655. DRIVER_NAME);
  656. return -EBUSY;
  657. }
  658. pdev = platform_device_alloc(DRIVER_NAME, -1);
  659. if (!pdev) {
  660. dev_err(&client->dev, "can't alloc pdev\n");
  661. return -ENOMEM;
  662. }
  663. status = platform_device_add(pdev);
  664. if (status) {
  665. platform_device_put(pdev);
  666. return status;
  667. }
  668. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  669. dev_dbg(&client->dev, "can't talk I2C?\n");
  670. status = -EIO;
  671. goto free;
  672. }
  673. twl_priv = devm_kzalloc(&client->dev, sizeof(struct twl_private),
  674. GFP_KERNEL);
  675. if (!twl_priv) {
  676. status = -ENOMEM;
  677. goto free;
  678. }
  679. if ((id->driver_data) & TWL6030_CLASS) {
  680. twl_priv->twl_id = TWL6030_CLASS_ID;
  681. twl_priv->twl_map = &twl6030_map[0];
  682. /* The charger base address is different in twl6032 */
  683. if ((id->driver_data) & TWL6032_SUBCLASS)
  684. twl_priv->twl_map[TWL_MODULE_MAIN_CHARGE].base =
  685. TWL6032_BASEADD_CHARGER;
  686. twl_regmap_config = twl6030_regmap_config;
  687. } else {
  688. twl_priv->twl_id = TWL4030_CLASS_ID;
  689. twl_priv->twl_map = &twl4030_map[0];
  690. twl_regmap_config = twl4030_regmap_config;
  691. }
  692. num_slaves = twl_get_num_slaves();
  693. twl_priv->twl_modules = devm_kcalloc(&client->dev,
  694. num_slaves,
  695. sizeof(struct twl_client),
  696. GFP_KERNEL);
  697. if (!twl_priv->twl_modules) {
  698. status = -ENOMEM;
  699. goto free;
  700. }
  701. for (i = 0; i < num_slaves; i++) {
  702. struct twl_client *twl = &twl_priv->twl_modules[i];
  703. if (i == 0) {
  704. twl->client = client;
  705. } else {
  706. twl->client = i2c_new_dummy_device(client->adapter,
  707. client->addr + i);
  708. if (IS_ERR(twl->client)) {
  709. dev_err(&client->dev,
  710. "can't attach client %d\n", i);
  711. status = PTR_ERR(twl->client);
  712. goto fail;
  713. }
  714. }
  715. twl->regmap = devm_regmap_init_i2c(twl->client,
  716. &twl_regmap_config[i]);
  717. if (IS_ERR(twl->regmap)) {
  718. status = PTR_ERR(twl->regmap);
  719. dev_err(&client->dev,
  720. "Failed to allocate regmap %d, err: %d\n", i,
  721. status);
  722. goto fail;
  723. }
  724. }
  725. twl_priv->ready = true;
  726. /* setup clock framework */
  727. clocks_init(&client->dev);
  728. /* read TWL IDCODE Register */
  729. if (twl_class_is_4030()) {
  730. status = twl_read_idcode_register();
  731. WARN(status < 0, "Error: reading twl_idcode register value\n");
  732. }
  733. /* Maybe init the T2 Interrupt subsystem */
  734. if (client->irq) {
  735. if (twl_class_is_4030()) {
  736. twl4030_init_chip_irq(id->name);
  737. irq_base = twl4030_init_irq(&client->dev, client->irq);
  738. } else {
  739. irq_base = twl6030_init_irq(&client->dev, client->irq);
  740. }
  741. if (irq_base < 0) {
  742. status = irq_base;
  743. goto fail;
  744. }
  745. }
  746. /*
  747. * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
  748. * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
  749. * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
  750. *
  751. * Also, always enable SmartReflex bit as that's needed for omaps to
  752. * do anything over I2C4 for voltage scaling even if SmartReflex
  753. * is disabled. Without the SmartReflex bit omap sys_clkreq idle
  754. * signal will never trigger for retention idle.
  755. */
  756. if (twl_class_is_4030()) {
  757. u8 temp;
  758. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
  759. temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
  760. I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
  761. twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
  762. twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &temp,
  763. TWL4030_DCDC_GLOBAL_CFG);
  764. temp |= SMARTREFLEX_ENABLE;
  765. twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER, temp,
  766. TWL4030_DCDC_GLOBAL_CFG);
  767. }
  768. status = of_platform_populate(node, NULL, twl_auxdata_lookup,
  769. &client->dev);
  770. fail:
  771. if (status < 0)
  772. twl_remove(client);
  773. free:
  774. if (status < 0)
  775. platform_device_unregister(pdev);
  776. return status;
  777. }
  778. static int __maybe_unused twl_suspend(struct device *dev)
  779. {
  780. struct i2c_client *client = to_i2c_client(dev);
  781. if (client->irq)
  782. disable_irq(client->irq);
  783. return 0;
  784. }
  785. static int __maybe_unused twl_resume(struct device *dev)
  786. {
  787. struct i2c_client *client = to_i2c_client(dev);
  788. if (client->irq)
  789. enable_irq(client->irq);
  790. return 0;
  791. }
  792. static SIMPLE_DEV_PM_OPS(twl_dev_pm_ops, twl_suspend, twl_resume);
  793. static const struct i2c_device_id twl_ids[] = {
  794. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  795. { "twl5030", 0 }, /* T2 updated */
  796. { "twl5031", TWL5031 }, /* TWL5030 updated */
  797. { "tps65950", 0 }, /* catalog version of twl5030 */
  798. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  799. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  800. { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
  801. and vibrator. Charger in USB module*/
  802. { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
  803. { "twl6032", TWL6030_CLASS | TWL6032_SUBCLASS }, /* "Phoenix lite" */
  804. { /* end of list */ },
  805. };
  806. /* One Client Driver , 4 Clients */
  807. static struct i2c_driver twl_driver = {
  808. .driver.name = DRIVER_NAME,
  809. .driver.pm = &twl_dev_pm_ops,
  810. .id_table = twl_ids,
  811. .probe = twl_probe,
  812. .remove = twl_remove,
  813. };
  814. builtin_i2c_driver(twl_driver);