twl-core.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /*
  2. * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
  3. * and audio CODEC devices
  4. *
  5. * Copyright (C) 2005-2006 Texas Instruments, Inc.
  6. *
  7. * Modifications to defer interrupt handling to a kernel thread:
  8. * Copyright (C) 2006 MontaVista Software, Inc.
  9. *
  10. * Based on tlv320aic23.c:
  11. * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
  12. *
  13. * Code cleanup and modifications to IRQ handler.
  14. * by syed khasim <x0khasim@ti.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #include <linux/init.h>
  31. #include <linux/mutex.h>
  32. #include <linux/module.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/clk.h>
  35. #include <linux/err.h>
  36. #include <linux/device.h>
  37. #include <linux/of.h>
  38. #include <linux/of_irq.h>
  39. #include <linux/of_platform.h>
  40. #include <linux/irq.h>
  41. #include <linux/irqdomain.h>
  42. #include <linux/regulator/machine.h>
  43. #include <linux/i2c.h>
  44. #include <linux/i2c/twl.h>
  45. #include "twl-core.h"
  46. /*
  47. * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
  48. * Management and System Companion Device" chips originally designed for
  49. * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
  50. * often at around 3 Mbit/sec, including for interrupt handling.
  51. *
  52. * This driver core provides genirq support for the interrupts emitted,
  53. * by the various modules, and exports register access primitives.
  54. *
  55. * FIXME this driver currently requires use of the first interrupt line
  56. * (and associated registers).
  57. */
  58. #define DRIVER_NAME "twl"
  59. /* Triton Core internal information (BEGIN) */
  60. /* Last - for index max*/
  61. #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
  62. #define TWL_NUM_SLAVES 4
  63. #define SUB_CHIP_ID0 0
  64. #define SUB_CHIP_ID1 1
  65. #define SUB_CHIP_ID2 2
  66. #define SUB_CHIP_ID3 3
  67. #define SUB_CHIP_ID_INVAL 0xff
  68. #define TWL_MODULE_LAST TWL4030_MODULE_LAST
  69. /* Base Address defns for twl4030_map[] */
  70. /* subchip/slave 0 - USB ID */
  71. #define TWL4030_BASEADD_USB 0x0000
  72. /* subchip/slave 1 - AUD ID */
  73. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  74. #define TWL4030_BASEADD_GPIO 0x0098
  75. #define TWL4030_BASEADD_INTBR 0x0085
  76. #define TWL4030_BASEADD_PIH 0x0080
  77. #define TWL4030_BASEADD_TEST 0x004C
  78. /* subchip/slave 2 - AUX ID */
  79. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  80. #define TWL4030_BASEADD_LED 0x00EE
  81. #define TWL4030_BASEADD_MADC 0x0000
  82. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  83. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  84. #define TWL4030_BASEADD_PWM0 0x00F8
  85. #define TWL4030_BASEADD_PWM1 0x00FB
  86. #define TWL4030_BASEADD_PWMA 0x00EF
  87. #define TWL4030_BASEADD_PWMB 0x00F1
  88. #define TWL4030_BASEADD_KEYPAD 0x00D2
  89. #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
  90. #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
  91. one */
  92. /* subchip/slave 3 - POWER ID */
  93. #define TWL4030_BASEADD_BACKUP 0x0014
  94. #define TWL4030_BASEADD_INT 0x002E
  95. #define TWL4030_BASEADD_PM_MASTER 0x0036
  96. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  97. #define TWL4030_BASEADD_RTC 0x001C
  98. #define TWL4030_BASEADD_SECURED_REG 0x0000
  99. /* Triton Core internal information (END) */
  100. /* subchip/slave 0 0x48 - POWER */
  101. #define TWL6030_BASEADD_RTC 0x0000
  102. #define TWL6030_BASEADD_MEM 0x0017
  103. #define TWL6030_BASEADD_PM_MASTER 0x001F
  104. #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
  105. #define TWL6030_BASEADD_PM_MISC 0x00E2
  106. #define TWL6030_BASEADD_PM_PUPD 0x00F0
  107. /* subchip/slave 1 0x49 - FEATURE */
  108. #define TWL6030_BASEADD_USB 0x0000
  109. #define TWL6030_BASEADD_GPADC_CTRL 0x002E
  110. #define TWL6030_BASEADD_AUX 0x0090
  111. #define TWL6030_BASEADD_PWM 0x00BA
  112. #define TWL6030_BASEADD_GASGAUGE 0x00C0
  113. #define TWL6030_BASEADD_PIH 0x00D0
  114. #define TWL6030_BASEADD_CHARGER 0x00E0
  115. #define TWL6025_BASEADD_CHARGER 0x00DA
  116. /* subchip/slave 2 0x4A - DFT */
  117. #define TWL6030_BASEADD_DIEID 0x00C0
  118. /* subchip/slave 3 0x4B - AUDIO */
  119. #define TWL6030_BASEADD_AUDIO 0x0000
  120. #define TWL6030_BASEADD_RSV 0x0000
  121. #define TWL6030_BASEADD_ZERO 0x0000
  122. /* Few power values */
  123. #define R_CFG_BOOT 0x05
  124. /* some fields in R_CFG_BOOT */
  125. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  126. #define HFCLK_FREQ_26_MHZ (2 << 0)
  127. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  128. #define HIGH_PERF_SQ (1 << 3)
  129. #define CK32K_LOWPWR_EN (1 << 7)
  130. /*----------------------------------------------------------------------*/
  131. /* is driver active, bound to a chip? */
  132. static bool inuse;
  133. /* TWL IDCODE Register value */
  134. static u32 twl_idcode;
  135. static unsigned int twl_id;
  136. unsigned int twl_rev(void)
  137. {
  138. return twl_id;
  139. }
  140. EXPORT_SYMBOL(twl_rev);
  141. /* Structure for each TWL4030/TWL6030 Slave */
  142. struct twl_client {
  143. struct i2c_client *client;
  144. u8 address;
  145. /* max numb of i2c_msg required is for read =2 */
  146. struct i2c_msg xfer_msg[2];
  147. /* To lock access to xfer_msg */
  148. struct mutex xfer_lock;
  149. };
  150. static struct twl_client twl_modules[TWL_NUM_SLAVES];
  151. /* mapping the module id to slave id and base address */
  152. struct twl_mapping {
  153. unsigned char sid; /* Slave ID */
  154. unsigned char base; /* base address */
  155. };
  156. static struct twl_mapping *twl_map;
  157. static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  158. /*
  159. * NOTE: don't change this table without updating the
  160. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  161. * so they continue to match the order in this table.
  162. */
  163. { 0, TWL4030_BASEADD_USB },
  164. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  165. { 1, TWL4030_BASEADD_GPIO },
  166. { 1, TWL4030_BASEADD_INTBR },
  167. { 1, TWL4030_BASEADD_PIH },
  168. { 1, TWL4030_BASEADD_TEST },
  169. { 2, TWL4030_BASEADD_KEYPAD },
  170. { 2, TWL4030_BASEADD_MADC },
  171. { 2, TWL4030_BASEADD_INTERRUPTS },
  172. { 2, TWL4030_BASEADD_LED },
  173. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  174. { 2, TWL4030_BASEADD_PRECHARGE },
  175. { 2, TWL4030_BASEADD_PWM0 },
  176. { 2, TWL4030_BASEADD_PWM1 },
  177. { 2, TWL4030_BASEADD_PWMA },
  178. { 2, TWL4030_BASEADD_PWMB },
  179. { 2, TWL5031_BASEADD_ACCESSORY },
  180. { 2, TWL5031_BASEADD_INTERRUPTS },
  181. { 3, TWL4030_BASEADD_BACKUP },
  182. { 3, TWL4030_BASEADD_INT },
  183. { 3, TWL4030_BASEADD_PM_MASTER },
  184. { 3, TWL4030_BASEADD_PM_RECEIVER },
  185. { 3, TWL4030_BASEADD_RTC },
  186. { 3, TWL4030_BASEADD_SECURED_REG },
  187. };
  188. static struct twl_mapping twl6030_map[] = {
  189. /*
  190. * NOTE: don't change this table without updating the
  191. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  192. * so they continue to match the order in this table.
  193. */
  194. { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
  195. { SUB_CHIP_ID_INVAL, TWL6030_BASEADD_AUDIO },
  196. { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
  197. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  198. { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
  199. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  200. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  201. { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
  202. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  203. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  204. { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
  205. { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
  206. { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
  207. { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
  208. { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
  209. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  210. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  211. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  212. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  213. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  214. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
  215. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
  216. { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
  217. { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
  218. { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
  219. };
  220. /*----------------------------------------------------------------------*/
  221. /* Exported Functions */
  222. /**
  223. * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
  224. * @mod_no: module number
  225. * @value: an array of num_bytes+1 containing data to write
  226. * @reg: register address (just offset will do)
  227. * @num_bytes: number of bytes to transfer
  228. *
  229. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  230. * valid data starts at Offset 1.
  231. *
  232. * Returns the result of operation - 0 is success
  233. */
  234. int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  235. {
  236. int ret;
  237. int sid;
  238. struct twl_client *twl;
  239. struct i2c_msg *msg;
  240. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  241. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  242. return -EPERM;
  243. }
  244. if (unlikely(!inuse)) {
  245. pr_err("%s: not initialized\n", DRIVER_NAME);
  246. return -EPERM;
  247. }
  248. sid = twl_map[mod_no].sid;
  249. if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
  250. pr_err("%s: module %d is not part of the pmic\n",
  251. DRIVER_NAME, mod_no);
  252. return -EINVAL;
  253. }
  254. twl = &twl_modules[sid];
  255. mutex_lock(&twl->xfer_lock);
  256. /*
  257. * [MSG1]: fill the register address data
  258. * fill the data Tx buffer
  259. */
  260. msg = &twl->xfer_msg[0];
  261. msg->addr = twl->address;
  262. msg->len = num_bytes + 1;
  263. msg->flags = 0;
  264. msg->buf = value;
  265. /* over write the first byte of buffer with the register address */
  266. *value = twl_map[mod_no].base + reg;
  267. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
  268. mutex_unlock(&twl->xfer_lock);
  269. /* i2c_transfer returns number of messages transferred */
  270. if (ret != 1) {
  271. pr_err("%s: i2c_write failed to transfer all messages\n",
  272. DRIVER_NAME);
  273. if (ret < 0)
  274. return ret;
  275. else
  276. return -EIO;
  277. } else {
  278. return 0;
  279. }
  280. }
  281. EXPORT_SYMBOL(twl_i2c_write);
  282. /**
  283. * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
  284. * @mod_no: module number
  285. * @value: an array of num_bytes containing data to be read
  286. * @reg: register address (just offset will do)
  287. * @num_bytes: number of bytes to transfer
  288. *
  289. * Returns result of operation - num_bytes is success else failure.
  290. */
  291. int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  292. {
  293. int ret;
  294. u8 val;
  295. int sid;
  296. struct twl_client *twl;
  297. struct i2c_msg *msg;
  298. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  299. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  300. return -EPERM;
  301. }
  302. if (unlikely(!inuse)) {
  303. pr_err("%s: not initialized\n", DRIVER_NAME);
  304. return -EPERM;
  305. }
  306. sid = twl_map[mod_no].sid;
  307. if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
  308. pr_err("%s: module %d is not part of the pmic\n",
  309. DRIVER_NAME, mod_no);
  310. return -EINVAL;
  311. }
  312. twl = &twl_modules[sid];
  313. mutex_lock(&twl->xfer_lock);
  314. /* [MSG1] fill the register address data */
  315. msg = &twl->xfer_msg[0];
  316. msg->addr = twl->address;
  317. msg->len = 1;
  318. msg->flags = 0; /* Read the register value */
  319. val = twl_map[mod_no].base + reg;
  320. msg->buf = &val;
  321. /* [MSG2] fill the data rx buffer */
  322. msg = &twl->xfer_msg[1];
  323. msg->addr = twl->address;
  324. msg->flags = I2C_M_RD; /* Read the register value */
  325. msg->len = num_bytes; /* only n bytes */
  326. msg->buf = value;
  327. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  328. mutex_unlock(&twl->xfer_lock);
  329. /* i2c_transfer returns number of messages transferred */
  330. if (ret != 2) {
  331. pr_err("%s: i2c_read failed to transfer all messages\n",
  332. DRIVER_NAME);
  333. if (ret < 0)
  334. return ret;
  335. else
  336. return -EIO;
  337. } else {
  338. return 0;
  339. }
  340. }
  341. EXPORT_SYMBOL(twl_i2c_read);
  342. /**
  343. * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
  344. * @mod_no: module number
  345. * @value: the value to be written 8 bit
  346. * @reg: register address (just offset will do)
  347. *
  348. * Returns result of operation - 0 is success
  349. */
  350. int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  351. {
  352. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  353. u8 temp_buffer[2] = { 0 };
  354. /* offset 1 contains the data */
  355. temp_buffer[1] = value;
  356. return twl_i2c_write(mod_no, temp_buffer, reg, 1);
  357. }
  358. EXPORT_SYMBOL(twl_i2c_write_u8);
  359. /**
  360. * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
  361. * @mod_no: module number
  362. * @value: the value read 8 bit
  363. * @reg: register address (just offset will do)
  364. *
  365. * Returns result of operation - 0 is success
  366. */
  367. int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  368. {
  369. return twl_i2c_read(mod_no, value, reg, 1);
  370. }
  371. EXPORT_SYMBOL(twl_i2c_read_u8);
  372. /*----------------------------------------------------------------------*/
  373. /**
  374. * twl_read_idcode_register - API to read the IDCODE register.
  375. *
  376. * Unlocks the IDCODE register and read the 32 bit value.
  377. */
  378. static int twl_read_idcode_register(void)
  379. {
  380. int err;
  381. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
  382. REG_UNLOCK_TEST_REG);
  383. if (err) {
  384. pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
  385. goto fail;
  386. }
  387. err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
  388. REG_IDCODE_7_0, 4);
  389. if (err) {
  390. pr_err("TWL4030: unable to read IDCODE -%d\n", err);
  391. goto fail;
  392. }
  393. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
  394. if (err)
  395. pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
  396. fail:
  397. return err;
  398. }
  399. /**
  400. * twl_get_type - API to get TWL Si type.
  401. *
  402. * Api to get the TWL Si type from IDCODE value.
  403. */
  404. int twl_get_type(void)
  405. {
  406. return TWL_SIL_TYPE(twl_idcode);
  407. }
  408. EXPORT_SYMBOL_GPL(twl_get_type);
  409. /**
  410. * twl_get_version - API to get TWL Si version.
  411. *
  412. * Api to get the TWL Si version from IDCODE value.
  413. */
  414. int twl_get_version(void)
  415. {
  416. return TWL_SIL_REV(twl_idcode);
  417. }
  418. EXPORT_SYMBOL_GPL(twl_get_version);
  419. /**
  420. * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate.
  421. *
  422. * Api to get the TWL HFCLK rate based on BOOT_CFG register.
  423. */
  424. int twl_get_hfclk_rate(void)
  425. {
  426. u8 ctrl;
  427. int rate;
  428. twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &ctrl, R_CFG_BOOT);
  429. switch (ctrl & 0x3) {
  430. case HFCLK_FREQ_19p2_MHZ:
  431. rate = 19200000;
  432. break;
  433. case HFCLK_FREQ_26_MHZ:
  434. rate = 26000000;
  435. break;
  436. case HFCLK_FREQ_38p4_MHZ:
  437. rate = 38400000;
  438. break;
  439. default:
  440. pr_err("TWL4030: HFCLK is not configured\n");
  441. rate = -EINVAL;
  442. break;
  443. }
  444. return rate;
  445. }
  446. EXPORT_SYMBOL_GPL(twl_get_hfclk_rate);
  447. static struct device *
  448. add_numbered_child(unsigned chip, const char *name, int num,
  449. void *pdata, unsigned pdata_len,
  450. bool can_wakeup, int irq0, int irq1)
  451. {
  452. struct platform_device *pdev;
  453. struct twl_client *twl = &twl_modules[chip];
  454. int status;
  455. pdev = platform_device_alloc(name, num);
  456. if (!pdev) {
  457. dev_dbg(&twl->client->dev, "can't alloc dev\n");
  458. status = -ENOMEM;
  459. goto err;
  460. }
  461. pdev->dev.parent = &twl->client->dev;
  462. if (pdata) {
  463. status = platform_device_add_data(pdev, pdata, pdata_len);
  464. if (status < 0) {
  465. dev_dbg(&pdev->dev, "can't add platform_data\n");
  466. goto err;
  467. }
  468. }
  469. if (irq0) {
  470. struct resource r[2] = {
  471. { .start = irq0, .flags = IORESOURCE_IRQ, },
  472. { .start = irq1, .flags = IORESOURCE_IRQ, },
  473. };
  474. status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
  475. if (status < 0) {
  476. dev_dbg(&pdev->dev, "can't add irqs\n");
  477. goto err;
  478. }
  479. }
  480. status = platform_device_add(pdev);
  481. if (status == 0)
  482. device_init_wakeup(&pdev->dev, can_wakeup);
  483. err:
  484. if (status < 0) {
  485. platform_device_put(pdev);
  486. dev_err(&twl->client->dev, "can't add %s dev\n", name);
  487. return ERR_PTR(status);
  488. }
  489. return &pdev->dev;
  490. }
  491. static inline struct device *add_child(unsigned chip, const char *name,
  492. void *pdata, unsigned pdata_len,
  493. bool can_wakeup, int irq0, int irq1)
  494. {
  495. return add_numbered_child(chip, name, -1, pdata, pdata_len,
  496. can_wakeup, irq0, irq1);
  497. }
  498. static struct device *
  499. add_regulator_linked(int num, struct regulator_init_data *pdata,
  500. struct regulator_consumer_supply *consumers,
  501. unsigned num_consumers, unsigned long features)
  502. {
  503. unsigned sub_chip_id;
  504. struct twl_regulator_driver_data drv_data;
  505. /* regulator framework demands init_data ... */
  506. if (!pdata)
  507. return NULL;
  508. if (consumers) {
  509. pdata->consumer_supplies = consumers;
  510. pdata->num_consumer_supplies = num_consumers;
  511. }
  512. if (pdata->driver_data) {
  513. /* If we have existing drv_data, just add the flags */
  514. struct twl_regulator_driver_data *tmp;
  515. tmp = pdata->driver_data;
  516. tmp->features |= features;
  517. } else {
  518. /* add new driver data struct, used only during init */
  519. drv_data.features = features;
  520. drv_data.set_voltage = NULL;
  521. drv_data.get_voltage = NULL;
  522. drv_data.data = NULL;
  523. pdata->driver_data = &drv_data;
  524. }
  525. /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
  526. sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
  527. return add_numbered_child(sub_chip_id, "twl_reg", num,
  528. pdata, sizeof(*pdata), false, 0, 0);
  529. }
  530. static struct device *
  531. add_regulator(int num, struct regulator_init_data *pdata,
  532. unsigned long features)
  533. {
  534. return add_regulator_linked(num, pdata, NULL, 0, features);
  535. }
  536. /*
  537. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  538. * for the PIH, and the next are for the PWR_INT SIH, since
  539. * that's how twl_init_irq() sets things up.
  540. */
  541. static int
  542. add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
  543. unsigned long features)
  544. {
  545. struct device *child;
  546. unsigned sub_chip_id;
  547. if (IS_ENABLED(CONFIG_GPIO_TWL4030) && pdata->gpio) {
  548. child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
  549. pdata->gpio, sizeof(*pdata->gpio),
  550. false, irq_base + GPIO_INTR_OFFSET, 0);
  551. if (IS_ERR(child))
  552. return PTR_ERR(child);
  553. }
  554. if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030) && pdata->keypad) {
  555. child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
  556. pdata->keypad, sizeof(*pdata->keypad),
  557. true, irq_base + KEYPAD_INTR_OFFSET, 0);
  558. if (IS_ERR(child))
  559. return PTR_ERR(child);
  560. }
  561. if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc) {
  562. child = add_child(2, "twl4030_madc",
  563. pdata->madc, sizeof(*pdata->madc),
  564. true, irq_base + MADC_INTR_OFFSET, 0);
  565. if (IS_ERR(child))
  566. return PTR_ERR(child);
  567. }
  568. if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030)) {
  569. /*
  570. * REVISIT platform_data here currently might expose the
  571. * "msecure" line ... but for now we just expect board
  572. * setup to tell the chip "it's always ok to SET_TIME".
  573. * Eventually, Linux might become more aware of such
  574. * HW security concerns, and "least privilege".
  575. */
  576. sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
  577. child = add_child(sub_chip_id, "twl_rtc",
  578. NULL, 0,
  579. true, irq_base + RTC_INTR_OFFSET, 0);
  580. if (IS_ERR(child))
  581. return PTR_ERR(child);
  582. }
  583. if (IS_ENABLED(CONFIG_TWL4030_USB) && pdata->usb &&
  584. twl_class_is_4030()) {
  585. static struct regulator_consumer_supply usb1v5 = {
  586. .supply = "usb1v5",
  587. };
  588. static struct regulator_consumer_supply usb1v8 = {
  589. .supply = "usb1v8",
  590. };
  591. static struct regulator_consumer_supply usb3v1[] = {
  592. { .supply = "usb3v1" },
  593. { .supply = "bci3v1" },
  594. };
  595. /* First add the regulators so that they can be used by transceiver */
  596. if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
  597. /* this is a template that gets copied */
  598. struct regulator_init_data usb_fixed = {
  599. .constraints.valid_modes_mask =
  600. REGULATOR_MODE_NORMAL
  601. | REGULATOR_MODE_STANDBY,
  602. .constraints.valid_ops_mask =
  603. REGULATOR_CHANGE_MODE
  604. | REGULATOR_CHANGE_STATUS,
  605. };
  606. child = add_regulator_linked(TWL4030_REG_VUSB1V5,
  607. &usb_fixed, &usb1v5, 1,
  608. features);
  609. if (IS_ERR(child))
  610. return PTR_ERR(child);
  611. child = add_regulator_linked(TWL4030_REG_VUSB1V8,
  612. &usb_fixed, &usb1v8, 1,
  613. features);
  614. if (IS_ERR(child))
  615. return PTR_ERR(child);
  616. child = add_regulator_linked(TWL4030_REG_VUSB3V1,
  617. &usb_fixed, usb3v1, 2,
  618. features);
  619. if (IS_ERR(child))
  620. return PTR_ERR(child);
  621. }
  622. child = add_child(0, "twl4030_usb",
  623. pdata->usb, sizeof(*pdata->usb),
  624. true,
  625. /* irq0 = USB_PRES, irq1 = USB */
  626. irq_base + USB_PRES_INTR_OFFSET,
  627. irq_base + USB_INTR_OFFSET);
  628. if (IS_ERR(child))
  629. return PTR_ERR(child);
  630. /* we need to connect regulators to this transceiver */
  631. if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child) {
  632. usb1v5.dev_name = dev_name(child);
  633. usb1v8.dev_name = dev_name(child);
  634. usb3v1[0].dev_name = dev_name(child);
  635. }
  636. }
  637. if (IS_ENABLED(CONFIG_TWL6030_USB) && pdata->usb &&
  638. twl_class_is_6030()) {
  639. static struct regulator_consumer_supply usb3v3;
  640. int regulator;
  641. if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
  642. /* this is a template that gets copied */
  643. struct regulator_init_data usb_fixed = {
  644. .constraints.valid_modes_mask =
  645. REGULATOR_MODE_NORMAL
  646. | REGULATOR_MODE_STANDBY,
  647. .constraints.valid_ops_mask =
  648. REGULATOR_CHANGE_MODE
  649. | REGULATOR_CHANGE_STATUS,
  650. };
  651. if (features & TWL6025_SUBCLASS) {
  652. usb3v3.supply = "ldousb";
  653. regulator = TWL6025_REG_LDOUSB;
  654. } else {
  655. usb3v3.supply = "vusb";
  656. regulator = TWL6030_REG_VUSB;
  657. }
  658. child = add_regulator_linked(regulator, &usb_fixed,
  659. &usb3v3, 1,
  660. features);
  661. if (IS_ERR(child))
  662. return PTR_ERR(child);
  663. }
  664. pdata->usb->features = features;
  665. child = add_child(0, "twl6030_usb",
  666. pdata->usb, sizeof(*pdata->usb),
  667. true,
  668. /* irq1 = VBUS_PRES, irq0 = USB ID */
  669. irq_base + USBOTG_INTR_OFFSET,
  670. irq_base + USB_PRES_INTR_OFFSET);
  671. if (IS_ERR(child))
  672. return PTR_ERR(child);
  673. /* we need to connect regulators to this transceiver */
  674. if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child)
  675. usb3v3.dev_name = dev_name(child);
  676. } else if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) &&
  677. twl_class_is_6030()) {
  678. if (features & TWL6025_SUBCLASS)
  679. child = add_regulator(TWL6025_REG_LDOUSB,
  680. pdata->ldousb, features);
  681. else
  682. child = add_regulator(TWL6030_REG_VUSB,
  683. pdata->vusb, features);
  684. if (IS_ERR(child))
  685. return PTR_ERR(child);
  686. }
  687. if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG) && twl_class_is_4030()) {
  688. child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
  689. if (IS_ERR(child))
  690. return PTR_ERR(child);
  691. }
  692. if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON) && twl_class_is_4030()) {
  693. child = add_child(1, "twl4030_pwrbutton",
  694. NULL, 0, true, irq_base + 8 + 0, 0);
  695. if (IS_ERR(child))
  696. return PTR_ERR(child);
  697. }
  698. if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO) && pdata->audio &&
  699. twl_class_is_4030()) {
  700. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  701. child = add_child(sub_chip_id, "twl4030-audio",
  702. pdata->audio, sizeof(*pdata->audio),
  703. false, 0, 0);
  704. if (IS_ERR(child))
  705. return PTR_ERR(child);
  706. }
  707. /* twl4030 regulators */
  708. if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_4030()) {
  709. child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
  710. features);
  711. if (IS_ERR(child))
  712. return PTR_ERR(child);
  713. child = add_regulator(TWL4030_REG_VIO, pdata->vio,
  714. features);
  715. if (IS_ERR(child))
  716. return PTR_ERR(child);
  717. child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
  718. features);
  719. if (IS_ERR(child))
  720. return PTR_ERR(child);
  721. child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
  722. features);
  723. if (IS_ERR(child))
  724. return PTR_ERR(child);
  725. child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
  726. features);
  727. if (IS_ERR(child))
  728. return PTR_ERR(child);
  729. child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
  730. features);
  731. if (IS_ERR(child))
  732. return PTR_ERR(child);
  733. child = add_regulator((features & TWL4030_VAUX2)
  734. ? TWL4030_REG_VAUX2_4030
  735. : TWL4030_REG_VAUX2,
  736. pdata->vaux2, features);
  737. if (IS_ERR(child))
  738. return PTR_ERR(child);
  739. child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
  740. features);
  741. if (IS_ERR(child))
  742. return PTR_ERR(child);
  743. child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
  744. features);
  745. if (IS_ERR(child))
  746. return PTR_ERR(child);
  747. child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
  748. features);
  749. if (IS_ERR(child))
  750. return PTR_ERR(child);
  751. }
  752. /* maybe add LDOs that are omitted on cost-reduced parts */
  753. if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && !(features & TPS_SUBSET)
  754. && twl_class_is_4030()) {
  755. child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
  756. features);
  757. if (IS_ERR(child))
  758. return PTR_ERR(child);
  759. child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
  760. features);
  761. if (IS_ERR(child))
  762. return PTR_ERR(child);
  763. child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
  764. features);
  765. if (IS_ERR(child))
  766. return PTR_ERR(child);
  767. child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
  768. features);
  769. if (IS_ERR(child))
  770. return PTR_ERR(child);
  771. child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
  772. features);
  773. if (IS_ERR(child))
  774. return PTR_ERR(child);
  775. child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
  776. features);
  777. if (IS_ERR(child))
  778. return PTR_ERR(child);
  779. }
  780. /* twl6030 regulators */
  781. if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
  782. !(features & TWL6025_SUBCLASS)) {
  783. child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1,
  784. features);
  785. if (IS_ERR(child))
  786. return PTR_ERR(child);
  787. child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2,
  788. features);
  789. if (IS_ERR(child))
  790. return PTR_ERR(child);
  791. child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3,
  792. features);
  793. if (IS_ERR(child))
  794. return PTR_ERR(child);
  795. child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8,
  796. features);
  797. if (IS_ERR(child))
  798. return PTR_ERR(child);
  799. child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1,
  800. features);
  801. if (IS_ERR(child))
  802. return PTR_ERR(child);
  803. child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
  804. features);
  805. if (IS_ERR(child))
  806. return PTR_ERR(child);
  807. child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
  808. features);
  809. if (IS_ERR(child))
  810. return PTR_ERR(child);
  811. child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
  812. features);
  813. if (IS_ERR(child))
  814. return PTR_ERR(child);
  815. child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
  816. features);
  817. if (IS_ERR(child))
  818. return PTR_ERR(child);
  819. child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
  820. features);
  821. if (IS_ERR(child))
  822. return PTR_ERR(child);
  823. child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
  824. features);
  825. if (IS_ERR(child))
  826. return PTR_ERR(child);
  827. child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
  828. features);
  829. if (IS_ERR(child))
  830. return PTR_ERR(child);
  831. child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
  832. features);
  833. if (IS_ERR(child))
  834. return PTR_ERR(child);
  835. child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
  836. features);
  837. if (IS_ERR(child))
  838. return PTR_ERR(child);
  839. }
  840. /* 6030 and 6025 share this regulator */
  841. if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030()) {
  842. child = add_regulator(TWL6030_REG_VANA, pdata->vana,
  843. features);
  844. if (IS_ERR(child))
  845. return PTR_ERR(child);
  846. }
  847. /* twl6025 regulators */
  848. if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
  849. (features & TWL6025_SUBCLASS)) {
  850. child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
  851. features);
  852. if (IS_ERR(child))
  853. return PTR_ERR(child);
  854. child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
  855. features);
  856. if (IS_ERR(child))
  857. return PTR_ERR(child);
  858. child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
  859. features);
  860. if (IS_ERR(child))
  861. return PTR_ERR(child);
  862. child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
  863. features);
  864. if (IS_ERR(child))
  865. return PTR_ERR(child);
  866. child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
  867. features);
  868. if (IS_ERR(child))
  869. return PTR_ERR(child);
  870. child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
  871. features);
  872. if (IS_ERR(child))
  873. return PTR_ERR(child);
  874. child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
  875. features);
  876. if (IS_ERR(child))
  877. return PTR_ERR(child);
  878. child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
  879. features);
  880. if (IS_ERR(child))
  881. return PTR_ERR(child);
  882. child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
  883. features);
  884. if (IS_ERR(child))
  885. return PTR_ERR(child);
  886. child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
  887. features);
  888. if (IS_ERR(child))
  889. return PTR_ERR(child);
  890. child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
  891. features);
  892. if (IS_ERR(child))
  893. return PTR_ERR(child);
  894. }
  895. if (IS_ENABLED(CONFIG_CHARGER_TWL4030) && pdata->bci &&
  896. !(features & (TPS_SUBSET | TWL5031))) {
  897. child = add_child(3, "twl4030_bci",
  898. pdata->bci, sizeof(*pdata->bci), false,
  899. /* irq0 = CHG_PRES, irq1 = BCI */
  900. irq_base + BCI_PRES_INTR_OFFSET,
  901. irq_base + BCI_INTR_OFFSET);
  902. if (IS_ERR(child))
  903. return PTR_ERR(child);
  904. }
  905. return 0;
  906. }
  907. /*----------------------------------------------------------------------*/
  908. /*
  909. * These three functions initialize the on-chip clock framework,
  910. * letting it generate the right frequencies for USB, MADC, and
  911. * other purposes.
  912. */
  913. static inline int __init protect_pm_master(void)
  914. {
  915. int e = 0;
  916. e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
  917. TWL4030_PM_MASTER_PROTECT_KEY);
  918. return e;
  919. }
  920. static inline int __init unprotect_pm_master(void)
  921. {
  922. int e = 0;
  923. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  924. TWL4030_PM_MASTER_KEY_CFG1,
  925. TWL4030_PM_MASTER_PROTECT_KEY);
  926. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  927. TWL4030_PM_MASTER_KEY_CFG2,
  928. TWL4030_PM_MASTER_PROTECT_KEY);
  929. return e;
  930. }
  931. static void clocks_init(struct device *dev,
  932. struct twl4030_clock_init_data *clock)
  933. {
  934. int e = 0;
  935. struct clk *osc;
  936. u32 rate;
  937. u8 ctrl = HFCLK_FREQ_26_MHZ;
  938. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  939. if (cpu_is_omap2430())
  940. osc = clk_get(dev, "osc_ck");
  941. else
  942. osc = clk_get(dev, "osc_sys_ck");
  943. if (IS_ERR(osc)) {
  944. printk(KERN_WARNING "Skipping twl internal clock init and "
  945. "using bootloader value (unknown osc rate)\n");
  946. return;
  947. }
  948. rate = clk_get_rate(osc);
  949. clk_put(osc);
  950. #else
  951. /* REVISIT for non-OMAP systems, pass the clock rate from
  952. * board init code, using platform_data.
  953. */
  954. osc = ERR_PTR(-EIO);
  955. printk(KERN_WARNING "Skipping twl internal clock init and "
  956. "using bootloader value (unknown osc rate)\n");
  957. return;
  958. #endif
  959. switch (rate) {
  960. case 19200000:
  961. ctrl = HFCLK_FREQ_19p2_MHZ;
  962. break;
  963. case 26000000:
  964. ctrl = HFCLK_FREQ_26_MHZ;
  965. break;
  966. case 38400000:
  967. ctrl = HFCLK_FREQ_38p4_MHZ;
  968. break;
  969. }
  970. ctrl |= HIGH_PERF_SQ;
  971. if (clock && clock->ck32k_lowpwr_enable)
  972. ctrl |= CK32K_LOWPWR_EN;
  973. e |= unprotect_pm_master();
  974. /* effect->MADC+USB ck en */
  975. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  976. e |= protect_pm_master();
  977. if (e < 0)
  978. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  979. }
  980. /*----------------------------------------------------------------------*/
  981. static int twl_remove(struct i2c_client *client)
  982. {
  983. unsigned i, num_slaves;
  984. int status;
  985. if (twl_class_is_4030()) {
  986. status = twl4030_exit_irq();
  987. num_slaves = TWL_NUM_SLAVES;
  988. } else {
  989. status = twl6030_exit_irq();
  990. num_slaves = TWL_NUM_SLAVES - 1;
  991. }
  992. if (status < 0)
  993. return status;
  994. for (i = 0; i < num_slaves; i++) {
  995. struct twl_client *twl = &twl_modules[i];
  996. if (twl->client && twl->client != client)
  997. i2c_unregister_device(twl->client);
  998. twl_modules[i].client = NULL;
  999. }
  1000. inuse = false;
  1001. return 0;
  1002. }
  1003. /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
  1004. static int __devinit
  1005. twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1006. {
  1007. struct twl4030_platform_data *pdata = client->dev.platform_data;
  1008. struct device_node *node = client->dev.of_node;
  1009. int irq_base = 0;
  1010. int status;
  1011. unsigned i, num_slaves;
  1012. if (node && !pdata) {
  1013. /*
  1014. * XXX: Temporary pdata until the information is correctly
  1015. * retrieved by every TWL modules from DT.
  1016. */
  1017. pdata = devm_kzalloc(&client->dev,
  1018. sizeof(struct twl4030_platform_data),
  1019. GFP_KERNEL);
  1020. if (!pdata)
  1021. return -ENOMEM;
  1022. }
  1023. if (!pdata) {
  1024. dev_dbg(&client->dev, "no platform data?\n");
  1025. return -EINVAL;
  1026. }
  1027. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  1028. dev_dbg(&client->dev, "can't talk I2C?\n");
  1029. return -EIO;
  1030. }
  1031. if (inuse) {
  1032. dev_dbg(&client->dev, "driver is already in use\n");
  1033. return -EBUSY;
  1034. }
  1035. if ((id->driver_data) & TWL6030_CLASS) {
  1036. twl_id = TWL6030_CLASS_ID;
  1037. twl_map = &twl6030_map[0];
  1038. num_slaves = TWL_NUM_SLAVES - 1;
  1039. } else {
  1040. twl_id = TWL4030_CLASS_ID;
  1041. twl_map = &twl4030_map[0];
  1042. num_slaves = TWL_NUM_SLAVES;
  1043. }
  1044. for (i = 0; i < num_slaves; i++) {
  1045. struct twl_client *twl = &twl_modules[i];
  1046. twl->address = client->addr + i;
  1047. if (i == 0) {
  1048. twl->client = client;
  1049. } else {
  1050. twl->client = i2c_new_dummy(client->adapter,
  1051. twl->address);
  1052. if (!twl->client) {
  1053. dev_err(&client->dev,
  1054. "can't attach client %d\n", i);
  1055. status = -ENOMEM;
  1056. goto fail;
  1057. }
  1058. }
  1059. mutex_init(&twl->xfer_lock);
  1060. }
  1061. inuse = true;
  1062. /* setup clock framework */
  1063. clocks_init(&client->dev, pdata->clock);
  1064. /* read TWL IDCODE Register */
  1065. if (twl_id == TWL4030_CLASS_ID) {
  1066. status = twl_read_idcode_register();
  1067. WARN(status < 0, "Error: reading twl_idcode register value\n");
  1068. }
  1069. /* load power event scripts */
  1070. if (IS_ENABLED(CONFIG_TWL4030_POWER) && pdata->power)
  1071. twl4030_power_init(pdata->power);
  1072. /* Maybe init the T2 Interrupt subsystem */
  1073. if (client->irq) {
  1074. if (twl_class_is_4030()) {
  1075. twl4030_init_chip_irq(id->name);
  1076. irq_base = twl4030_init_irq(&client->dev, client->irq);
  1077. } else {
  1078. irq_base = twl6030_init_irq(&client->dev, client->irq);
  1079. }
  1080. if (irq_base < 0) {
  1081. status = irq_base;
  1082. goto fail;
  1083. }
  1084. }
  1085. /*
  1086. * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
  1087. * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
  1088. * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
  1089. */
  1090. if (twl_class_is_4030()) {
  1091. u8 temp;
  1092. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
  1093. temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
  1094. I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
  1095. twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
  1096. }
  1097. status = -ENODEV;
  1098. if (node)
  1099. status = of_platform_populate(node, NULL, NULL, &client->dev);
  1100. if (status)
  1101. status = add_children(pdata, irq_base, id->driver_data);
  1102. fail:
  1103. if (status < 0)
  1104. twl_remove(client);
  1105. return status;
  1106. }
  1107. static const struct i2c_device_id twl_ids[] = {
  1108. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  1109. { "twl5030", 0 }, /* T2 updated */
  1110. { "twl5031", TWL5031 }, /* TWL5030 updated */
  1111. { "tps65950", 0 }, /* catalog version of twl5030 */
  1112. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  1113. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  1114. { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
  1115. and vibrator. Charger in USB module*/
  1116. { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
  1117. { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
  1118. { /* end of list */ },
  1119. };
  1120. MODULE_DEVICE_TABLE(i2c, twl_ids);
  1121. /* One Client Driver , 4 Clients */
  1122. static struct i2c_driver twl_driver = {
  1123. .driver.name = DRIVER_NAME,
  1124. .id_table = twl_ids,
  1125. .probe = twl_probe,
  1126. .remove = twl_remove,
  1127. };
  1128. static int __init twl_init(void)
  1129. {
  1130. return i2c_add_driver(&twl_driver);
  1131. }
  1132. subsys_initcall(twl_init);
  1133. static void __exit twl_exit(void)
  1134. {
  1135. i2c_del_driver(&twl_driver);
  1136. }
  1137. module_exit(twl_exit);
  1138. MODULE_AUTHOR("Texas Instruments, Inc.");
  1139. MODULE_DESCRIPTION("I2C Core interface for TWL");
  1140. MODULE_LICENSE("GPL");