twl-core.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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/platform_device.h>
  33. #include <linux/clk.h>
  34. #include <linux/err.h>
  35. #include <linux/regulator/machine.h>
  36. #include <linux/i2c.h>
  37. #include <linux/i2c/twl.h>
  38. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  39. #include <plat/cpu.h>
  40. #endif
  41. /*
  42. * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
  43. * Management and System Companion Device" chips originally designed for
  44. * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
  45. * often at around 3 Mbit/sec, including for interrupt handling.
  46. *
  47. * This driver core provides genirq support for the interrupts emitted,
  48. * by the various modules, and exports register access primitives.
  49. *
  50. * FIXME this driver currently requires use of the first interrupt line
  51. * (and associated registers).
  52. */
  53. #define DRIVER_NAME "twl"
  54. #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
  55. #define twl_has_keypad() true
  56. #else
  57. #define twl_has_keypad() false
  58. #endif
  59. #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
  60. #define twl_has_gpio() true
  61. #else
  62. #define twl_has_gpio() false
  63. #endif
  64. #if defined(CONFIG_REGULATOR_TWL4030) \
  65. || defined(CONFIG_REGULATOR_TWL4030_MODULE)
  66. #define twl_has_regulator() true
  67. #else
  68. #define twl_has_regulator() false
  69. #endif
  70. #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
  71. #define twl_has_madc() true
  72. #else
  73. #define twl_has_madc() false
  74. #endif
  75. #ifdef CONFIG_TWL4030_POWER
  76. #define twl_has_power() true
  77. #else
  78. #define twl_has_power() false
  79. #endif
  80. #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
  81. #define twl_has_rtc() true
  82. #else
  83. #define twl_has_rtc() false
  84. #endif
  85. #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
  86. #define twl_has_usb() true
  87. #else
  88. #define twl_has_usb() false
  89. #endif
  90. #if defined(CONFIG_TWL4030_WATCHDOG) || \
  91. defined(CONFIG_TWL4030_WATCHDOG_MODULE)
  92. #define twl_has_watchdog() true
  93. #else
  94. #define twl_has_watchdog() false
  95. #endif
  96. #if defined(CONFIG_TWL4030_CODEC) || defined(CONFIG_TWL4030_CODEC_MODULE) ||\
  97. defined(CONFIG_SND_SOC_TWL6040) || defined(CONFIG_SND_SOC_TWL6040_MODULE)
  98. #define twl_has_codec() true
  99. #else
  100. #define twl_has_codec() false
  101. #endif
  102. /* Triton Core internal information (BEGIN) */
  103. /* Last - for index max*/
  104. #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
  105. #define TWL_NUM_SLAVES 4
  106. #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
  107. || defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
  108. #define twl_has_pwrbutton() true
  109. #else
  110. #define twl_has_pwrbutton() false
  111. #endif
  112. #define SUB_CHIP_ID0 0
  113. #define SUB_CHIP_ID1 1
  114. #define SUB_CHIP_ID2 2
  115. #define SUB_CHIP_ID3 3
  116. #define TWL_MODULE_LAST TWL4030_MODULE_LAST
  117. /* Base Address defns for twl4030_map[] */
  118. /* subchip/slave 0 - USB ID */
  119. #define TWL4030_BASEADD_USB 0x0000
  120. /* subchip/slave 1 - AUD ID */
  121. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  122. #define TWL4030_BASEADD_GPIO 0x0098
  123. #define TWL4030_BASEADD_INTBR 0x0085
  124. #define TWL4030_BASEADD_PIH 0x0080
  125. #define TWL4030_BASEADD_TEST 0x004C
  126. /* subchip/slave 2 - AUX ID */
  127. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  128. #define TWL4030_BASEADD_LED 0x00EE
  129. #define TWL4030_BASEADD_MADC 0x0000
  130. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  131. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  132. #define TWL4030_BASEADD_PWM0 0x00F8
  133. #define TWL4030_BASEADD_PWM1 0x00FB
  134. #define TWL4030_BASEADD_PWMA 0x00EF
  135. #define TWL4030_BASEADD_PWMB 0x00F1
  136. #define TWL4030_BASEADD_KEYPAD 0x00D2
  137. #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
  138. #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
  139. one */
  140. /* subchip/slave 3 - POWER ID */
  141. #define TWL4030_BASEADD_BACKUP 0x0014
  142. #define TWL4030_BASEADD_INT 0x002E
  143. #define TWL4030_BASEADD_PM_MASTER 0x0036
  144. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  145. #define TWL4030_BASEADD_RTC 0x001C
  146. #define TWL4030_BASEADD_SECURED_REG 0x0000
  147. /* Triton Core internal information (END) */
  148. /* subchip/slave 0 0x48 - POWER */
  149. #define TWL6030_BASEADD_RTC 0x0000
  150. #define TWL6030_BASEADD_MEM 0x0017
  151. #define TWL6030_BASEADD_PM_MASTER 0x001F
  152. #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
  153. #define TWL6030_BASEADD_PM_MISC 0x00E2
  154. #define TWL6030_BASEADD_PM_PUPD 0x00F0
  155. /* subchip/slave 1 0x49 - FEATURE */
  156. #define TWL6030_BASEADD_USB 0x0000
  157. #define TWL6030_BASEADD_GPADC_CTRL 0x002E
  158. #define TWL6030_BASEADD_AUX 0x0090
  159. #define TWL6030_BASEADD_PWM 0x00BA
  160. #define TWL6030_BASEADD_GASGAUGE 0x00C0
  161. #define TWL6030_BASEADD_PIH 0x00D0
  162. #define TWL6030_BASEADD_CHARGER 0x00E0
  163. /* subchip/slave 2 0x4A - DFT */
  164. #define TWL6030_BASEADD_DIEID 0x00C0
  165. /* subchip/slave 3 0x4B - AUDIO */
  166. #define TWL6030_BASEADD_AUDIO 0x0000
  167. #define TWL6030_BASEADD_RSV 0x0000
  168. #define TWL6030_BASEADD_ZERO 0x0000
  169. /* Few power values */
  170. #define R_CFG_BOOT 0x05
  171. /* some fields in R_CFG_BOOT */
  172. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  173. #define HFCLK_FREQ_26_MHZ (2 << 0)
  174. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  175. #define HIGH_PERF_SQ (1 << 3)
  176. #define CK32K_LOWPWR_EN (1 << 7)
  177. /* chip-specific feature flags, for i2c_device_id.driver_data */
  178. #define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
  179. #define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
  180. #define TWL5031 BIT(2) /* twl5031 has different registers */
  181. #define TWL6030_CLASS BIT(3) /* TWL6030 class */
  182. /*----------------------------------------------------------------------*/
  183. /* is driver active, bound to a chip? */
  184. static bool inuse;
  185. static unsigned int twl_id;
  186. unsigned int twl_rev(void)
  187. {
  188. return twl_id;
  189. }
  190. EXPORT_SYMBOL(twl_rev);
  191. /* Structure for each TWL4030/TWL6030 Slave */
  192. struct twl_client {
  193. struct i2c_client *client;
  194. u8 address;
  195. /* max numb of i2c_msg required is for read =2 */
  196. struct i2c_msg xfer_msg[2];
  197. /* To lock access to xfer_msg */
  198. struct mutex xfer_lock;
  199. };
  200. static struct twl_client twl_modules[TWL_NUM_SLAVES];
  201. /* mapping the module id to slave id and base address */
  202. struct twl_mapping {
  203. unsigned char sid; /* Slave ID */
  204. unsigned char base; /* base address */
  205. };
  206. struct twl_mapping *twl_map;
  207. static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  208. /*
  209. * NOTE: don't change this table without updating the
  210. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  211. * so they continue to match the order in this table.
  212. */
  213. { 0, TWL4030_BASEADD_USB },
  214. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  215. { 1, TWL4030_BASEADD_GPIO },
  216. { 1, TWL4030_BASEADD_INTBR },
  217. { 1, TWL4030_BASEADD_PIH },
  218. { 1, TWL4030_BASEADD_TEST },
  219. { 2, TWL4030_BASEADD_KEYPAD },
  220. { 2, TWL4030_BASEADD_MADC },
  221. { 2, TWL4030_BASEADD_INTERRUPTS },
  222. { 2, TWL4030_BASEADD_LED },
  223. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  224. { 2, TWL4030_BASEADD_PRECHARGE },
  225. { 2, TWL4030_BASEADD_PWM0 },
  226. { 2, TWL4030_BASEADD_PWM1 },
  227. { 2, TWL4030_BASEADD_PWMA },
  228. { 2, TWL4030_BASEADD_PWMB },
  229. { 2, TWL5031_BASEADD_ACCESSORY },
  230. { 2, TWL5031_BASEADD_INTERRUPTS },
  231. { 3, TWL4030_BASEADD_BACKUP },
  232. { 3, TWL4030_BASEADD_INT },
  233. { 3, TWL4030_BASEADD_PM_MASTER },
  234. { 3, TWL4030_BASEADD_PM_RECEIVER },
  235. { 3, TWL4030_BASEADD_RTC },
  236. { 3, TWL4030_BASEADD_SECURED_REG },
  237. };
  238. static struct twl_mapping twl6030_map[] = {
  239. /*
  240. * NOTE: don't change this table without updating the
  241. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  242. * so they continue to match the order in this table.
  243. */
  244. { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
  245. { SUB_CHIP_ID3, TWL6030_BASEADD_AUDIO },
  246. { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
  247. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  248. { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
  249. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  250. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  251. { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
  252. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  253. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  254. { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
  255. { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
  256. { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
  257. { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
  258. { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
  259. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  260. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  261. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  262. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  263. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  264. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
  265. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
  266. { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
  267. { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
  268. };
  269. /*----------------------------------------------------------------------*/
  270. /* Exported Functions */
  271. /**
  272. * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
  273. * @mod_no: module number
  274. * @value: an array of num_bytes+1 containing data to write
  275. * @reg: register address (just offset will do)
  276. * @num_bytes: number of bytes to transfer
  277. *
  278. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  279. * valid data starts at Offset 1.
  280. *
  281. * Returns the result of operation - 0 is success
  282. */
  283. int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  284. {
  285. int ret;
  286. int sid;
  287. struct twl_client *twl;
  288. struct i2c_msg *msg;
  289. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  290. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  291. return -EPERM;
  292. }
  293. sid = twl_map[mod_no].sid;
  294. twl = &twl_modules[sid];
  295. if (unlikely(!inuse)) {
  296. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  297. return -EPERM;
  298. }
  299. mutex_lock(&twl->xfer_lock);
  300. /*
  301. * [MSG1]: fill the register address data
  302. * fill the data Tx buffer
  303. */
  304. msg = &twl->xfer_msg[0];
  305. msg->addr = twl->address;
  306. msg->len = num_bytes + 1;
  307. msg->flags = 0;
  308. msg->buf = value;
  309. /* over write the first byte of buffer with the register address */
  310. *value = twl_map[mod_no].base + reg;
  311. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
  312. mutex_unlock(&twl->xfer_lock);
  313. /* i2c_transfer returns number of messages transferred */
  314. if (ret != 1) {
  315. pr_err("%s: i2c_write failed to transfer all messages\n",
  316. DRIVER_NAME);
  317. if (ret < 0)
  318. return ret;
  319. else
  320. return -EIO;
  321. } else {
  322. return 0;
  323. }
  324. }
  325. EXPORT_SYMBOL(twl_i2c_write);
  326. /**
  327. * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
  328. * @mod_no: module number
  329. * @value: an array of num_bytes containing data to be read
  330. * @reg: register address (just offset will do)
  331. * @num_bytes: number of bytes to transfer
  332. *
  333. * Returns result of operation - num_bytes is success else failure.
  334. */
  335. int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  336. {
  337. int ret;
  338. u8 val;
  339. int sid;
  340. struct twl_client *twl;
  341. struct i2c_msg *msg;
  342. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  343. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  344. return -EPERM;
  345. }
  346. sid = twl_map[mod_no].sid;
  347. twl = &twl_modules[sid];
  348. if (unlikely(!inuse)) {
  349. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  350. return -EPERM;
  351. }
  352. mutex_lock(&twl->xfer_lock);
  353. /* [MSG1] fill the register address data */
  354. msg = &twl->xfer_msg[0];
  355. msg->addr = twl->address;
  356. msg->len = 1;
  357. msg->flags = 0; /* Read the register value */
  358. val = twl_map[mod_no].base + reg;
  359. msg->buf = &val;
  360. /* [MSG2] fill the data rx buffer */
  361. msg = &twl->xfer_msg[1];
  362. msg->addr = twl->address;
  363. msg->flags = I2C_M_RD; /* Read the register value */
  364. msg->len = num_bytes; /* only n bytes */
  365. msg->buf = value;
  366. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  367. mutex_unlock(&twl->xfer_lock);
  368. /* i2c_transfer returns number of messages transferred */
  369. if (ret != 2) {
  370. pr_err("%s: i2c_read failed to transfer all messages\n",
  371. DRIVER_NAME);
  372. if (ret < 0)
  373. return ret;
  374. else
  375. return -EIO;
  376. } else {
  377. return 0;
  378. }
  379. }
  380. EXPORT_SYMBOL(twl_i2c_read);
  381. /**
  382. * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
  383. * @mod_no: module number
  384. * @value: the value to be written 8 bit
  385. * @reg: register address (just offset will do)
  386. *
  387. * Returns result of operation - 0 is success
  388. */
  389. int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  390. {
  391. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  392. u8 temp_buffer[2] = { 0 };
  393. /* offset 1 contains the data */
  394. temp_buffer[1] = value;
  395. return twl_i2c_write(mod_no, temp_buffer, reg, 1);
  396. }
  397. EXPORT_SYMBOL(twl_i2c_write_u8);
  398. /**
  399. * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
  400. * @mod_no: module number
  401. * @value: the value read 8 bit
  402. * @reg: register address (just offset will do)
  403. *
  404. * Returns result of operation - 0 is success
  405. */
  406. int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  407. {
  408. return twl_i2c_read(mod_no, value, reg, 1);
  409. }
  410. EXPORT_SYMBOL(twl_i2c_read_u8);
  411. /*----------------------------------------------------------------------*/
  412. static struct device *
  413. add_numbered_child(unsigned chip, const char *name, int num,
  414. void *pdata, unsigned pdata_len,
  415. bool can_wakeup, int irq0, int irq1)
  416. {
  417. struct platform_device *pdev;
  418. struct twl_client *twl = &twl_modules[chip];
  419. int status;
  420. pdev = platform_device_alloc(name, num);
  421. if (!pdev) {
  422. dev_dbg(&twl->client->dev, "can't alloc dev\n");
  423. status = -ENOMEM;
  424. goto err;
  425. }
  426. device_init_wakeup(&pdev->dev, can_wakeup);
  427. pdev->dev.parent = &twl->client->dev;
  428. if (pdata) {
  429. status = platform_device_add_data(pdev, pdata, pdata_len);
  430. if (status < 0) {
  431. dev_dbg(&pdev->dev, "can't add platform_data\n");
  432. goto err;
  433. }
  434. }
  435. if (irq0) {
  436. struct resource r[2] = {
  437. { .start = irq0, .flags = IORESOURCE_IRQ, },
  438. { .start = irq1, .flags = IORESOURCE_IRQ, },
  439. };
  440. status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
  441. if (status < 0) {
  442. dev_dbg(&pdev->dev, "can't add irqs\n");
  443. goto err;
  444. }
  445. }
  446. status = platform_device_add(pdev);
  447. err:
  448. if (status < 0) {
  449. platform_device_put(pdev);
  450. dev_err(&twl->client->dev, "can't add %s dev\n", name);
  451. return ERR_PTR(status);
  452. }
  453. return &pdev->dev;
  454. }
  455. static inline struct device *add_child(unsigned chip, const char *name,
  456. void *pdata, unsigned pdata_len,
  457. bool can_wakeup, int irq0, int irq1)
  458. {
  459. return add_numbered_child(chip, name, -1, pdata, pdata_len,
  460. can_wakeup, irq0, irq1);
  461. }
  462. static struct device *
  463. add_regulator_linked(int num, struct regulator_init_data *pdata,
  464. struct regulator_consumer_supply *consumers,
  465. unsigned num_consumers)
  466. {
  467. unsigned sub_chip_id;
  468. /* regulator framework demands init_data ... */
  469. if (!pdata)
  470. return NULL;
  471. if (consumers) {
  472. pdata->consumer_supplies = consumers;
  473. pdata->num_consumer_supplies = num_consumers;
  474. }
  475. /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
  476. sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
  477. return add_numbered_child(sub_chip_id, "twl_reg", num,
  478. pdata, sizeof(*pdata), false, 0, 0);
  479. }
  480. static struct device *
  481. add_regulator(int num, struct regulator_init_data *pdata)
  482. {
  483. return add_regulator_linked(num, pdata, NULL, 0);
  484. }
  485. /*
  486. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  487. * for the PIH, and the next are for the PWR_INT SIH, since
  488. * that's how twl_init_irq() sets things up.
  489. */
  490. static int
  491. add_children(struct twl4030_platform_data *pdata, unsigned long features)
  492. {
  493. struct device *child;
  494. unsigned sub_chip_id;
  495. if (twl_has_gpio() && pdata->gpio) {
  496. child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
  497. pdata->gpio, sizeof(*pdata->gpio),
  498. false, pdata->irq_base + GPIO_INTR_OFFSET, 0);
  499. if (IS_ERR(child))
  500. return PTR_ERR(child);
  501. }
  502. if (twl_has_keypad() && pdata->keypad) {
  503. child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
  504. pdata->keypad, sizeof(*pdata->keypad),
  505. true, pdata->irq_base + KEYPAD_INTR_OFFSET, 0);
  506. if (IS_ERR(child))
  507. return PTR_ERR(child);
  508. }
  509. if (twl_has_madc() && pdata->madc) {
  510. child = add_child(2, "twl4030_madc",
  511. pdata->madc, sizeof(*pdata->madc),
  512. true, pdata->irq_base + MADC_INTR_OFFSET, 0);
  513. if (IS_ERR(child))
  514. return PTR_ERR(child);
  515. }
  516. if (twl_has_rtc()) {
  517. /*
  518. * REVISIT platform_data here currently might expose the
  519. * "msecure" line ... but for now we just expect board
  520. * setup to tell the chip "it's always ok to SET_TIME".
  521. * Eventually, Linux might become more aware of such
  522. * HW security concerns, and "least privilege".
  523. */
  524. sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
  525. child = add_child(sub_chip_id, "twl_rtc",
  526. NULL, 0,
  527. true, pdata->irq_base + RTC_INTR_OFFSET, 0);
  528. if (IS_ERR(child))
  529. return PTR_ERR(child);
  530. }
  531. if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
  532. static struct regulator_consumer_supply usb1v5 = {
  533. .supply = "usb1v5",
  534. };
  535. static struct regulator_consumer_supply usb1v8 = {
  536. .supply = "usb1v8",
  537. };
  538. static struct regulator_consumer_supply usb3v1 = {
  539. .supply = "usb3v1",
  540. };
  541. /* First add the regulators so that they can be used by transceiver */
  542. if (twl_has_regulator()) {
  543. /* this is a template that gets copied */
  544. struct regulator_init_data usb_fixed = {
  545. .constraints.valid_modes_mask =
  546. REGULATOR_MODE_NORMAL
  547. | REGULATOR_MODE_STANDBY,
  548. .constraints.valid_ops_mask =
  549. REGULATOR_CHANGE_MODE
  550. | REGULATOR_CHANGE_STATUS,
  551. };
  552. child = add_regulator_linked(TWL4030_REG_VUSB1V5,
  553. &usb_fixed, &usb1v5, 1);
  554. if (IS_ERR(child))
  555. return PTR_ERR(child);
  556. child = add_regulator_linked(TWL4030_REG_VUSB1V8,
  557. &usb_fixed, &usb1v8, 1);
  558. if (IS_ERR(child))
  559. return PTR_ERR(child);
  560. child = add_regulator_linked(TWL4030_REG_VUSB3V1,
  561. &usb_fixed, &usb3v1, 1);
  562. if (IS_ERR(child))
  563. return PTR_ERR(child);
  564. }
  565. child = add_child(0, "twl4030_usb",
  566. pdata->usb, sizeof(*pdata->usb),
  567. true,
  568. /* irq0 = USB_PRES, irq1 = USB */
  569. pdata->irq_base + USB_PRES_INTR_OFFSET,
  570. pdata->irq_base + USB_INTR_OFFSET);
  571. if (IS_ERR(child))
  572. return PTR_ERR(child);
  573. /* we need to connect regulators to this transceiver */
  574. if (twl_has_regulator() && child) {
  575. usb1v5.dev = child;
  576. usb1v8.dev = child;
  577. usb3v1.dev = child;
  578. }
  579. }
  580. if (twl_has_watchdog()) {
  581. child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
  582. if (IS_ERR(child))
  583. return PTR_ERR(child);
  584. }
  585. if (twl_has_pwrbutton()) {
  586. child = add_child(1, "twl4030_pwrbutton",
  587. NULL, 0, true, pdata->irq_base + 8 + 0, 0);
  588. if (IS_ERR(child))
  589. return PTR_ERR(child);
  590. }
  591. if (twl_has_codec() && pdata->codec && twl_class_is_4030()) {
  592. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  593. child = add_child(sub_chip_id, "twl4030-audio",
  594. pdata->codec, sizeof(*pdata->codec),
  595. false, 0, 0);
  596. if (IS_ERR(child))
  597. return PTR_ERR(child);
  598. }
  599. /* Phoenix codec driver is probed directly atm */
  600. if (twl_has_codec() && pdata->codec && twl_class_is_6030()) {
  601. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  602. child = add_child(sub_chip_id, "twl6040-codec",
  603. pdata->codec, sizeof(*pdata->codec),
  604. false, 0, 0);
  605. if (IS_ERR(child))
  606. return PTR_ERR(child);
  607. }
  608. /* twl4030 regulators */
  609. if (twl_has_regulator() && twl_class_is_4030()) {
  610. child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
  611. if (IS_ERR(child))
  612. return PTR_ERR(child);
  613. child = add_regulator(TWL4030_REG_VIO, pdata->vio);
  614. if (IS_ERR(child))
  615. return PTR_ERR(child);
  616. child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1);
  617. if (IS_ERR(child))
  618. return PTR_ERR(child);
  619. child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2);
  620. if (IS_ERR(child))
  621. return PTR_ERR(child);
  622. child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
  623. if (IS_ERR(child))
  624. return PTR_ERR(child);
  625. child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
  626. if (IS_ERR(child))
  627. return PTR_ERR(child);
  628. child = add_regulator((features & TWL4030_VAUX2)
  629. ? TWL4030_REG_VAUX2_4030
  630. : TWL4030_REG_VAUX2,
  631. pdata->vaux2);
  632. if (IS_ERR(child))
  633. return PTR_ERR(child);
  634. child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1);
  635. if (IS_ERR(child))
  636. return PTR_ERR(child);
  637. child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2);
  638. if (IS_ERR(child))
  639. return PTR_ERR(child);
  640. child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig);
  641. if (IS_ERR(child))
  642. return PTR_ERR(child);
  643. }
  644. /* maybe add LDOs that are omitted on cost-reduced parts */
  645. if (twl_has_regulator() && !(features & TPS_SUBSET)
  646. && twl_class_is_4030()) {
  647. child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
  648. if (IS_ERR(child))
  649. return PTR_ERR(child);
  650. child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
  651. if (IS_ERR(child))
  652. return PTR_ERR(child);
  653. child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
  654. if (IS_ERR(child))
  655. return PTR_ERR(child);
  656. child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
  657. if (IS_ERR(child))
  658. return PTR_ERR(child);
  659. child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
  660. if (IS_ERR(child))
  661. return PTR_ERR(child);
  662. child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
  663. if (IS_ERR(child))
  664. return PTR_ERR(child);
  665. }
  666. /* twl6030 regulators */
  667. if (twl_has_regulator() && twl_class_is_6030()) {
  668. child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc);
  669. if (IS_ERR(child))
  670. return PTR_ERR(child);
  671. child = add_regulator(TWL6030_REG_VPP, pdata->vpp);
  672. if (IS_ERR(child))
  673. return PTR_ERR(child);
  674. child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim);
  675. if (IS_ERR(child))
  676. return PTR_ERR(child);
  677. child = add_regulator(TWL6030_REG_VANA, pdata->vana);
  678. if (IS_ERR(child))
  679. return PTR_ERR(child);
  680. child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio);
  681. if (IS_ERR(child))
  682. return PTR_ERR(child);
  683. child = add_regulator(TWL6030_REG_VDAC, pdata->vdac);
  684. if (IS_ERR(child))
  685. return PTR_ERR(child);
  686. child = add_regulator(TWL6030_REG_VUSB, pdata->vusb);
  687. if (IS_ERR(child))
  688. return PTR_ERR(child);
  689. child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1);
  690. if (IS_ERR(child))
  691. return PTR_ERR(child);
  692. child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2);
  693. if (IS_ERR(child))
  694. return PTR_ERR(child);
  695. child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3);
  696. if (IS_ERR(child))
  697. return PTR_ERR(child);
  698. }
  699. return 0;
  700. }
  701. /*----------------------------------------------------------------------*/
  702. /*
  703. * These three functions initialize the on-chip clock framework,
  704. * letting it generate the right frequencies for USB, MADC, and
  705. * other purposes.
  706. */
  707. static inline int __init protect_pm_master(void)
  708. {
  709. int e = 0;
  710. e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
  711. TWL4030_PM_MASTER_PROTECT_KEY);
  712. return e;
  713. }
  714. static inline int __init unprotect_pm_master(void)
  715. {
  716. int e = 0;
  717. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  718. TWL4030_PM_MASTER_KEY_CFG1,
  719. TWL4030_PM_MASTER_PROTECT_KEY);
  720. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  721. TWL4030_PM_MASTER_KEY_CFG2,
  722. TWL4030_PM_MASTER_PROTECT_KEY);
  723. return e;
  724. }
  725. static void clocks_init(struct device *dev,
  726. struct twl4030_clock_init_data *clock)
  727. {
  728. int e = 0;
  729. struct clk *osc;
  730. u32 rate;
  731. u8 ctrl = HFCLK_FREQ_26_MHZ;
  732. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  733. if (cpu_is_omap2430())
  734. osc = clk_get(dev, "osc_ck");
  735. else
  736. osc = clk_get(dev, "osc_sys_ck");
  737. if (IS_ERR(osc)) {
  738. printk(KERN_WARNING "Skipping twl internal clock init and "
  739. "using bootloader value (unknown osc rate)\n");
  740. return;
  741. }
  742. rate = clk_get_rate(osc);
  743. clk_put(osc);
  744. #else
  745. /* REVISIT for non-OMAP systems, pass the clock rate from
  746. * board init code, using platform_data.
  747. */
  748. osc = ERR_PTR(-EIO);
  749. printk(KERN_WARNING "Skipping twl internal clock init and "
  750. "using bootloader value (unknown osc rate)\n");
  751. return;
  752. #endif
  753. switch (rate) {
  754. case 19200000:
  755. ctrl = HFCLK_FREQ_19p2_MHZ;
  756. break;
  757. case 26000000:
  758. ctrl = HFCLK_FREQ_26_MHZ;
  759. break;
  760. case 38400000:
  761. ctrl = HFCLK_FREQ_38p4_MHZ;
  762. break;
  763. }
  764. ctrl |= HIGH_PERF_SQ;
  765. if (clock && clock->ck32k_lowpwr_enable)
  766. ctrl |= CK32K_LOWPWR_EN;
  767. e |= unprotect_pm_master();
  768. /* effect->MADC+USB ck en */
  769. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  770. e |= protect_pm_master();
  771. if (e < 0)
  772. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  773. }
  774. /*----------------------------------------------------------------------*/
  775. int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  776. int twl4030_exit_irq(void);
  777. int twl4030_init_chip_irq(const char *chip);
  778. int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  779. int twl6030_exit_irq(void);
  780. static int twl_remove(struct i2c_client *client)
  781. {
  782. unsigned i;
  783. int status;
  784. if (twl_class_is_4030())
  785. status = twl4030_exit_irq();
  786. else
  787. status = twl6030_exit_irq();
  788. if (status < 0)
  789. return status;
  790. for (i = 0; i < TWL_NUM_SLAVES; i++) {
  791. struct twl_client *twl = &twl_modules[i];
  792. if (twl->client && twl->client != client)
  793. i2c_unregister_device(twl->client);
  794. twl_modules[i].client = NULL;
  795. }
  796. inuse = false;
  797. return 0;
  798. }
  799. /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
  800. static int __init
  801. twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
  802. {
  803. int status;
  804. unsigned i;
  805. struct twl4030_platform_data *pdata = client->dev.platform_data;
  806. u8 temp;
  807. if (!pdata) {
  808. dev_dbg(&client->dev, "no platform data?\n");
  809. return -EINVAL;
  810. }
  811. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  812. dev_dbg(&client->dev, "can't talk I2C?\n");
  813. return -EIO;
  814. }
  815. if (inuse) {
  816. dev_dbg(&client->dev, "driver is already in use\n");
  817. return -EBUSY;
  818. }
  819. for (i = 0; i < TWL_NUM_SLAVES; i++) {
  820. struct twl_client *twl = &twl_modules[i];
  821. twl->address = client->addr + i;
  822. if (i == 0)
  823. twl->client = client;
  824. else {
  825. twl->client = i2c_new_dummy(client->adapter,
  826. twl->address);
  827. if (!twl->client) {
  828. dev_err(&client->dev,
  829. "can't attach client %d\n", i);
  830. status = -ENOMEM;
  831. goto fail;
  832. }
  833. }
  834. mutex_init(&twl->xfer_lock);
  835. }
  836. inuse = true;
  837. if ((id->driver_data) & TWL6030_CLASS) {
  838. twl_id = TWL6030_CLASS_ID;
  839. twl_map = &twl6030_map[0];
  840. } else {
  841. twl_id = TWL4030_CLASS_ID;
  842. twl_map = &twl4030_map[0];
  843. }
  844. /* setup clock framework */
  845. clocks_init(&client->dev, pdata->clock);
  846. /* load power event scripts */
  847. if (twl_has_power() && pdata->power)
  848. twl4030_power_init(pdata->power);
  849. /* Maybe init the T2 Interrupt subsystem */
  850. if (client->irq
  851. && pdata->irq_base
  852. && pdata->irq_end > pdata->irq_base) {
  853. if (twl_class_is_4030()) {
  854. twl4030_init_chip_irq(id->name);
  855. status = twl4030_init_irq(client->irq, pdata->irq_base,
  856. pdata->irq_end);
  857. } else {
  858. status = twl6030_init_irq(client->irq, pdata->irq_base,
  859. pdata->irq_end);
  860. }
  861. if (status < 0)
  862. goto fail;
  863. }
  864. /* Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
  865. * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
  866. * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
  867. */
  868. if (twl_class_is_4030()) {
  869. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
  870. temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
  871. I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
  872. twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
  873. }
  874. status = add_children(pdata, id->driver_data);
  875. fail:
  876. if (status < 0)
  877. twl_remove(client);
  878. return status;
  879. }
  880. static const struct i2c_device_id twl_ids[] = {
  881. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  882. { "twl5030", 0 }, /* T2 updated */
  883. { "twl5031", TWL5031 }, /* TWL5030 updated */
  884. { "tps65950", 0 }, /* catalog version of twl5030 */
  885. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  886. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  887. { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
  888. { /* end of list */ },
  889. };
  890. MODULE_DEVICE_TABLE(i2c, twl_ids);
  891. /* One Client Driver , 4 Clients */
  892. static struct i2c_driver twl_driver = {
  893. .driver.name = DRIVER_NAME,
  894. .id_table = twl_ids,
  895. .probe = twl_probe,
  896. .remove = twl_remove,
  897. };
  898. static int __init twl_init(void)
  899. {
  900. return i2c_add_driver(&twl_driver);
  901. }
  902. subsys_initcall(twl_init);
  903. static void __exit twl_exit(void)
  904. {
  905. i2c_del_driver(&twl_driver);
  906. }
  907. module_exit(twl_exit);
  908. MODULE_AUTHOR("Texas Instruments, Inc.");
  909. MODULE_DESCRIPTION("I2C Core interface for TWL");
  910. MODULE_LICENSE("GPL");