rt8973.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /* drivers/mfd/rt8973.c
  2. * Richtek RT8973 Multifunction Device / MUIC Driver
  3. *
  4. * Copyright (C) 2013
  5. * Author: Patrick Chang <patrick_chang@richtek.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/rtdefs.h>
  14. #include <linux/module.h>
  15. #include <linux/version.h>
  16. #include <linux/slab.h>
  17. #include <linux/gpio.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/i2c.h>
  20. #include <linux/wakelock.h>
  21. #include <linux/delay.h>
  22. #include <linux/power_supply.h>
  23. #include <linux/platform_data/rt8973.h>
  24. #ifdef SAMSUNG_MVRL_MUIC_RT8973
  25. #include <linux/gpio-pxa.h>
  26. #include <linux/platform_data/mv_usb.h>
  27. #include <mach/gpio-edge.h>
  28. #endif
  29. #if defined (CONFIG_OF)
  30. #include <linux/of_device.h>
  31. #include <linux/of_gpio.h>
  32. #endif
  33. #ifdef CONFIG_USB_HOST_NOTIFY
  34. #include <linux/host_notify.h>
  35. #endif
  36. #if defined(CONFIG_MACH_VICTORLTE_CTC)
  37. /* spmi control */
  38. extern int spmi_ext_register_writel_extra(u8 sid, u16 ad, u8 *buf, int len);
  39. extern int spmi_ext_register_readl_extra(u8 sid, u16 ad, u8 *buf, int len);
  40. #endif
  41. //////////////////////////////////////////////////////////////////////
  42. int rt_uart_connecting;
  43. EXPORT_SYMBOL(rt_uart_connecting);
  44. static int jig_state;
  45. int rt_check_jig_state(void)
  46. {
  47. return jig_state;
  48. }
  49. EXPORT_SYMBOL(rt_check_jig_state);
  50. //////////////////////////////////////////////////////////////////////
  51. #define RT8973_DEVICE_NAME "rt8973"
  52. #define ALIAS_NAME RT8973_DEVICE_NAME
  53. #define RT8973_DRV_VER "2.0.8SEC_Q"
  54. #define RT8973_IRQF_MODE IRQF_TRIGGER_FALLING
  55. #define RT8973_REG_CHIP_ID 0x01
  56. #define RT8973_REG_CONTROL 0x02
  57. #define RT8973_REG_INT_FLAG1 0x03
  58. #define RT8973_REG_INT_FLAG2 0x04
  59. #define RT8973_REG_INTERRUPT_MASK1 0x05
  60. #define RT8973_REG_INTERRUPT_MASK2 0x06
  61. #define RT8973_REG_ADC 0x07
  62. #define RT8973_REG_DEVICE1 0x0A
  63. #define RT8973_REG_DEVICE2 0x0B
  64. #define RT8973_REG_MANUAL_SW1 0x13
  65. #define RT8973_REG_MANUAL_SW2 0x14
  66. #define RT8973_REG_RESET 0x1B
  67. #define DCD_T_RETRY 2
  68. #define RT8973_DEVICE1_OTG 0x01
  69. #define RT8973_DEVICE1_SDP 0x04
  70. #define RT8973_DEVICE1_UART 0x08
  71. #define RT8973_DEVICE1_CDPORT 0x20
  72. #define RT8973_DEVICE1_DCPORT 0x40
  73. extern unsigned int system_rev;
  74. #ifdef SAMSUNG_MVRL_MUIC_RT8973
  75. static struct gpio_edge_desc muic_int_gpio;
  76. #endif
  77. struct device_desc {
  78. char *name;
  79. uint32_t reg_val;
  80. int cable_type;
  81. };
  82. static const struct device_desc device_to_cable_type_mapping[] = {
  83. {
  84. .name = "OTG",
  85. .reg_val = RT8973_DEVICE1_OTG,
  86. .cable_type = MUIC_RT8973_CABLE_TYPE_OTG,
  87. },
  88. {
  89. .name = "USB SDP",
  90. .reg_val = RT8973_DEVICE1_SDP,
  91. .cable_type = MUIC_RT8973_CABLE_TYPE_USB,
  92. },
  93. {
  94. .name = "UART",
  95. .reg_val = RT8973_DEVICE1_UART,
  96. .cable_type = MUIC_RT8973_CABLE_TYPE_UART,
  97. },
  98. {
  99. .name = "USB CDP",
  100. .reg_val = RT8973_DEVICE1_CDPORT,
  101. .cable_type = MUIC_RT8973_CABLE_TYPE_CDP,
  102. },
  103. {
  104. .name = "USB DCP",
  105. .reg_val = RT8973_DEVICE1_DCPORT,
  106. .cable_type = MUIC_RT8973_CABLE_TYPE_REGULAR_TA,
  107. },
  108. };
  109. struct id_desc {
  110. char *name;
  111. int cable_type_with_vbus;
  112. int cable_type_without_vbus;
  113. };
  114. static const struct id_desc id_to_cable_type_mapping[] = {
  115. {
  116. /* 00000, 0 */
  117. .name = "OTG",
  118. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_OTG,
  119. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_OTG,
  120. },
  121. { /* 00001, 1 */
  122. .name = "AT&T TA/Unknown",
  123. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  124. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  125. },
  126. { /* 00010, 2 */
  127. .name = "AT&T TA/Unknown",
  128. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  129. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  130. },
  131. { /* 00011, 3 */
  132. .name = "AT&T TA/Unknown",
  133. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  134. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  135. },
  136. { /* 00100, 4 */
  137. .name = "AT&T TA/Unknown",
  138. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  139. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  140. },
  141. { /* 00101, 5 */
  142. .name = "AT&T TA/Unknown",
  143. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  144. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  145. },
  146. { /* 00110, 6 */
  147. .name = "AT&T TA/Unknown",
  148. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  149. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  150. },
  151. { /* 00111, 7 */
  152. .name = "AT&T TA/Unknown",
  153. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  154. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  155. },
  156. { /* 01000, 8 */
  157. .name = "AT&T TA/Unknown",
  158. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  159. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  160. },
  161. { /* 01001, 9 */
  162. .name = "AT&T TA/Unknown",
  163. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  164. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  165. },
  166. { /* 01010, 10 */
  167. .name = "AT&T TA/Unknown",
  168. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  169. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  170. },
  171. { /* 01011, 11 */
  172. .name = "AT&T TA/Unknown",
  173. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  174. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  175. },
  176. { /* 01100, 12 */
  177. .name = "AT&T TA/Unknown",
  178. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  179. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  180. },
  181. { /* 01101, 13 */
  182. .name = "AT&T TA/Unknown",
  183. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  184. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  185. },
  186. { /* 01110, 14 */
  187. .name = "AT&T TA/Unknown",
  188. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  189. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  190. },
  191. { /* 01111, 15 */
  192. .name = "AT&T TA/Unknown",
  193. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  194. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  195. },
  196. { /* 10000, 16 */
  197. .name = "AT&T TA/Unknown",
  198. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  199. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  200. },
  201. { /* 10001, 17 */
  202. .name = "AT&T TA/Unknown",
  203. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  204. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  205. },
  206. { /* 10010, 18 */
  207. .name = "AT&T TA/Unknown",
  208. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  209. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  210. },
  211. { /* 10011, 19 */
  212. .name = "AT&T TA/Unknown",
  213. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  214. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  215. },
  216. { /* 10100, 20 */
  217. .name = "AT&T TA/Unknown",
  218. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  219. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  220. },
  221. { /* 10101, 21 */
  222. .name = "ADC0x15 Charger/Unknown",
  223. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_0x15,
  224. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  225. },
  226. { /* 10110, 22 */
  227. .name = "AT&T TA/Unknown",
  228. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  229. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  230. },
  231. { /* 10111, 23 */
  232. .name = "Type 1 Charger/Unknown",
  233. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_TYPE1_CHARGER,
  234. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  235. },
  236. { /* 11000, 24 */
  237. .name = "FM BOOT OFF USB/Unknown",
  238. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_JIG_USB_OFF,
  239. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  240. },
  241. { /* 11001, 25 */
  242. .name = "FM BOOT ON USB/Unknown",
  243. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_JIG_USB_ON,
  244. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  245. },
  246. { /* 11010, 26 */
  247. .name = "AT&T TA/Unknown",
  248. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  249. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  250. },
  251. { /* 11011, 27 */
  252. .name = "AT&T TA/Unknown",
  253. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  254. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  255. },
  256. { /* 11100, 28 */
  257. .name = "JIG UART BOOT OFF",
  258. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF_WITH_VBUS,
  259. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF,
  260. },
  261. { /* 11101, 29 */
  262. .name = "JIG UART BOOT ON",
  263. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_JIG_UART_ON_WITH_VBUS,
  264. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_JIG_UART_ON,
  265. },
  266. { /* 11110, 30 */
  267. .name = "AT&T TA/Unknown",
  268. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  269. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_UNKNOWN,
  270. },
  271. { /* 11111, 31 */
  272. .name = "AT&T TA/No cable",
  273. .cable_type_with_vbus = MUIC_RT8973_CABLE_TYPE_ATT_TA,
  274. .cable_type_without_vbus = MUIC_RT8973_CABLE_TYPE_NONE,
  275. },
  276. };
  277. enum {
  278. VBUS_SHIFT = 0,
  279. ACCESSORY_SHIFT,
  280. OCP_SHIFT,
  281. OVP_SHIFT,
  282. OTP_SHIFT,
  283. ADC_CHG_SHIFT,
  284. CABLE_CHG_SHIFT,
  285. OTG_SHIFT,
  286. DCDT_SHIFT,
  287. USB_SHIFT,
  288. UART_SHIFT,
  289. JIG_SHIFT,
  290. L200K_USB_SHIFT,
  291. };
  292. struct rt8973_status {
  293. int cable_type;
  294. int id_adc;
  295. uint8_t irq_flags[2];
  296. uint8_t device_reg[2];
  297. /* Processed useful status
  298. * Compare previous and current regs
  299. * to get this information */
  300. union {
  301. struct {
  302. uint32_t vbus_status:1;
  303. uint32_t accessory_status:1;
  304. uint32_t ocp_status:1;
  305. uint32_t ovp_status:1;
  306. uint32_t otp_status:1;
  307. uint32_t adc_chg_status:1;
  308. uint32_t cable_chg_status:1;
  309. uint32_t otg_status:1;
  310. uint32_t dcdt_status:1;
  311. uint32_t usb_connect:1;
  312. uint32_t uart_connect:1;
  313. uint32_t jig_connect:1;
  314. uint32_t l200k_usb_connect:1;
  315. };
  316. uint32_t status;
  317. };
  318. };
  319. typedef struct rt8973_chip {
  320. struct i2c_client *iic;
  321. struct mutex io_lock;
  322. struct rt8973_platform_data *pdata;
  323. struct device *dev;
  324. struct device *switch_dev;
  325. struct workqueue_struct *wq;
  326. struct delayed_work irq_work;
  327. struct delayed_work init_work;
  328. struct wake_lock muic_wake_lock;
  329. struct rt8973_status prev_status;
  330. struct rt8973_status curr_status;
  331. int dcdt_retry_count;
  332. int irq;
  333. int adc_reg_addr;
  334. } rt8973_chip_t;
  335. static struct rt8973_status *current_status;
  336. static int32_t rt8973_read(struct rt8973_chip *chip, uint8_t reg,
  337. uint8_t nbytes, uint8_t *buff)
  338. {
  339. int ret;
  340. mutex_lock(&chip->io_lock);
  341. ret = i2c_smbus_read_i2c_block_data(chip->iic, reg, nbytes, buff);
  342. mutex_unlock(&chip->io_lock);
  343. return ret;
  344. }
  345. static int32_t rt8973_reg_read(struct rt8973_chip *chip, int reg)
  346. {
  347. int ret;
  348. mutex_lock(&chip->io_lock);
  349. ret = i2c_smbus_read_byte_data(chip->iic, reg);
  350. mutex_unlock(&chip->io_lock);
  351. return ret;
  352. }
  353. int32_t rt8973_reg_write(struct rt8973_chip *chip, int reg, unsigned char data)
  354. {
  355. int ret;
  356. mutex_lock(&chip->io_lock);
  357. ret = i2c_smbus_write_byte_data(chip->iic, reg, data);
  358. mutex_unlock(&chip->io_lock);
  359. return ret;
  360. }
  361. static int32_t rt8973_assign_bits(struct rt8973_chip *chip, int reg,
  362. unsigned char mask, unsigned char data)
  363. {
  364. struct i2c_client *iic;
  365. int ret;
  366. iic = chip->iic;
  367. mutex_lock(&chip->io_lock);
  368. ret = i2c_smbus_read_byte_data(iic, reg);
  369. if (ret < 0)
  370. goto out;
  371. ret &= ~mask;
  372. ret |= data;
  373. ret = i2c_smbus_write_byte_data(iic, reg, ret);
  374. out:
  375. mutex_unlock(&chip->io_lock);
  376. return ret;
  377. }
  378. int32_t rt8973_set_bits(struct rt8973_chip *chip, int reg, unsigned char mask)
  379. {
  380. return rt8973_assign_bits(chip, reg, mask, mask);
  381. }
  382. int32_t rt8973_clr_bits(struct rt8973_chip *chip, int reg, unsigned char mask)
  383. {
  384. return rt8973_assign_bits(chip, reg, mask, 0);
  385. }
  386. static inline int rt8973_update_regs(rt8973_chip_t *chip)
  387. {
  388. int ret;
  389. ret = rt8973_read(chip, RT8973_REG_INT_FLAG1,
  390. 2, chip->curr_status.irq_flags);
  391. if (ret < 0)
  392. return ret;
  393. ret = rt8973_read(chip, RT8973_REG_DEVICE1,
  394. 2, chip->curr_status.device_reg);
  395. if (ret < 0)
  396. return ret;
  397. ret = rt8973_reg_read(chip, chip->adc_reg_addr);
  398. if (ret < 0)
  399. return ret;
  400. chip->curr_status.id_adc = ret;
  401. /* invalid id value */
  402. if (ret >= ARRAY_SIZE(id_to_cable_type_mapping))
  403. return -EINVAL;
  404. return 0;
  405. }
  406. static int rt8973_get_cable_type_by_device_reg(rt8973_chip_t *chip)
  407. {
  408. uint32_t device_reg = chip->curr_status.device_reg[1];
  409. int i;
  410. pr_info("RT8973 : Device = 0x%x, 0x%x\n",
  411. (int)chip->curr_status.device_reg[0],
  412. (int)chip->curr_status.device_reg[1]);
  413. device_reg <<= 8;
  414. device_reg |= chip->curr_status.device_reg[0];
  415. for (i = 0; i < ARRAY_SIZE(device_to_cable_type_mapping); i++) {
  416. if (device_to_cable_type_mapping[i].reg_val == device_reg) {
  417. if (chip->curr_status.dcdt_status == 1) {
  418. /* USB ID ==> open, i.e., ADC reading = 0x1f */
  419. return id_to_cable_type_mapping[0x1f].cable_type_with_vbus;
  420. }
  421. return device_to_cable_type_mapping[i].cable_type;
  422. }
  423. }
  424. /* not found */
  425. return -EINVAL;
  426. }
  427. static int rt8973_get_cable_type_by_id(rt8973_chip_t *chip)
  428. {
  429. int id_val = chip->curr_status.id_adc;
  430. int cable_type;
  431. pr_info("RT8973 : ID value = 0x%x\n", id_val);
  432. if (chip->curr_status.vbus_status)
  433. cable_type = id_to_cable_type_mapping[id_val].cable_type_with_vbus;
  434. else
  435. cable_type = id_to_cable_type_mapping[id_val].cable_type_without_vbus;
  436. /* Special case for L ID200K USB cable */
  437. if (cable_type == MUIC_RT8973_CABLE_TYPE_TYPE1_CHARGER) {
  438. /* ID = 200KOhm, VBUS = 1, DCD_T = 0 and CHGDET = 0 ==> L SDP*/
  439. if ((chip->curr_status.irq_flags[0]&0x0C) == 0)
  440. cable_type = MUIC_RT8973_CABLE_TYPE_L200K_SPEC_USB;
  441. }
  442. return cable_type;
  443. }
  444. static int rt8973_get_cable_type(rt8973_chip_t *chip)
  445. {
  446. int ret;
  447. /* Check dangerous case first and it can make system stop charging */
  448. if (chip->curr_status.ovp_status | chip->curr_status.ocp_status)
  449. return MUIC_RT8973_CABLE_TYPE_UNKNOWN;
  450. ret = rt8973_get_cable_type_by_device_reg(chip);
  451. if (ret >= 0)
  452. return ret;
  453. else
  454. return rt8973_get_cable_type_by_id(chip);
  455. }
  456. static void rt8973_preprocess_status(rt8973_chip_t *chip)
  457. {
  458. chip->curr_status.ocp_status =
  459. (chip->curr_status.irq_flags[1] & 0x60) ? 1 : 0;
  460. chip->curr_status.ovp_status =
  461. ((chip->curr_status.irq_flags[1] & 0x10) ||
  462. (chip->curr_status.irq_flags[0] & 0x10)) ? 1 : 0;
  463. chip->curr_status.otp_status =
  464. ((chip->curr_status.irq_flags[1] & 0x08) ||
  465. (chip->curr_status.irq_flags[0] & 0x80)) ? 1 : 0;
  466. chip->curr_status.dcdt_status =
  467. (chip->curr_status.irq_flags[0] & 0x08) ? 1 : 0;
  468. chip->curr_status.vbus_status =
  469. (chip->curr_status.irq_flags[1] & 0x02) ? 0 : 1;
  470. chip->curr_status.cable_type = rt8973_get_cable_type(chip);
  471. chip->curr_status.adc_chg_status =
  472. (chip->prev_status.id_adc != chip->curr_status.id_adc) ? 1 : 0;
  473. chip->curr_status.otg_status =
  474. (chip->curr_status.cable_type ==
  475. MUIC_RT8973_CABLE_TYPE_OTG) ? 1 : 0;
  476. chip->curr_status.accessory_status =
  477. ((chip->curr_status.cable_type !=
  478. MUIC_RT8973_CABLE_TYPE_NONE) &&
  479. (chip->curr_status.cable_type !=
  480. MUIC_RT8973_CABLE_TYPE_UNKNOWN)) ? 1 : 0;
  481. chip->curr_status.cable_chg_status =
  482. (chip->curr_status.cable_type !=
  483. chip->prev_status.cable_type) ? 1 : 0;
  484. chip->curr_status.usb_connect =
  485. ((chip->curr_status.cable_type ==
  486. MUIC_RT8973_CABLE_TYPE_USB) ||
  487. (chip->curr_status.cable_type ==
  488. MUIC_RT8973_CABLE_TYPE_CDP) ||
  489. (chip->curr_status.cable_type ==
  490. MUIC_RT8973_CABLE_TYPE_JIG_USB_OFF) ||
  491. (chip->curr_status.cable_type ==
  492. MUIC_RT8973_CABLE_TYPE_L200K_SPEC_USB) ||
  493. (chip->curr_status.cable_type ==
  494. MUIC_RT8973_CABLE_TYPE_JIG_USB_ON)) ? 1 : 0;
  495. chip->curr_status.uart_connect =
  496. ((chip->curr_status.cable_type ==
  497. MUIC_RT8973_CABLE_TYPE_UART) ||
  498. (chip->curr_status.cable_type ==
  499. MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF) ||
  500. (chip->curr_status.cable_type ==
  501. MUIC_RT8973_CABLE_TYPE_JIG_UART_ON) ||
  502. (chip->curr_status.cable_type ==
  503. MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF_WITH_VBUS) ||
  504. (chip->curr_status.cable_type ==
  505. MUIC_RT8973_CABLE_TYPE_JIG_UART_ON_WITH_VBUS)) ? 1 : 0;
  506. chip->curr_status.jig_connect =
  507. ((chip->curr_status.cable_type ==
  508. MUIC_RT8973_CABLE_TYPE_JIG_USB_OFF) ||
  509. (chip->curr_status.cable_type ==
  510. MUIC_RT8973_CABLE_TYPE_JIG_USB_ON) ||
  511. (chip->curr_status.cable_type ==
  512. MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF) ||
  513. (chip->curr_status.cable_type ==
  514. MUIC_RT8973_CABLE_TYPE_JIG_UART_ON) ||
  515. (chip->curr_status.cable_type ==
  516. MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF_WITH_VBUS) ||
  517. (chip->curr_status.cable_type ==
  518. MUIC_RT8973_CABLE_TYPE_JIG_UART_ON_WITH_VBUS)) ? 1 : 0;
  519. chip->curr_status.l200k_usb_connect = (chip->curr_status.cable_type ==
  520. MUIC_RT8973_CABLE_TYPE_L200K_SPEC_USB) ? 1 : 0;
  521. }
  522. #define FLAG_HIGH (0x01)
  523. #define FLAG_LOW (0x02)
  524. #define FLAG_LOW_TO_HIGH (0x04)
  525. #define FLAG_HIGH_TO_LOW (0x08)
  526. #define FLAG_RISING FLAG_LOW_TO_HIGH
  527. #define FLAG_FALLING FLAG_HIGH_TO_LOW
  528. #define FLAG_CHANGED (FLAG_LOW_TO_HIGH | FLAG_HIGH_TO_LOW)
  529. static inline uint32_t state_check(unsigned int old_state,
  530. unsigned int new_state,
  531. unsigned int bit_mask)
  532. {
  533. unsigned int ret = 0;
  534. old_state &= bit_mask;
  535. new_state &= bit_mask;
  536. if (new_state)
  537. ret |= FLAG_HIGH;
  538. else
  539. ret |= FLAG_LOW;
  540. if (old_state != new_state) {
  541. if (new_state)
  542. ret |= FLAG_LOW_TO_HIGH;
  543. else
  544. ret |= FLAG_HIGH_TO_LOW;
  545. }
  546. return ret;
  547. }
  548. struct rt8973_event_handler {
  549. char *name;
  550. uint32_t bit_mask;
  551. uint32_t type;
  552. void (*handler) (struct rt8973_chip *chip,
  553. const struct rt8973_event_handler *handler,
  554. unsigned int old_status, unsigned int new_status);
  555. };
  556. static void rt8973_ocp_handler(struct rt8973_chip *chip,
  557. const struct rt8973_event_handler *handler,
  558. unsigned int old_status, unsigned int new_status)
  559. {
  560. pr_err("RT8973 : OCP event triggered\n");
  561. if (chip->pdata->ocp_callback)
  562. chip->pdata->ocp_callback();
  563. }
  564. static void rt8973_ovp_handler(struct rt8973_chip *chip,
  565. const struct rt8973_event_handler *handler,
  566. unsigned int old_status, unsigned int new_status)
  567. {
  568. pr_err("RT8973 : OVP event triggered\n");
  569. if (chip->pdata->ovp_callback)
  570. chip->pdata->ovp_callback();
  571. }
  572. static void rt8973_otp_handler(struct rt8973_chip *chip,
  573. const struct rt8973_event_handler *handler,
  574. unsigned int old_status, unsigned int new_status)
  575. {
  576. pr_err("RT8973 : OTP event triggered\n");
  577. if (chip->pdata->otp_callback)
  578. chip->pdata->otp_callback();
  579. }
  580. struct rt8973_event_handler urgent_event_handlers[] = {
  581. {
  582. .name = "OCP",
  583. .bit_mask = (1 << OCP_SHIFT),
  584. .type = FLAG_RISING,
  585. .handler = rt8973_ocp_handler,
  586. },
  587. {
  588. .name = "OVP",
  589. .bit_mask = (1 << OVP_SHIFT),
  590. .type = FLAG_RISING,
  591. .handler = rt8973_ovp_handler,
  592. },
  593. {
  594. .name = "OTP",
  595. .bit_mask = (1 << OTP_SHIFT),
  596. .type = FLAG_RISING,
  597. .handler = rt8973_otp_handler,
  598. },
  599. };
  600. #if RTDBGINFO_LEVEL <= RTDBGLEVEL
  601. static char *rt8973_cable_names[] = {
  602. "MUIC_RT8973_CABLE_TYPE_NONE",
  603. "MUIC_RT8973_CABLE_TYPE_UART",
  604. "MUIC_RT8973_CABLE_TYPE_USB",
  605. "MUIC_RT8973_CABLE_TYPE_OTG",
  606. /* TA Group */
  607. "MUIC_RT8973_CABLE_TYPE_REGULAR_TA",
  608. "MUIC_RT8973_CABLE_TYPE_ATT_TA",
  609. "MUIC_RT8973_CABLE_TYPE_0x15",
  610. "MUIC_RT8973_CABLE_TYPE_TYPE1_CHARGER",
  611. "MUIC_RT8973_CABLE_TYPE_0x1A",
  612. /* JIG Group */
  613. "MUIC_RT8973_CABLE_TYPE_JIG_USB_OFF",
  614. "MUIC_RT8973_CABLE_TYPE_JIG_USB_ON",
  615. "MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF",
  616. "MUIC_RT8973_CABLE_TYPE_JIG_UART_ON",
  617. /* JIG type with VBUS */
  618. "MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF_WITH_VBUS",
  619. "MUIC_RT8973_CABLE_TYPE_JIG_UART_ON_WITH_VBUS",
  620. "MUIC_RT8973_CABLE_TYPE_CDP",
  621. "MUIC_RT8973_CABLE_TYPE_L200K_SPEC_USB",
  622. "MUIC_RT8973_CABLE_TYPE_UNKNOWN",
  623. "MUIC_RT8973_CABLE_TYPE_INVALID",
  624. };
  625. #endif /*RTDBGINFO_LEVEL<=RTDBGLEVEL */
  626. #define cable_change_callback sec_charger_cb
  627. extern void sec_charger_cb(u8 cable_type);
  628. static void rt8973_cable_change_handler(struct rt8973_chip *chip,
  629. const struct rt8973_event_handler
  630. *handler, unsigned int old_status,
  631. unsigned int new_status)
  632. {
  633. pr_info("RT8973 : Cable change to %s\n",
  634. rt8973_cable_names[chip->curr_status.cable_type]);
  635. cable_change_callback(chip->curr_status.cable_type);
  636. if (chip->pdata->cable_chg_callback)
  637. chip->pdata->cable_chg_callback(chip->curr_status.cable_type);
  638. }
  639. static void rt8973_otg_attach_handler(struct rt8973_chip *chip,
  640. const struct rt8973_event_handler
  641. *handler, unsigned int old_status,
  642. unsigned int new_status)
  643. {
  644. pr_info("RT8973 : OTG attached\n");
  645. /* Disable USBCHDEN and AutoConfig */
  646. rt8973_reg_write(chip, RT8973_REG_MANUAL_SW1, 0x24);
  647. rt8973_clr_bits(chip, RT8973_REG_CONTROL, (1 << 2) | (1 << 6));
  648. if (chip->pdata->otg_callback)
  649. chip->pdata->otg_callback(1);
  650. }
  651. static void rt8973_otg_detach_handler(struct rt8973_chip *chip,
  652. const struct rt8973_event_handler
  653. *handler, unsigned int old_status,
  654. unsigned int new_status)
  655. {
  656. pr_info("RT8973 : OTG detached\n");
  657. /* Enable USBCHDEN and AutoConfig */
  658. rt8973_reg_write(chip, RT8973_REG_MANUAL_SW1, 0x00);
  659. rt8973_set_bits(chip, RT8973_REG_CONTROL, (1 << 2) | (1 << 6));
  660. if (chip->pdata->otg_callback)
  661. chip->pdata->otg_callback(0);
  662. }
  663. static void rt8973_usb_attach_handler(struct rt8973_chip *chip,
  664. const struct rt8973_event_handler
  665. *handler, unsigned int old_status,
  666. unsigned int new_status)
  667. {
  668. pr_info("RT8973 : USB attached\n");
  669. if (chip->pdata->usb_callback)
  670. chip->pdata->usb_callback(1);
  671. }
  672. static void rt8973_usb_detach_handler(struct rt8973_chip *chip,
  673. const struct rt8973_event_handler
  674. *handler, unsigned int old_status,
  675. unsigned int new_status)
  676. {
  677. pr_info("RT8973 : USB detached\n");
  678. if (chip->pdata->usb_callback)
  679. chip->pdata->usb_callback(0);
  680. }
  681. static void rt8973_uart_attach_handler(struct rt8973_chip *chip,
  682. const struct rt8973_event_handler
  683. *handler, unsigned int old_status,
  684. unsigned int new_status)
  685. {
  686. pr_info("RT8973 : UART attached\n");
  687. if (chip->pdata->uart_callback)
  688. chip->pdata->uart_callback(1);
  689. }
  690. static void rt8973_uart_detach_handler(struct rt8973_chip *chip,
  691. const struct rt8973_event_handler
  692. *handler, unsigned int old_status,
  693. unsigned int new_status)
  694. {
  695. pr_info("RT8973 : UART detached\n");
  696. if (chip->pdata->uart_callback)
  697. chip->pdata->uart_callback(0);
  698. }
  699. static inline jig_type_t get_jig_type(int cable_type)
  700. {
  701. jig_type_t type = JIG_USB_BOOT_ON;
  702. switch (cable_type) {
  703. case MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF_WITH_VBUS:
  704. case MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF:
  705. type = JIG_UART_BOOT_OFF;
  706. break;
  707. case MUIC_RT8973_CABLE_TYPE_JIG_UART_ON_WITH_VBUS:
  708. case MUIC_RT8973_CABLE_TYPE_JIG_UART_ON:
  709. type = JIG_UART_BOOT_ON;
  710. break;
  711. case MUIC_RT8973_CABLE_TYPE_JIG_USB_OFF:
  712. type = JIG_USB_BOOT_OFF;
  713. break;
  714. case MUIC_RT8973_CABLE_TYPE_JIG_USB_ON:
  715. type = JIG_USB_BOOT_ON;
  716. break;
  717. }
  718. return type;
  719. }
  720. static void rt8973_jig_attach_handler(struct rt8973_chip *chip,
  721. const struct rt8973_event_handler
  722. *handler, unsigned int old_status,
  723. unsigned int new_status)
  724. {
  725. jig_type_t type;
  726. type = get_jig_type(chip->curr_status.cable_type);
  727. pr_info("RT8973 : JIG attached (type = %d)\n", (int)type);
  728. if (chip->pdata->jig_callback)
  729. chip->pdata->jig_callback(type, 1);
  730. }
  731. static void rt8973_jig_detach_handler(struct rt8973_chip *chip,
  732. const struct rt8973_event_handler
  733. *handler, unsigned int old_status,
  734. unsigned int new_status)
  735. {
  736. jig_type_t type;
  737. type = get_jig_type(chip->prev_status.cable_type);
  738. pr_info("RT8973 : JIG detached (type = %d)\n", (int)type);
  739. if (chip->pdata->jig_callback)
  740. chip->pdata->jig_callback(type, 0);
  741. }
  742. static void rt8973_l200k_usb_attach_handler(struct rt8973_chip *chip,
  743. const struct rt8973_event_handler
  744. *handler, unsigned int old_status,
  745. unsigned int new_status)
  746. {
  747. pr_info("RT8973 : 200K special USB cable attached\n");
  748. /* Make switch connect to USB path */
  749. rt8973_reg_write(chip, RT8973_REG_MANUAL_SW1, 0x24);
  750. /* Change to manual-config */
  751. rt8973_clr_bits(chip, RT8973_REG_CONTROL, 1 << 2);
  752. }
  753. static void rt8973_l200k_usb_detach_handler(struct rt8973_chip *chip,
  754. const struct rt8973_event_handler
  755. *handler, unsigned int old_status,
  756. unsigned int new_status)
  757. {
  758. pr_info("RT8973 : 200K special USB cable detached\n");
  759. /* Make switch opened */
  760. rt8973_reg_write(chip, RT8973_REG_MANUAL_SW1, 0x00);
  761. /* Change to auto-config */
  762. rt8973_set_bits(chip, RT8973_REG_CONTROL, 1 << 2);
  763. }
  764. struct rt8973_event_handler normal_event_handlers[] = {
  765. {
  766. .name = "200K special USB attached",
  767. .bit_mask = (1 << L200K_USB_SHIFT),
  768. .type = FLAG_RISING,
  769. .handler = rt8973_l200k_usb_attach_handler,
  770. },
  771. {
  772. .name = "200K special USB detached",
  773. .bit_mask = (1 << L200K_USB_SHIFT),
  774. .type = FLAG_FALLING,
  775. .handler = rt8973_l200k_usb_detach_handler,
  776. },
  777. {
  778. .name = "Cable changed",
  779. .bit_mask = (1 << CABLE_CHG_SHIFT),
  780. .type = FLAG_HIGH,
  781. .handler = rt8973_cable_change_handler,
  782. },
  783. {
  784. .name = "OTG attached",
  785. .bit_mask = (1 << OTG_SHIFT),
  786. .type = FLAG_RISING,
  787. .handler = rt8973_otg_attach_handler,
  788. },
  789. {
  790. .name = "OTG detached",
  791. .bit_mask = (1 << OTG_SHIFT),
  792. .type = FLAG_FALLING,
  793. .handler = rt8973_otg_detach_handler,
  794. },
  795. {
  796. .name = "USB attached",
  797. .bit_mask = (1 << USB_SHIFT),
  798. .type = FLAG_RISING,
  799. .handler = rt8973_usb_attach_handler,
  800. },
  801. {
  802. .name = "USB detached",
  803. .bit_mask = (1 << USB_SHIFT),
  804. .type = FLAG_FALLING,
  805. .handler = rt8973_usb_detach_handler,
  806. },
  807. {
  808. .name = "UART attached",
  809. .bit_mask = (1 << UART_SHIFT),
  810. .type = FLAG_RISING,
  811. .handler = rt8973_uart_attach_handler,
  812. },
  813. {
  814. .name = "UART detached",
  815. .bit_mask = (1 << UART_SHIFT),
  816. .type = FLAG_FALLING,
  817. .handler = rt8973_uart_detach_handler,
  818. },
  819. {
  820. .name = "JIG attached",
  821. .bit_mask = (1 << JIG_SHIFT),
  822. .type = FLAG_RISING,
  823. .handler = rt8973_jig_attach_handler,
  824. },
  825. {
  826. .name = "JIG detached",
  827. .bit_mask = (1 << JIG_SHIFT),
  828. .type = FLAG_FALLING,
  829. .handler = rt8973_jig_detach_handler,
  830. },
  831. };
  832. static void rt8973_process_urgent_evt(rt8973_chip_t *chip)
  833. {
  834. unsigned int i;
  835. unsigned int type, sta_chk;
  836. uint32_t prev_status, curr_status;
  837. prev_status = chip->prev_status.status;
  838. curr_status = chip->curr_status.status;
  839. for (i = 0; i < ARRAY_SIZE(urgent_event_handlers); i++) {
  840. sta_chk = state_check(prev_status,
  841. curr_status,
  842. urgent_event_handlers[i].bit_mask);
  843. type = urgent_event_handlers[i].type;
  844. if (type & sta_chk) {
  845. if (urgent_event_handlers[i].handler)
  846. urgent_event_handlers[i].handler(chip,
  847. urgent_event_handlers + i,
  848. prev_status,
  849. curr_status);
  850. }
  851. }
  852. }
  853. static void rt8973_process_normal_evt(rt8973_chip_t *chip)
  854. {
  855. unsigned int i;
  856. unsigned int type, sta_chk;
  857. uint32_t prev_status, curr_status;
  858. prev_status = chip->prev_status.status;
  859. curr_status = chip->curr_status.status;
  860. for (i = 0; i < ARRAY_SIZE(normal_event_handlers); i++) {
  861. sta_chk = state_check(prev_status,
  862. curr_status,
  863. normal_event_handlers[i].bit_mask);
  864. type = normal_event_handlers[i].type;
  865. if (type & sta_chk) {
  866. if (normal_event_handlers[i].handler)
  867. normal_event_handlers[i].handler(chip,
  868. normal_event_handlers + i,
  869. prev_status,
  870. curr_status);
  871. }
  872. }
  873. }
  874. #if defined(CONFIG_TOUCHSCREEN_MELFAS_CS02_GF1) || defined(CONFIG_TOUCHSCREEN_IST30XX)
  875. extern void charger_enable(int enable);
  876. #endif
  877. #if defined(CONFIG_TOUCHSCREEN_MMS136)
  878. extern void tsp_charger_infom(bool en);
  879. #endif
  880. #if defined(CONFIG_MACH_VICTORLTE_CTC)
  881. #define PMIC_SLAVE_ID 0x0
  882. #define PMIC_SMBBP_BAT_IF_BPD_CTRL 0x1248
  883. /* control flash function without BAT_ID */
  884. static void flash_control(bool en) {
  885. u8 sid = PMIC_SLAVE_ID;
  886. u8 buf[16];
  887. int addr = PMIC_SMBBP_BAT_IF_BPD_CTRL;
  888. spmi_ext_register_readl_extra(sid, addr, buf, 1);
  889. if (en)
  890. buf[0] &= ~BIT(0);
  891. else
  892. buf[0] |= BIT(0);
  893. spmi_ext_register_writel_extra(sid, addr, buf, 1);
  894. }
  895. #endif
  896. static void rt8973_irq_work(struct work_struct *work)
  897. {
  898. int ret;
  899. rt8973_chip_t *chip = container_of(to_delayed_work(work),
  900. rt8973_chip_t, irq_work);
  901. /* enable interrupt */
  902. rt8973_clr_bits(chip, RT8973_REG_CONTROL, 0x01);
  903. chip->prev_status = chip->curr_status;
  904. ret = rt8973_update_regs(chip);
  905. if (ret < 0) {
  906. pr_err("RT8973 : Error : can't update(read) register status:%d\n", ret);
  907. /* roll back status */
  908. chip->curr_status = chip->prev_status;
  909. return;
  910. }
  911. /* for printing out registers -- start */
  912. pr_info("%s : INTF1 = 0x%x, INTF2 = 0x%x\n", __FUNCTION__,
  913. (int)chip->curr_status.irq_flags[0],
  914. (int)chip->curr_status.irq_flags[1]);
  915. pr_info("%s : DEV1 = 0x%x, DEV2 = 0x%x\n", __FUNCTION__,
  916. (int)chip->curr_status.device_reg[0],
  917. (int)chip->curr_status.device_reg[1]);
  918. pr_info("%s : ADC = 0x%x\n", __FUNCTION__,
  919. (int)chip->curr_status.id_adc);
  920. /* for printint out registers -- end*/
  921. rt8973_preprocess_status(chip);
  922. pr_info("RT8973 : Status : cable type = %d,\n"
  923. "vbus = %d, accessory = %d\n"
  924. "ocp = %d, ovp = %d, otp = %d,\n"
  925. "adc_chg = %d, cable_chg = %d\n"
  926. "otg = %d, dcdt = %d, usb = %d,\n"
  927. "uart = %d, jig = %d\n"
  928. "200k usb cable = %d\n",
  929. chip->curr_status.cable_type,
  930. chip->curr_status.vbus_status,
  931. chip->curr_status.accessory_status,
  932. chip->curr_status.ocp_status,
  933. chip->curr_status.ovp_status,
  934. chip->curr_status.otp_status,
  935. chip->curr_status.adc_chg_status,
  936. chip->curr_status.cable_chg_status,
  937. chip->curr_status.otg_status,
  938. chip->curr_status.dcdt_status,
  939. chip->curr_status.usb_connect,
  940. chip->curr_status.uart_connect,
  941. chip->curr_status.jig_connect,
  942. chip->curr_status.l200k_usb_connect);
  943. rt8973_process_urgent_evt(chip);
  944. if (chip->curr_status.dcdt_status) {
  945. if (chip->dcdt_retry_count >= DCD_T_RETRY) {
  946. chip->dcdt_retry_count = 0; /* reset counter */
  947. pr_info("RT8973 : Exceeded maxima retry times\n");
  948. /* continue to process event */
  949. } else {
  950. chip->dcdt_retry_count++;
  951. /* DCD_T -> roll back previous status */
  952. chip->curr_status = chip->prev_status;
  953. pr_info("RT8973 : DCD_T event triggered, do re-detect\n");
  954. rt8973_clr_bits(chip, RT8973_REG_CONTROL, 0x60);
  955. msleep_interruptible(1);
  956. rt8973_set_bits(chip, RT8973_REG_CONTROL, 0x60);
  957. return;
  958. }
  959. }
  960. rt8973_process_normal_evt(chip);
  961. //////////////////////////////////////////////////////////////////////
  962. if(chip->curr_status.cable_type == MUIC_RT8973_CABLE_TYPE_JIG_USB_OFF ||\
  963. chip->curr_status.cable_type == MUIC_RT8973_CABLE_TYPE_JIG_USB_ON ||\
  964. chip->curr_status.cable_type == MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF ||\
  965. chip->curr_status.cable_type == MUIC_RT8973_CABLE_TYPE_JIG_UART_ON)
  966. jig_state = 1;
  967. else
  968. jig_state = 0;
  969. if(chip->curr_status.cable_type == MUIC_RT8973_CABLE_TYPE_JIG_USB_OFF ||\
  970. chip->curr_status.cable_type == MUIC_RT8973_CABLE_TYPE_JIG_USB_ON ||\
  971. chip->curr_status.cable_type == MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF) {
  972. #if defined(CONFIG_MACH_VICTORLTE_CTC)
  973. flash_control(true);
  974. #endif
  975. }
  976. else if(chip->curr_status.cable_type == MUIC_RT8973_CABLE_TYPE_UART ||\
  977. chip->curr_status.cable_type == MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF) {
  978. rt_uart_connecting = 1;
  979. #if defined(CONFIG_MACH_VICTORLTE_CTC)
  980. flash_control(true);
  981. #endif
  982. }
  983. else if(chip->curr_status.cable_type==MUIC_RT8973_CABLE_TYPE_NONE){
  984. rt_uart_connecting = 0;
  985. #if defined(CONFIG_MACH_VICTORLTE_CTC)
  986. flash_control(false);
  987. #endif
  988. }
  989. //////////////////////////////////////////////////////////////////////
  990. #if defined(CONFIG_TOUCHSCREEN_MELFAS_CS02_GF1) || defined(CONFIG_TOUCHSCREEN_IST30XX)
  991. if(chip->curr_status.cable_type==MUIC_RT8973_CABLE_TYPE_NONE){
  992. charger_enable(0);
  993. }
  994. else if(chip->curr_status.cable_type==MUIC_RT8973_CABLE_TYPE_UNKNOWN){
  995. printk("[TSP] %s : attached, but don't noti (MUIC_RT8973_CABLE_TYPE_UNKNOWN)\n",__func__);
  996. }
  997. else{
  998. charger_enable(1);
  999. }
  1000. #endif
  1001. #if defined(CONFIG_TOUCHSCREEN_MMS136)
  1002. if(chip->curr_status.cable_type!=0)
  1003. tsp_charger_infom(1);
  1004. else
  1005. tsp_charger_infom(0);
  1006. #endif
  1007. }
  1008. static irqreturn_t rt8973_irq_handler(int irq, void *data)
  1009. {
  1010. struct rt8973_chip *chip = data;
  1011. wake_lock_timeout(&(chip->muic_wake_lock), msecs_to_jiffies(1000));
  1012. pr_info("RT8973 : RT8973 interrupt triggered! irq = %d\n", irq);
  1013. queue_delayed_work(chip->wq, &chip->irq_work, msecs_to_jiffies(250));
  1014. return IRQ_HANDLED;
  1015. }
  1016. static const struct i2c_device_id rt8973_id_table[] = {
  1017. {"rt8973", 0},
  1018. {},
  1019. };
  1020. MODULE_DEVICE_TABLE(i2c, rt8973_id_table);
  1021. static bool rt8973_reset_check(struct i2c_client *iic)
  1022. {
  1023. int ret;
  1024. ret = i2c_smbus_read_byte_data(iic, RT8973_REG_CHIP_ID);
  1025. if (ret < 0) {
  1026. pr_err("RT8973 : can't read device ID from IC(%d)\n", ret);
  1027. return false;
  1028. }
  1029. if ((ret&0x07) != 0x02) {
  1030. pr_err("RT8973 : vendor ID mismatch (0x%d)!\n", ret);
  1031. return false;
  1032. }
  1033. /* write default value instead of sending reset command*/
  1034. /* REG[0x02] = 0xE5, REG[0x14] = 0x01*/
  1035. i2c_smbus_write_byte_data(iic, RT8973_REG_CONTROL, 0xE5);
  1036. i2c_smbus_write_byte_data(iic, RT8973_REG_MANUAL_SW2, 0x01);
  1037. return true;
  1038. }
  1039. #define CHECK_PSY_READY 0
  1040. #define BATTERY_PSY_NAME "battery"
  1041. #define CHARGER_PSY_NAME "sec-charger"
  1042. #define POLL_DELAY 50
  1043. static void rt8973_init_work(struct work_struct *work)
  1044. {
  1045. int ret;
  1046. rt8973_chip_t *chip = container_of(to_delayed_work(work),
  1047. rt8973_chip_t, init_work);
  1048. #if CHECK_PSY_READY
  1049. /* make sure that Samsung battery and battery are ready to receive notification*/
  1050. struct power_supply *psy_battery = NULL;
  1051. struct power_supply *psy_charger = NULL;
  1052. do {
  1053. psy_battery = power_supply_get_by_name(BATTERY_PSY_NAME);
  1054. psy_charger = power_supply_get_by_name(CHARGER_PSY_NAME);
  1055. if (psy_battery == NULL || psy_charger == NULL) {
  1056. dev_info(&chip->iic->dev, "RT8973 : Battery and charger are not ready\r\n");
  1057. msleep(POLL_DELAY);
  1058. }
  1059. } while (psy_battery == NULL || psy_charger == NULL);
  1060. dev_info(&chip->iic->dev, "RT8973 : Battery and charger are ready\r\n");
  1061. #endif
  1062. /* Dummy read */
  1063. rt8973_reg_read(chip, RT8973_REG_INT_FLAG1);
  1064. rt8973_clr_bits(chip, RT8973_REG_CONTROL, 0x60);
  1065. msleep_interruptible(1);
  1066. rt8973_set_bits(chip, RT8973_REG_CONTROL, 0x60);
  1067. /* enable interrupt */
  1068. ret = rt8973_clr_bits(chip, RT8973_REG_CONTROL, 0x01);
  1069. if (ret < 0) {
  1070. dev_err(&chip->iic->dev, "can't enable rt8973's INT (%d)\r\n",ret);
  1071. }
  1072. /* Execute 1st detection and report cable type*/
  1073. queue_delayed_work(chip->wq, &chip->irq_work, msecs_to_jiffies(250));
  1074. }
  1075. static void rt8973_init_regs(rt8973_chip_t *chip)
  1076. {
  1077. int chip_id = rt8973_reg_read(chip, RT8973_REG_CHIP_ID);
  1078. pr_info("RT8973 : rt8973_init_regs\n");
  1079. /* initialize with MUIC_RT8973_CABLE_TYPE_INVALID
  1080. * to make 1st detection work always report cable type */
  1081. chip->curr_status.cable_type = MUIC_RT8973_CABLE_TYPE_INVALID;
  1082. chip->curr_status.id_adc = 0x1f;
  1083. /* for rev 0, turn off i2c reset function */
  1084. if (((chip_id & 0xf8) >> 3) == 0)
  1085. rt8973_set_bits(chip, RT8973_REG_CONTROL, 0x08);
  1086. /* Revision ID >=2 --> RT8973A, it should use Reg[0x23] instead of Reg[0x07] to read ADC */
  1087. if (((chip_id & 0xf8) >> 3) >= 2)
  1088. chip->adc_reg_addr = 0x23;
  1089. else
  1090. chip->adc_reg_addr = RT8973_REG_ADC;
  1091. pr_info("RT8973 : chip_id = 0x%x, ADC register addr = 0x%x\n", chip_id, chip->adc_reg_addr);
  1092. /* Only mask Connect */
  1093. rt8973_reg_write(chip, RT8973_REG_INTERRUPT_MASK1, 0x20);
  1094. /* Only mask OCP_LATCH and POR */
  1095. rt8973_reg_write(chip, RT8973_REG_INTERRUPT_MASK2, 0x24);
  1096. /* enable interrupt */
  1097. //rt8973_clr_bits(chip, RT8973_REG_CONTROL, 0x01);
  1098. /* Execute 1st dectection */
  1099. queue_delayed_work(chip->wq, &chip->init_work, msecs_to_jiffies(10));
  1100. }
  1101. static ssize_t adc_show(struct device *dev,
  1102. struct device_attribute *attr, char *buf)
  1103. {
  1104. u8 adc_value[] = "1C";
  1105. u8 adc_fail = 0;
  1106. if (current_status->cable_type == MUIC_RT8973_CABLE_TYPE_JIG_UART_OFF) {
  1107. pr_info("RT8973 : adc_show JIG UART BOOT OFF\n");
  1108. return sprintf(buf, "%s\n", adc_value);
  1109. } else {
  1110. pr_info("RT8973 : adc_show no detect\n");
  1111. return sprintf(buf, "%d\n", adc_fail);
  1112. }
  1113. }
  1114. static ssize_t usb_state_show_attrs(struct device *dev,
  1115. struct device_attribute *attr, char *buf)
  1116. {
  1117. if (current_status->usb_connect)
  1118. return sprintf(buf, "USB_STATE_CONFIGURED\n");
  1119. else
  1120. return sprintf(buf, "USB_STATE_NOTCONFIGURED\n");
  1121. }
  1122. static ssize_t usb_sel_show_attrs(struct device *dev,
  1123. struct device_attribute *attr, char *buf)
  1124. {
  1125. return sprintf(buf, "PDA");
  1126. }
  1127. #if defined(CONFIG_USB_HOST_NOTIFY)
  1128. static ssize_t rt8973_muic_set_otg_test(struct device *dev,
  1129. struct device_attribute *attr,
  1130. const char *buf, size_t count)
  1131. {
  1132. pr_info("%s: buf:%s\n", __func__, buf);
  1133. if (!strncmp(buf, "0", 1)) {
  1134. sec_otg_notify(HNOTIFY_OTG_POWER_ON);
  1135. } else if (!strncmp(buf, "1", 1)) {
  1136. sec_otg_notify(HNOTIFY_OTG_POWER_OFF);
  1137. } else {
  1138. pr_warn("%s: Wrong command\n", __func__);
  1139. return count;
  1140. }
  1141. return count;
  1142. }
  1143. #endif
  1144. static DEVICE_ATTR(adc, S_IRUGO | S_IWUSR | S_IWGRP | S_IXOTH /*0665 */ ,
  1145. adc_show, NULL);
  1146. static DEVICE_ATTR(usb_state, S_IRUGO, usb_state_show_attrs, NULL);
  1147. static DEVICE_ATTR(usb_sel, S_IRUGO, usb_sel_show_attrs, NULL);
  1148. #if defined(CONFIG_USB_HOST_NOTIFY)
  1149. static DEVICE_ATTR(otg_test, 0664, NULL, rt8973_muic_set_otg_test);
  1150. #endif
  1151. /*
  1152. static int sec_get_usb_vbus(unsigned int *level)
  1153. {
  1154. if (current_status->vbus_status) {
  1155. pr_info("RT8973 : set VBUS_HIGH\n");
  1156. *level = VBUS_HIGH;
  1157. } else {
  1158. pr_info("RT8973 : set VBUS_LOW\n");
  1159. *level = VBUS_LOW;
  1160. }
  1161. return 0;
  1162. }*/
  1163. #ifdef CONFIG_OF
  1164. static int rt8973_parse_dt(struct device *dev, struct rt8973_platform_data *pdata)
  1165. {
  1166. struct device_node *np = dev->of_node;
  1167. /*changes can be added later, when needed*/
  1168. #if 0
  1169. /* regulator info */
  1170. pdata->i2c_pull_up = of_property_read_bool(np, "rt8973,i2c-pull-up");
  1171. /* reset, irq gpio info */
  1172. pdata->gpio_scl = of_get_named_gpio_flags(np, "rt8973,scl-gpio",
  1173. 0, &pdata->scl_gpio_flags);
  1174. pdata->gpio_sda = of_get_named_gpio_flags(np, "rt8973,sda-gpio",
  1175. 0, &pdata->sda_gpio_flags);
  1176. #endif
  1177. pdata->gpio_int = of_get_named_gpio_flags(np, "rt8973,irq-gpio",
  1178. 0, &pdata->irq_gpio_flags);
  1179. pr_info("RT8973 : %s: irq-gpio: %u \n", __func__, pdata->gpio_int);
  1180. return 0;
  1181. }
  1182. #endif
  1183. static int __devinit rt8973_probe(struct i2c_client *client,
  1184. const struct i2c_device_id *id)
  1185. {
  1186. struct rt8973_platform_data *pdata;
  1187. struct rt8973_chip *chip;
  1188. struct device *switch_dev;
  1189. int ret;
  1190. pr_info("RT8973 : Richtek RT8973 driver probing...\n");
  1191. dev_info(&client->dev,"%s:rt8973 probe called \n",__func__);
  1192. if(client->dev.of_node) {
  1193. pdata = devm_kzalloc(&client->dev,
  1194. sizeof(struct rt8973_platform_data), GFP_KERNEL);
  1195. if (!pdata) {
  1196. dev_err(&client->dev, "Failed to allocate memory \n");
  1197. return -ENOMEM;
  1198. }
  1199. pdata = &rt8973_pdata;
  1200. ret = rt8973_parse_dt(&client->dev, pdata);
  1201. if (ret < 0)
  1202. return ret;
  1203. #if 0
  1204. pdata->callback = rt8973_callback;
  1205. pdata->dock_init = rt8973_dock_init;
  1206. //KBJ temp block ( this source is based on fsa9280, but following value doesn't match fsa880 )
  1207. pdata->oxp_callback = rt8973_oxp_callback;
  1208. pdata->mhl_sel = NULL;
  1209. #endif
  1210. gpio_tlmm_config(GPIO_CFG(pdata->gpio_int, 0, GPIO_CFG_INPUT,
  1211. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  1212. client->irq = gpio_to_irq(pdata->gpio_int);
  1213. } else
  1214. pdata = client->dev.platform_data;
  1215. if (!pdata)
  1216. return -EINVAL;
  1217. if (pdata->dock_init)
  1218. pdata->dock_init();
  1219. ret = i2c_check_functionality(client->adapter,
  1220. I2C_FUNC_SMBUS_BYTE_DATA |
  1221. I2C_FUNC_SMBUS_I2C_BLOCK);
  1222. if (ret < 0) {
  1223. pr_err("RT8973 : Error : i2c functionality check failed\n");
  1224. goto err_i2c_func;
  1225. }
  1226. if (!rt8973_reset_check(client)) {
  1227. ret = -ENODEV;
  1228. goto err_reset_rt8973_fail;
  1229. }
  1230. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1231. if (chip == NULL) {
  1232. ret = -ENOMEM;
  1233. goto err_nomem;
  1234. }
  1235. current_status = &chip->curr_status;
  1236. wake_lock_init(&chip->muic_wake_lock, WAKE_LOCK_SUSPEND,
  1237. "rt8973_wakelock");
  1238. ret = gpio_request(pdata->gpio_int, "RT8973_EINT");
  1239. if (ret < 0)
  1240. pr_err("RT8973 : Warning : failed to request GPIO%d (retval = %d)\n",
  1241. pdata->gpio_int, ret);
  1242. ret = gpio_direction_input(pdata->gpio_int);
  1243. if (ret < 0)
  1244. pr_err("RT8973 : Warning : failed to set GPIO%d as input pin(retval = %d)\n",
  1245. pdata->gpio_int, ret);
  1246. chip->iic = client;
  1247. chip->dev = &client->dev;
  1248. chip->pdata = pdata;
  1249. i2c_set_clientdata(client, chip);
  1250. chip->wq = create_workqueue("rt8973_workqueue");
  1251. INIT_DELAYED_WORK(&chip->irq_work, rt8973_irq_work);
  1252. INIT_DELAYED_WORK(&chip->init_work, rt8973_init_work);
  1253. #ifdef SAMSUNG_MVRL_MUIC_RT8973
  1254. pxa_usb_set_extern_call(PXA_USB_DEV_OTG, vbus, get_vbus,
  1255. sec_get_usb_vbus);
  1256. #endif
  1257. mutex_init(&chip->io_lock);
  1258. pr_info("RT8973 : Request IRQ %d(GPIO %d)...\n",
  1259. gpio_to_irq(pdata->gpio_int), pdata->gpio_int);
  1260. client->irq = gpio_to_irq(pdata->gpio_int);
  1261. ret = request_irq(gpio_to_irq(pdata->gpio_int),
  1262. rt8973_irq_handler, RT8973_IRQF_MODE | IRQF_NO_SUSPEND,
  1263. RT8973_DEVICE_NAME, chip);
  1264. if (ret < 0) {
  1265. pr_err
  1266. ("RT8973 : failed to request irq %d (gpio=%d, retval=%d)\n",
  1267. gpio_to_irq(pdata->gpio_int), pdata->gpio_int, ret);
  1268. goto err_request_irq_fail;
  1269. }
  1270. rt8973_init_regs(chip);
  1271. switch_dev = device_create(sec_class, NULL, 0, NULL, "switch");
  1272. chip->switch_dev = switch_dev;
  1273. if (device_create_file(switch_dev, &dev_attr_adc) < 0)
  1274. pr_err("RT8973 : Failed to create device file(%s)!\n",
  1275. dev_attr_adc.attr.name);
  1276. if (device_create_file(switch_dev, &dev_attr_usb_state) < 0)
  1277. pr_err("RT8973 : Failed to create device file(%s)!\n",
  1278. dev_attr_usb_state.attr.name);
  1279. if (device_create_file(switch_dev, &dev_attr_usb_sel) < 0)
  1280. pr_err("RT8973 : Failed to create device file(%s)!\n",
  1281. dev_attr_usb_sel.attr.name);
  1282. #if defined(CONFIG_USB_HOST_NOTIFY)
  1283. if (device_create_file(switch_dev, &dev_attr_otg_test) < 0)
  1284. pr_err("RT8973 : Failed to create device file(%s)!\n",
  1285. dev_attr_otg_test.attr.name);
  1286. #endif
  1287. #if defined(CONFIG_RT8973_JIG_WAKEUP)
  1288. pr_info("RT8973 : client->irq %d\n",client->irq);
  1289. enable_irq_wake(client->irq);
  1290. pdata->dock_init = rt8973_dock_init;
  1291. #endif
  1292. // muic_int_gpio.mfp = pdata->gpio_int;
  1293. // mmp_gpio_edge_add(&muic_int_gpio);
  1294. pr_info("RT8973 : Richtek RT8973 MUIC driver initialize successfully\n");
  1295. return 0;
  1296. err_request_irq_fail:
  1297. mutex_destroy(&chip->io_lock);
  1298. destroy_workqueue(chip->wq);
  1299. gpio_free(pdata->gpio_int);
  1300. wake_lock_destroy(&chip->muic_wake_lock);
  1301. #if defined(CONFIG_RT8973_JIG_WAKEUP)
  1302. if (client->irq)
  1303. free_irq(client->irq, chip);
  1304. #endif
  1305. kfree(chip);
  1306. err_nomem:
  1307. err_reset_rt8973_fail:
  1308. err_i2c_func:
  1309. return ret;
  1310. }
  1311. static int __devexit rt8973_remove(struct i2c_client *client)
  1312. {
  1313. #if defined(CONFIG_RT8973_JIG_WAKEUP)
  1314. struct rt8973_chip *chip = i2c_get_clientdata(client);
  1315. #else
  1316. struct rt8973_chip *chip;
  1317. #endif
  1318. pr_info("RT8973 : Richtek RT8973 driver removing...\n");
  1319. #ifdef SAMSUNG_MVRL_MUIC_RT8973
  1320. mmp_gpio_edge_del(&muic_int_gpio);
  1321. #endif
  1322. #if defined(CONFIG_RT8973_JIG_WAKEUP)
  1323. if (client->irq) {
  1324. disable_irq_wake(client->irq);
  1325. free_irq(client->irq, chip);
  1326. }
  1327. #endif
  1328. chip = i2c_get_clientdata(client);
  1329. if (chip) {
  1330. gpio_free(chip->pdata->gpio_int);
  1331. mutex_destroy(&chip->io_lock);
  1332. if (chip->wq)
  1333. destroy_workqueue(chip->wq);
  1334. wake_lock_destroy(&chip->muic_wake_lock);
  1335. kfree(chip);
  1336. }
  1337. return 0;
  1338. }
  1339. static void rt8973_shutdown(struct i2c_client *iic)
  1340. {
  1341. struct rt8973_chip *chip;
  1342. chip = i2c_get_clientdata(iic);
  1343. disable_irq(iic->irq);
  1344. cancel_delayed_work_sync(&chip->irq_work);
  1345. pr_info("RT8973 : Shutdown : set rt8973 regs to default setting...\n");
  1346. i2c_smbus_write_byte_data(iic, RT8973_REG_CONTROL, 0xE5);
  1347. i2c_smbus_write_byte_data(iic, RT8973_REG_MANUAL_SW2, 0x01);
  1348. }
  1349. static struct of_device_id rt8973_i2c_match_table[] = {
  1350. { .compatible = "rt8973,i2c",},
  1351. {},
  1352. };
  1353. MODULE_DEVICE_TABLE(of, rt8973_i2c_match_table);
  1354. static struct i2c_driver rt8973_driver = {
  1355. .driver = {
  1356. .name = RT8973_DEVICE_NAME,
  1357. .owner = THIS_MODULE,
  1358. .of_match_table = rt8973_i2c_match_table,
  1359. },
  1360. .probe = rt8973_probe,
  1361. .remove = __devexit_p(rt8973_remove),
  1362. .shutdown = rt8973_shutdown,
  1363. .id_table = rt8973_id_table,
  1364. };
  1365. static int __init rt8973_i2c_init(void)
  1366. {
  1367. int ret;
  1368. #if defined(CONFIG_MACH_CS02) || defined(CONFIG_MACH_CS02VE)
  1369. if (system_rev >= 0x1) {
  1370. ret = i2c_add_driver(&rt8973_driver);
  1371. } else {
  1372. ret = 0;
  1373. }
  1374. #else
  1375. ret = i2c_add_driver(&rt8973_driver);
  1376. #endif
  1377. if (ret != 0)
  1378. pr_err("Failed to register RT8973 I2C driver: %d\n", ret);
  1379. return ret;
  1380. }
  1381. late_initcall(rt8973_i2c_init);
  1382. static void __exit rt8973_i2c_exit(void)
  1383. {
  1384. i2c_del_driver(&rt8973_driver);
  1385. }
  1386. module_exit(rt8973_i2c_exit);
  1387. MODULE_DESCRIPTION("Richtek RT8973 MUIC Driver");
  1388. MODULE_AUTHOR("Patrick Chang <patrick_chang@richtek.com>");
  1389. MODULE_VERSION(RT8973_DRV_VER);
  1390. MODULE_LICENSE("GPL");