smb358_charger.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  1. /*
  2. * smb358_charger.c
  3. * Samsung SMB358 Charger Driver
  4. *
  5. * Copyright (C) 2012 Samsung Electronics
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define DEBUG
  13. #include <linux/battery/sec_charger.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/of_gpio.h>
  17. #ifndef SLOW_CHARGING_CURRENT_STANDARD
  18. #if defined(CONFIG_MACH_HEAT_AIO)
  19. #define SLOW_CHARGING_CURRENT_STANDARD 400
  20. #else
  21. #define SLOW_CHARGING_CURRENT_STANDARD 1000
  22. #endif
  23. #endif
  24. static int smb358_i2c_write(struct i2c_client *client,
  25. int reg, u8 *buf)
  26. {
  27. int ret;
  28. ret = i2c_smbus_write_i2c_block_data(client, reg, 1, buf);
  29. if (ret < 0)
  30. dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret);
  31. return ret;
  32. }
  33. static int smb358_i2c_read(struct i2c_client *client,
  34. int reg, u8 *buf)
  35. {
  36. int ret;
  37. ret = i2c_smbus_read_i2c_block_data(client, reg, 1, buf);
  38. if (ret < 0)
  39. dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret);
  40. return ret;
  41. }
  42. /*static void smb358_i2c_write_array(struct i2c_client *client,
  43. u8 *buf, int size)
  44. {
  45. int i;
  46. for (i = 0; i < size; i += 3)
  47. smb358_i2c_write(client, (u8) (*(buf + i)), (buf + i) + 1);
  48. }*/
  49. static int smb358_update_reg(struct i2c_client *client, int reg, u8 data)
  50. {
  51. int ret;
  52. u8 r_data = 0;
  53. u8 w_data = 0;
  54. u8 o_data = data;
  55. ret = smb358_i2c_read(client, reg, &r_data);
  56. if (ret < 0) {
  57. dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
  58. goto error;
  59. }
  60. w_data = r_data | data;
  61. ret = smb358_i2c_write(client, reg, &w_data);
  62. if (ret < 0) {
  63. dev_err(&client->dev, "%s: error - write(%d)\n", __func__, ret);
  64. goto error;
  65. }
  66. ret = smb358_i2c_read(client, reg, &data);
  67. if (ret < 0) {
  68. dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
  69. goto error;
  70. }
  71. dev_dbg(&client->dev,
  72. "%s: reg(0x%02x) 0x%02x : 0x%02x -> 0x%02x -> 0x%02x\n",
  73. __func__, reg, o_data, r_data, w_data, data);
  74. error:
  75. return ret;
  76. }
  77. static int smb358_clear_reg(struct i2c_client *client, int reg, u8 data)
  78. {
  79. int ret;
  80. u8 r_data = 0;
  81. u8 w_data = 0;
  82. u8 o_data = data;
  83. ret = smb358_i2c_read(client, reg, &r_data);
  84. if (ret < 0) {
  85. dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
  86. goto error;
  87. }
  88. w_data = r_data & (~data);
  89. ret = smb358_i2c_write(client, reg, &w_data);
  90. if (ret < 0) {
  91. dev_err(&client->dev, "%s: error - write(%d)\n", __func__, ret);
  92. goto error;
  93. }
  94. ret = smb358_i2c_read(client, reg, &data);
  95. if (ret < 0) {
  96. dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
  97. goto error;
  98. }
  99. dev_dbg(&client->dev,
  100. "%s: reg(0x%02x)- 0x%02x : 0x%02x -> 0x%02x -> 0x%02x\n",
  101. __func__, reg, o_data, r_data, w_data, data);
  102. error:
  103. return ret;
  104. }
  105. static int smb358_volatile_writes(struct i2c_client *client, u8 value)
  106. {
  107. int ret = 0;
  108. if (value == SMB358_ENABLE_WRITE) {
  109. ret = smb358_update_reg(client, SMB358_COMMAND_A, 0x80);
  110. if (ret < 0) {
  111. dev_err(&client->dev, "%s: error(%d)\n", __func__, ret);
  112. goto error;
  113. }
  114. dev_dbg(&client->dev, "%s: ENABLED\n", __func__);
  115. } else {
  116. ret = smb358_clear_reg(client, SMB358_COMMAND_A, 0x80);
  117. if (ret < 0) {
  118. dev_err(&client->dev, "%s: error(%d)\n", __func__, ret);
  119. goto error;
  120. }
  121. dev_dbg(&client->dev, "%s: DISABLED\n", __func__);
  122. }
  123. error:
  124. return ret;
  125. }
  126. static void smb358_set_command(struct i2c_client *client,
  127. int reg, u8 datum)
  128. {
  129. int val;
  130. u8 after_data;
  131. if (smb358_i2c_write(client, reg, &datum) < 0)
  132. dev_err(&client->dev,
  133. "%s : error!\n", __func__);
  134. val = smb358_i2c_read(client, reg, &after_data);
  135. if (val >= 0)
  136. dev_info(&client->dev,
  137. "%s : reg(0x%02x) 0x%02x => 0x%02x\n",
  138. __func__, reg, datum, after_data);
  139. else
  140. dev_err(&client->dev, "%s : error!\n", __func__);
  141. }
  142. #if 0
  143. static void smb358_test_read(struct i2c_client *client)
  144. {
  145. u8 data = 0;
  146. u32 addr = 0;
  147. for (addr = 0; addr <= 0x0f; addr++) {
  148. smb358_i2c_read(client, addr, &data);
  149. dev_dbg(&client->dev,
  150. "%s : smb358 addr : 0x%02x data : 0x%02x\n",
  151. __func__, addr, data);
  152. }
  153. for (addr = 0x30; addr <= 0x3f; addr++) {
  154. smb358_i2c_read(client, addr, &data);
  155. dev_dbg(&client->dev,
  156. "%s : smb358 addr : 0x%02x data : 0x%02x\n",
  157. __func__, addr, data);
  158. }
  159. }
  160. #endif
  161. static void smb358_read_regs(struct i2c_client *client, char *str)
  162. {
  163. u8 data = 0;
  164. u32 addr = 0;
  165. for (addr = 0; addr <= 0x0f; addr++) {
  166. smb358_i2c_read(client, addr, &data);
  167. sprintf(str+strlen(str), "0x%x, ", data);
  168. }
  169. /* "#" considered as new line in application */
  170. sprintf(str+strlen(str), "#");
  171. for (addr = 0x30; addr <= 0x3f; addr++) {
  172. smb358_i2c_read(client, addr, &data);
  173. sprintf(str+strlen(str), "0x%x, ", data);
  174. }
  175. }
  176. static int smb358_get_aicl_current(u8 aicl_current)
  177. {
  178. int data;
  179. if (aicl_current <= 0x10)
  180. data = 300;
  181. else if (aicl_current <= 0x11)
  182. data = 500;
  183. else if (aicl_current <= 0x12)
  184. data = 700;
  185. else if (aicl_current <= 0x13)
  186. data = 1000;
  187. else if (aicl_current <= 0x14)
  188. data = 1200;
  189. else if (aicl_current <= 0x15)
  190. data = 1300;
  191. else if (aicl_current <= 0x16)
  192. data = 1800;
  193. else
  194. data = 2000;
  195. return data;
  196. }
  197. static int smb358_get_charging_status(struct i2c_client *client)
  198. {
  199. int status = POWER_SUPPLY_STATUS_UNKNOWN;
  200. u8 data_a = 0;
  201. u8 data_b = 0;
  202. u8 data_c = 0;
  203. u8 data_d = 0;
  204. u8 data_e = 0;
  205. u8 therm_control_a = 0;
  206. u8 other_control_a = 0;
  207. /*smb358_test_read(client);*/
  208. smb358_i2c_read(client, SMB358_STATUS_A, &data_a);
  209. dev_dbg(&client->dev,
  210. "%s : charger status A(0x%02x)\n", __func__, data_a);
  211. smb358_i2c_read(client, SMB358_STATUS_B, &data_b);
  212. dev_dbg(&client->dev,
  213. "%s : charger status B(0x%02x)\n", __func__, data_b);
  214. smb358_i2c_read(client, SMB358_STATUS_C, &data_c);
  215. dev_dbg(&client->dev,
  216. "%s : charger status C(0x%02x)\n", __func__, data_c);
  217. smb358_i2c_read(client, SMB358_STATUS_D, &data_d);
  218. dev_dbg(&client->dev,
  219. "%s : charger status D(0x%02x)\n", __func__, data_d);
  220. smb358_i2c_read(client, SMB358_STATUS_E, &data_e);
  221. dev_dbg(&client->dev,
  222. "%s : charger status E(0x%02x)\n", __func__, data_e);
  223. smb358_i2c_read(client, SMB358_THERM_CONTROL_A, &therm_control_a);
  224. dev_dbg(&client->dev,
  225. "%s : THERM_CONTROL_A(0x%02x)\n", __func__, therm_control_a);
  226. smb358_i2c_read(client, SMB358_OTHER_CONTROL_A, &other_control_a);
  227. dev_dbg(&client->dev,
  228. "%s : OTHER_CONTROL_A(0x%02x)\n", __func__, other_control_a);
  229. /* At least one charge cycle terminated,
  230. * Charge current < Termination Current
  231. */
  232. if (data_c & 0x20) {
  233. /* top-off by full charging */
  234. status = POWER_SUPPLY_STATUS_FULL;
  235. goto charging_status_end;
  236. }
  237. /* Is enabled ? */
  238. if (data_c & 0x01) {
  239. /* check for 0x06 : no charging (0b00) */
  240. /* not charging */
  241. if (!(data_c & 0x06)) {
  242. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  243. goto charging_status_end;
  244. } else {
  245. status = POWER_SUPPLY_STATUS_CHARGING;
  246. goto charging_status_end;
  247. }
  248. } else
  249. status = POWER_SUPPLY_STATUS_DISCHARGING;
  250. charging_status_end:
  251. return (int)status;
  252. }
  253. static int smb358_get_charging_health(struct i2c_client *client)
  254. {
  255. int health = POWER_SUPPLY_HEALTH_GOOD;
  256. u8 data_a = 0;
  257. u8 data_b = 0;
  258. u8 data_c = 0;
  259. u8 data_d = 0;
  260. u8 data_e = 0;
  261. smb358_i2c_read(client, SMB358_STATUS_A, &data_a);
  262. dev_dbg(&client->dev,
  263. "%s : charger status A(0x%02x)\n", __func__, data_a);
  264. smb358_i2c_read(client, SMB358_STATUS_B, &data_b);
  265. dev_dbg(&client->dev,
  266. "%s : charger status B(0x%02x)\n", __func__, data_b);
  267. smb358_i2c_read(client, SMB358_STATUS_C, &data_c);
  268. dev_dbg(&client->dev,
  269. "%s : charger status C(0x%02x)\n", __func__, data_c);
  270. smb358_i2c_read(client, SMB358_STATUS_D, &data_d);
  271. dev_dbg(&client->dev,
  272. "%s : charger status D(0x%02x)\n", __func__, data_d);
  273. smb358_i2c_read(client, SMB358_STATUS_E, &data_e);
  274. dev_dbg(&client->dev,
  275. "%s : charger status E(0x%02x)\n", __func__, data_e);
  276. smb358_i2c_read(client, SMB358_INTERRUPT_STATUS_E, &data_e);
  277. dev_dbg(&client->dev,
  278. "%s : charger interrupt status E(0x%02x)\n", __func__, data_e);
  279. if (data_e & 0x01)
  280. health = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
  281. else if (data_e & 0x04)
  282. health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  283. return (int)health;
  284. }
  285. /*static void smb358_allow_volatile_writes(struct i2c_client *client)
  286. {
  287. int val, reg;
  288. u8 data;
  289. reg = SMB358_COMMAND_A;
  290. val = smb358_i2c_read(client, reg, &data);
  291. if ((val >= 0) && !(data & 0x80)) {
  292. dev_dbg(&client->dev,
  293. "%s : reg(0x%02x): 0x%02x", __func__, reg, data);
  294. data |= (0x1 << 7);
  295. if (smb358_i2c_write(client, reg, &data) < 0)
  296. dev_err(&client->dev, "%s : error!\n", __func__);
  297. val = smb358_i2c_read(client, reg, &data);
  298. if (val >= 0) {
  299. data = (u8) data;
  300. dev_dbg(&client->dev, " => 0x%02x\n", data);
  301. }
  302. }
  303. }*/
  304. static u8 smb358_get_float_voltage_data(int float_voltage)
  305. {
  306. u8 data;
  307. if (float_voltage < 3500)
  308. data = 0;
  309. else if(float_voltage <= 4340)
  310. data = (float_voltage - 3500) / 20;
  311. else if(float_voltage == 4350)
  312. data = 0x2B; /* (4340 -3500)/20 + 1 */
  313. else if(float_voltage <= 4500)
  314. data = (float_voltage - 3500) / 20 + 1;
  315. else
  316. data = 51;
  317. return data;
  318. }
  319. static u8 smb358_get_input_current_limit_data(
  320. struct sec_charger_info *charger, int input_current)
  321. {
  322. u8 data;
  323. if (input_current <= 300)
  324. data = 0x00;
  325. else if (input_current <= 500)
  326. data = 0x01;
  327. else if (input_current <= 700)
  328. data = 0x02;
  329. else if (input_current <= 1000)
  330. data = 0x03;
  331. else if (input_current <= 1200)
  332. data = 0x04;
  333. else if (input_current <= 1500)
  334. data = 0x05;
  335. else if (input_current <= 1800)
  336. data = 0x06;
  337. else
  338. data = 0x07; /* set input current to 2000mA */
  339. return (data << 4);
  340. }
  341. static u8 smb358_get_term_current_limit_data(
  342. int termination_current)
  343. {
  344. u8 data;
  345. if (termination_current <= 30)
  346. data = 0x00;
  347. else if (termination_current <= 40)
  348. data = 0x01;
  349. else if (termination_current <= 60)
  350. data = 0x02;
  351. else if (termination_current <= 80)
  352. data = 0x03;
  353. else if (termination_current <= 100)
  354. data = 0x04;
  355. else if (termination_current <= 125)
  356. data = 0x05;
  357. else if (termination_current <= 150)
  358. data = 0x06;
  359. else
  360. data = 0x07; /* set termination current limit to 200mA */
  361. return data;
  362. }
  363. static u8 smb358_get_fast_charging_current_data(
  364. int fast_charging_current)
  365. {
  366. u8 data;
  367. if (fast_charging_current <= 200)
  368. data = 0x00;
  369. else if (fast_charging_current <= 450)
  370. data = 0x01;
  371. else if (fast_charging_current <= 600)
  372. data = 0x02;
  373. else if (fast_charging_current <= 900)
  374. data = 0x03;
  375. else if (fast_charging_current <= 1300)
  376. data = 0x04;
  377. else if (fast_charging_current <= 1500)
  378. data = 0x05;
  379. else if (fast_charging_current <= 1800)
  380. data = 0x06;
  381. else
  382. data = 0x07; /* set fast charging current to 2000mA */
  383. return data << 5;
  384. }
  385. static void smb358_enter_suspend(struct i2c_client *client)
  386. {
  387. u8 data = 0;
  388. pr_info("%s: ENTER SUSPEND\n", __func__);
  389. smb358_update_reg(client, SMB358_COMMAND_A, 0x80);
  390. smb358_set_command(client, SMB358_PIN_ENABLE_CONTROL, 0x18);
  391. data = (data | 0x4);
  392. smb358_set_command(client, SMB358_COMMAND_A, data);
  393. }
  394. #if 0
  395. #if (defined(CONFIG_MACH_MILLET3G_EUR) || defined(CONFIG_MACH_MATISSE3G_OPEN) || defined(CONFIG_MACH_BERLUTI3G_EUR))
  396. static void smb358_aicl_calibrate(struct i2c_client *client)
  397. {
  398. struct sec_charger_info *charger = i2c_get_clientdata(client);
  399. int ret = 0;
  400. u8 data = 0;
  401. u8 count = 0;
  402. u8 current_initial = 0;
  403. u8 current_final = 0;
  404. ret = smb358_i2c_read(client, SMB358_STATUS_E, &data);
  405. if (ret < 0) {
  406. dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
  407. }
  408. /* check If AICL complete */
  409. if(data & 0x10){
  410. current_initial = (data & 0x0F);
  411. if((current_initial >= 0x04) && (current_initial <= 0x05)){
  412. /* set 1000mA */
  413. ret = smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
  414. data = data & 0x0F;
  415. data = data | 0x30;
  416. smb358_set_command(client,SMB358_INPUT_CURRENTLIMIT, data);
  417. }else{
  418. /* set 1800mA */
  419. ret = smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
  420. data = data & 0x0F;
  421. data = data | 0x60;
  422. smb358_set_command(client,SMB358_INPUT_CURRENTLIMIT, data);
  423. }
  424. smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
  425. current_final = (data & 0x0F);
  426. dev_err(&charger->client->dev,
  427. "%s: AICL calibration success! input current (%dmA) -> (%dmA) ! \n",
  428. __func__,current_initial,current_final);
  429. return;
  430. }else{
  431. /* Incase of AICL not complete check three times */
  432. for(count=0 ;count < 3;count++)
  433. {
  434. msleep(300);
  435. ret = smb358_i2c_read(client, SMB358_STATUS_E, &data);
  436. if (ret < 0) {
  437. dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
  438. }
  439. if(data & 0x10){
  440. current_initial = (data & 0x0F);
  441. if((current_initial >= 0x04) && (current_initial <= 0x05)){
  442. /* set 1000mA */
  443. ret = smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
  444. data = data & 0x0F;
  445. data = data | 0x30;
  446. smb358_set_command(client,SMB358_INPUT_CURRENTLIMIT, data);
  447. }else{
  448. /* set 1800mA */
  449. ret = smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
  450. data = data & 0x0F;
  451. data = data | 0x60;
  452. smb358_set_command(client,SMB358_INPUT_CURRENTLIMIT, data);
  453. }
  454. smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
  455. current_final = (data & 0x0F);
  456. dev_err(&charger->client->dev,
  457. "%s: AICL calibration success! input current (%dmA) -> (%dmA) ! \n",
  458. __func__,current_initial,current_final);
  459. return;
  460. }else{
  461. dev_err(&charger->client->dev,
  462. "%s: AICL not complete \n",__func__);
  463. }
  464. }
  465. }
  466. dev_err(&charger->client->dev,
  467. "%s: AICL calibration Failed! current (%dmA) ! \n",__func__,current_initial);
  468. return;
  469. }
  470. #endif
  471. #endif
  472. static void smb358_check_slow_charging(struct work_struct *work)
  473. {
  474. struct sec_charger_info *charger =
  475. container_of(work, struct sec_charger_info, slow_work.work);
  476. u8 i, aicl_data;
  477. int aicl_current = 0;
  478. union power_supply_propval val;
  479. if (charger->pdata->chg_functions_setting &
  480. SEC_CHARGER_NO_GRADUAL_CHARGING_CURRENT) {
  481. pr_err("%s: aicl is disabled\n", __func__);
  482. return;
  483. }
  484. if (charger->pdata->charging_current
  485. [charger->cable_type].input_current_limit <= SLOW_CHARGING_CURRENT_STANDARD) {
  486. charger->is_slow_charging = true;
  487. } else {
  488. for(i = 0; i < 20; i++) {
  489. if (charger->cable_type ==
  490. POWER_SUPPLY_TYPE_BATTERY) {
  491. pr_info("%s: cable is removed\n", __func__);
  492. return;
  493. }
  494. msleep(200);
  495. smb358_i2c_read(charger->client, SMB358_STATUS_E, &aicl_data);
  496. if (aicl_data & 0x10) { /* check AICL complete */
  497. break;
  498. }
  499. if (i == 10) {
  500. pr_info("%s: aicl not complete, retry\n", __func__);
  501. /* disable AICL */
  502. smb358_set_command(charger->client,
  503. SMB358_VARIOUS_FUNCTIONS, 0x81);
  504. /* enable AICL */
  505. smb358_set_command(charger->client,
  506. SMB358_VARIOUS_FUNCTIONS, 0x95);
  507. }
  508. }
  509. aicl_data &= 0xF; /* get only AICL result field */
  510. switch (aicl_data) {
  511. case 0: /* AICL result 300mA */
  512. aicl_current = 300;
  513. break;
  514. case 1: /* AICL result 500mA */
  515. aicl_current = 500;
  516. break;
  517. case 2: /* AICL result 700mA */
  518. aicl_current = 700;
  519. break;
  520. case 3: /* AICL result 1000mA */
  521. aicl_current = 1000;
  522. break;
  523. case 4: /* AICL result 1200mA */
  524. aicl_current = 1200;
  525. break;
  526. case 5: /* AICL result 1300mA */
  527. aicl_current = 1300;
  528. break;
  529. case 6: /* AICL result 1800mA */
  530. aicl_current = 1800;
  531. break;
  532. case 7: /* AICL result 2000mA */
  533. aicl_current = 2000;
  534. break;
  535. default: /* etc */
  536. aicl_current = 2000;
  537. break;
  538. }
  539. if (aicl_current <= SLOW_CHARGING_CURRENT_STANDARD)
  540. charger->is_slow_charging = true;
  541. else
  542. charger->is_slow_charging = false;
  543. }
  544. pr_info("%s: Slow(%d), aicl_current(%d), input_current(%d)\n",
  545. __func__, charger->is_slow_charging, aicl_current, charger->pdata->charging_current
  546. [charger->cable_type].input_current_limit);
  547. psy_do_property("battery", set,
  548. POWER_SUPPLY_PROP_CHARGE_TYPE, val);
  549. }
  550. static void smb358_charger_function_control(
  551. struct i2c_client *client)
  552. {
  553. struct sec_charger_info *charger = i2c_get_clientdata(client);
  554. union power_supply_propval val, input_value;
  555. int status;
  556. u8 data;
  557. psy_do_property("battery", get,
  558. POWER_SUPPLY_PROP_STATUS, input_value);
  559. status = input_value.intval;
  560. charger->charging_current_max =
  561. charger->pdata->charging_current[
  562. charger->cable_type].input_current_limit;
  563. charger->charging_current =
  564. charger->pdata->charging_current[
  565. charger->cable_type].fast_charging_current;
  566. if (charger->charging_current < 0) {
  567. dev_dbg(&client->dev,
  568. "%s : OTG is activated. Ignore command!\n", __func__);
  569. return;
  570. }
  571. psy_do_property("battery", get,
  572. POWER_SUPPLY_PROP_HEALTH, input_value);
  573. if (input_value.intval ==
  574. POWER_SUPPLY_HEALTH_UNSPEC_FAILURE) {
  575. pr_info("[SMB358] Unspec_failure, charger suspend\n");
  576. smb358_enter_suspend(client);
  577. }
  578. else if (charger->cable_type ==
  579. POWER_SUPPLY_TYPE_BATTERY) {
  580. /* Charger Disabled */
  581. smb358_set_command(client, SMB358_COMMAND_A, 0xc0);
  582. if ((status == POWER_SUPPLY_STATUS_FULL) ||\
  583. (input_value.intval == POWER_SUPPLY_HEALTH_OVERHEAT) ||\
  584. (input_value.intval == POWER_SUPPLY_HEALTH_COLD))
  585. return;
  586. pr_info("[SMB358] Set the registers to the default configuration\n");
  587. /* Set the registers to the default configuration */
  588. smb358_set_command(client, SMB358_CHARGE_CURRENT, 0xFE);
  589. smb358_set_command(client, SMB358_INPUT_CURRENTLIMIT, 0x74);
  590. smb358_set_command(client, SMB358_VARIOUS_FUNCTIONS, 0xD7);
  591. data = 0x00;
  592. data |= smb358_get_float_voltage_data(charger->pdata->chg_float_voltage);
  593. smb358_set_command(client, SMB358_FLOAT_VOLTAGE, data);
  594. /* Disable Automatic Recharge */
  595. smb358_set_command(client, SMB358_CHARGE_CONTROL, 0x84);
  596. smb358_set_command(client, SMB358_PIN_ENABLE_CONTROL, 0x09);
  597. smb358_set_command(client, SMB358_THERM_CONTROL_A, 0xF0);
  598. smb358_set_command(client, SMB358_SYSOK_USB30_SELECTION, 0x08);
  599. smb358_set_command(client, SMB358_OTHER_CONTROL_A, 0x01);
  600. smb358_set_command(client, SMB358_OTG_TLIM_THERM_CONTROL, 0xF6);
  601. smb358_set_command(client, SMB358_LIMIT_CELL_TEMPERATURE_MONITOR, 0xA5);
  602. smb358_set_command(client, SMB358_STATUS_INTERRUPT, 0x00);
  603. smb358_set_command(client, SMB358_COMMAND_B, 0x00);
  604. if (charger->pdata->chg_irq) {
  605. smb358_set_command(client, SMB358_STAT_TIMERS_CONTROL, 0x1F);
  606. smb358_set_command(client, SMB358_FAULT_INTERRUPT, 0x0C);
  607. } else {
  608. smb358_set_command(client, SMB358_STAT_TIMERS_CONTROL, 0x0F);
  609. smb358_set_command(client, SMB358_FAULT_INTERRUPT, 0x00);
  610. }
  611. } else {
  612. int full_check_type;
  613. psy_do_property("battery", get,
  614. POWER_SUPPLY_PROP_CHARGE_NOW, val);
  615. if (val.intval == SEC_BATTERY_CHARGING_1ST)
  616. full_check_type = charger->pdata->full_check_type;
  617. else
  618. full_check_type = charger->pdata->full_check_type_2nd;
  619. smb358_i2c_read(client, SMB358_COMMAND_A, &data);
  620. if ((data & 0x10) && charger->pdata->vbus_ctrl_gpio) {
  621. int level;
  622. /* disable otg & charging */
  623. smb358_clear_reg(client, SMB358_COMMAND_A, 0x12);
  624. /* turn off vbus */
  625. gpio_set_value(charger->pdata->vbus_ctrl_gpio, 1);
  626. msleep(30);
  627. level = gpio_get_value_cansleep(charger->pdata->vbus_ctrl_gpio);
  628. pr_info("[SMB358] vbus ctrl gpio %d level %d\n", charger->pdata->vbus_ctrl_gpio, level);
  629. /* turn on vbus */
  630. gpio_set_value(charger->pdata->vbus_ctrl_gpio, 0);
  631. }
  632. /* [STEP - 1] ================================================
  633. * Volatile write permission(bit 7) - allow(1)
  634. * Charging Enable(bit 1) - Disabled(0, default)
  635. * STAT Output(bit 0) - Enabled(0)
  636. */
  637. if (data & 0x02) {
  638. u8 status_c;
  639. u8 status_e;
  640. smb358_i2c_read(client, SMB358_STATUS_C, &status_c);
  641. smb358_i2c_read(client, SMB358_STATUS_E, &status_e);
  642. pr_info("[SMB358] status_c: 0x%x, status_e: 0x%x\n", status_c, status_e);
  643. /* no charge or aicl not complete*/
  644. if (((status_c & 0x06) == 0) || (status_e & 0x10) == 0)
  645. smb358_set_command(client,
  646. SMB358_COMMAND_A, 0xC0);
  647. }
  648. /* [STEP - 2] ================================================
  649. * USB 5/1(9/1.5) Mode(bit 1) - USB1/USB1.5(0), USB5/USB9(1)
  650. * USB/HC Mode(bit 0) - USB5/1 or USB9/1.5 Mode(0)
  651. * High-Current Mode(1)
  652. */
  653. switch (charger->cable_type) {
  654. case POWER_SUPPLY_TYPE_UNKNOWN:
  655. case POWER_SUPPLY_TYPE_MAINS:
  656. case POWER_SUPPLY_TYPE_USB_CDP:
  657. case POWER_SUPPLY_TYPE_MISC:
  658. case POWER_SUPPLY_TYPE_WIRELESS:
  659. case POWER_SUPPLY_TYPE_CARDOCK:
  660. case POWER_SUPPLY_TYPE_UARTOFF:
  661. case POWER_SUPPLY_TYPE_LAN_HUB:
  662. case POWER_SUPPLY_TYPE_MHL_900:
  663. case POWER_SUPPLY_TYPE_MHL_1500:
  664. case POWER_SUPPLY_TYPE_SMART_OTG:
  665. case POWER_SUPPLY_TYPE_SMART_NOTG:
  666. #if defined(CONFIG_MUIC_SUPPORT_MULTIMEDIA_DOCK)
  667. case POWER_SUPPLY_TYPE_MDOCK_TA:
  668. #endif
  669. /* High-current mode */
  670. data = 0x03;
  671. break;
  672. case POWER_SUPPLY_TYPE_UPS:
  673. case POWER_SUPPLY_TYPE_USB:
  674. case POWER_SUPPLY_TYPE_USB_DCP:
  675. case POWER_SUPPLY_TYPE_USB_ACA:
  676. case POWER_SUPPLY_TYPE_MHL_500:
  677. case POWER_SUPPLY_TYPE_MHL_USB:
  678. case POWER_SUPPLY_TYPE_POWER_SHARING:
  679. #if defined(CONFIG_MUIC_SUPPORT_MULTIMEDIA_DOCK)
  680. case POWER_SUPPLY_TYPE_MDOCK_USB:
  681. #endif
  682. /* USB5 */
  683. data = 0x02;
  684. break;
  685. default:
  686. /* USB1 */
  687. data = 0x00;
  688. break;
  689. }
  690. smb358_set_command(client,
  691. SMB358_COMMAND_B, data);
  692. /* [STEP 3] Charge Current(0x00) ===============================
  693. * Set pre-charge current(bit 4:3) - 450mA(11)
  694. * Set fast charge current(bit 7:5)
  695. * Set termination current(bit 2:0)
  696. */
  697. if (charger->siop_level < 100) {
  698. charger->charging_current =
  699. charger->charging_current * charger->siop_level / 100;
  700. }
  701. dev_info(&client->dev,
  702. "%s : fast charging current (%dmA)\n",
  703. __func__, charger->charging_current);
  704. data = 0x18;
  705. data |= smb358_get_fast_charging_current_data(
  706. charger->charging_current);
  707. switch (full_check_type) {
  708. case SEC_BATTERY_FULLCHARGED_CHGGPIO:
  709. case SEC_BATTERY_FULLCHARGED_CHGINT:
  710. case SEC_BATTERY_FULLCHARGED_CHGPSY:
  711. if (val.intval == SEC_BATTERY_CHARGING_1ST) {
  712. dev_info(&client->dev,
  713. "%s : termination current (%dmA)\n",
  714. __func__, charger->pdata->charging_current[
  715. charger->cable_type].full_check_current_1st);
  716. data |= smb358_get_term_current_limit_data(
  717. charger->pdata->charging_current[
  718. charger->cable_type].full_check_current_1st);
  719. } else {
  720. dev_info(&client->dev,
  721. "%s : termination current (%dmA)\n",
  722. __func__, charger->pdata->charging_current[
  723. charger->cable_type].full_check_current_2nd);
  724. data |= smb358_get_term_current_limit_data(
  725. charger->pdata->charging_current[
  726. charger->cable_type].full_check_current_2nd);
  727. }
  728. break;
  729. }
  730. smb358_set_command(client,
  731. SMB358_CHARGE_CURRENT, data);
  732. /* [STEP - 4] =================================================
  733. * Enable(EN) Pin Control(bit 6) - i2c(0), Pin(1)
  734. * Pin control(bit 5) - active high(0), active low(1)
  735. * USB5/1/HC input State(bit3) - Dual-state input(1)
  736. * USB Input Pre-bias(bit 0) - Enable(1)
  737. */
  738. data = 0x09;
  739. if (charger->pdata->chg_gpio_en)
  740. data |= 0x40;
  741. if (charger->pdata->chg_polarity_en)
  742. data |= 0x20;
  743. smb358_set_command(client,
  744. SMB358_PIN_ENABLE_CONTROL, data);
  745. /* [STEP - 5] =============================================== */
  746. dev_info(&client->dev, "%s : input current (%dmA)\n",
  747. __func__, charger->pdata->charging_current
  748. [charger->cable_type].input_current_limit);
  749. /* Input current limit */
  750. data = 0x00;
  751. data |= smb358_get_input_current_limit_data(
  752. charger,
  753. charger->pdata->charging_current
  754. [charger->cable_type].input_current_limit);
  755. smb358_set_command(client,
  756. SMB358_INPUT_CURRENTLIMIT, data);
  757. /* [STEP - 6] =================================================
  758. * Input to System FET(bit 7) - Controlled by Register(1)
  759. * Max System voltage(bit 5) - Vflt + 0.1v(0)
  760. * AICL(bit 4) - Enabled(1)
  761. * VCHG Function(bit 0) - Enabled(1)
  762. */
  763. if (charger->pdata->chg_functions_setting &
  764. SEC_CHARGER_NO_GRADUAL_CHARGING_CURRENT)
  765. /* disable AICL */
  766. smb358_set_command(client,
  767. SMB358_VARIOUS_FUNCTIONS, 0x81);
  768. else {
  769. /* disable AICL */
  770. smb358_set_command(client,
  771. SMB358_VARIOUS_FUNCTIONS, 0x81);
  772. /* enable AICL */
  773. smb358_set_command(client,
  774. SMB358_VARIOUS_FUNCTIONS, 0x95);
  775. }
  776. /* [STEP - 7] =================================================
  777. * Pre-charged to Fast-charge Voltage Threshold(Bit 7:6) - 2.3V
  778. * Float Voltage(bit 5:0)
  779. */
  780. dev_dbg(&client->dev, "%s : float voltage (%dmV)\n",
  781. __func__, charger->pdata->chg_float_voltage);
  782. data = 0x00;
  783. data |= smb358_get_float_voltage_data(
  784. charger->pdata->chg_float_voltage);
  785. smb358_set_command(client,
  786. SMB358_FLOAT_VOLTAGE, data);
  787. /* [STEP - 8] =================================================
  788. * Charge control
  789. * Automatic Recharge disable(bit 7),
  790. * Current Termination disable(bit 6),
  791. * BMD disable(bit 5:4),
  792. * INOK Output Configuration : Push-pull(bit 3)
  793. * AICL glitch filter duration : 20msec(bit 0)
  794. * APSD disable
  795. */
  796. data = 0xC0;
  797. switch (full_check_type) {
  798. case SEC_BATTERY_FULLCHARGED_CHGGPIO:
  799. case SEC_BATTERY_FULLCHARGED_CHGINT:
  800. case SEC_BATTERY_FULLCHARGED_CHGPSY:
  801. /* Enable Current Termination */
  802. data &= 0xBF;
  803. break;
  804. }
  805. smb358_set_command(client,
  806. SMB358_CHARGE_CONTROL, data);
  807. /* [STEP - 9] =================================================
  808. * STAT active low(bit 7),
  809. * Complete charge Timeout(bit 3:2) - Disabled(11)
  810. * Pre-charge Timeout(bit 1:0) - Disable(11)
  811. */
  812. smb358_set_command(client,
  813. SMB358_STAT_TIMERS_CONTROL, 0x1F);
  814. #if defined(CONFIG_MACH_MATISSELTE_VZW)
  815. /* [STEP - 10] =================================================
  816. * Mininum System Voltage(bit 6) - 3.15v(0)
  817. * Therm monitor(bit 4) - Disabled(1)
  818. * Soft Cold/Hot Temp Limit Behavior(bit 3:2, bit 1:0) -
  819. * Charger Current + Float voltage Compensation(11)
  820. */
  821. smb358_set_command(client,
  822. SMB358_THERM_CONTROL_A, 0xB0);
  823. /* [STEP - 11] ================================================
  824. * OTG/ID Pin Control(bit 7:6) - RID Disabled, OTG I2c(00)
  825. * Minimum System Voltage(bit 4) - 3.15V(0)
  826. * Low-Battery/SYSOK Voltage threshold(bit 3:0) - 2.5V(0001)
  827. * if this bit is disabled,
  828. * input current for system will be disabled
  829. */
  830. smb358_set_command(client,
  831. SMB358_OTHER_CONTROL_A, 0x11);
  832. #elif defined(CONFIG_MACH_CHAGALL)
  833. /* [STEP - 10] =================================================
  834. * Mininum System Voltage(bit 6) - 3.60v(0)
  835. * Therm monitor(bit 4) - Disabled(1)
  836. * Soft Cold/Hot Temp Limit Behavior(bit 3:2, bit 1:0) -
  837. * Charger Current + Float voltage Compensation(11)
  838. */
  839. smb358_set_command(client,
  840. SMB358_THERM_CONTROL_A, 0xF0);
  841. /* [STEP - 11] ================================================
  842. * OTG/ID Pin Control(bit 7:6) - RID Disabled, OTG I2c(00)
  843. * Minimum System Voltage(bit 4) - 3.60V(0)
  844. * Low-Battery/SYSOK Voltage threshold(bit 3:0) - 2.5V(0001)
  845. * if this bit is disabled,
  846. * input current for system will be disabled
  847. */
  848. smb358_set_command(client,
  849. SMB358_OTHER_CONTROL_A, 0x01);
  850. #else
  851. /* [STEP - 10] =================================================
  852. * Mininum System Voltage(bit 6) - 3.75v(1)
  853. * Therm monitor(bit 4) - Disabled(1)
  854. * Soft Cold/Hot Temp Limit Behavior(bit 3:2, bit 1:0) -
  855. * Charger Current + Float voltage Compensation(11)
  856. */
  857. smb358_set_command(client,
  858. SMB358_THERM_CONTROL_A, 0xF0);
  859. /* [STEP - 11] ================================================
  860. * OTG/ID Pin Control(bit 7:6) - RID Disabled, OTG I2c(00)
  861. * Minimum System Voltage(bit 4) - 3.75V(1)
  862. * Low-Battery/SYSOK Voltage threshold(bit 3:0) - 2.5V(0001)
  863. * if this bit is disabled,
  864. * input current for system will be disabled
  865. */
  866. smb358_set_command(client,
  867. SMB358_OTHER_CONTROL_A, 0x11);
  868. #endif
  869. /* [STEP - 12] ================================================
  870. * Charge Current Compensation(bit 7:6) - 200mA(00)
  871. * Digital Thermal Regulation Threshold(bit 5:4) - 130c
  872. * OTG current Limit at USBIN(Bit 3:2) - 900mA(11)
  873. * OTG Battery UVLO Threshold(Bit 1:0) - 3.3V(11)
  874. */
  875. smb358_set_command(client,
  876. SMB358_OTG_TLIM_THERM_CONTROL, 0x3F);
  877. /* [STEP - 13] ================================================
  878. * Hard/Soft Limit Cell temp monitor
  879. */
  880. smb358_set_command(client,
  881. SMB358_LIMIT_CELL_TEMPERATURE_MONITOR, 0x01);
  882. /* [STEP - 14] ================================================
  883. * FAULT interrupt - Disabled for non chg_irq, OVP enabled for chg_irq
  884. */
  885. if (charger->pdata->chg_irq) {
  886. smb358_set_command(client,
  887. SMB358_FAULT_INTERRUPT, 0x0C);
  888. } else {
  889. smb358_set_command(client,
  890. SMB358_FAULT_INTERRUPT, 0x00);
  891. }
  892. /* [STEP - 15] ================================================
  893. * STATUS interrupt - Clear
  894. */
  895. smb358_set_command(client,
  896. SMB358_STATUS_INTERRUPT, 0x00);
  897. /* [STEP - 16] ================================================
  898. * Volatile write permission(bit 7) - allowed(1)
  899. * Charging Enable(bit 1) - Enabled(1)
  900. * STAT Output(bit 0) - Enabled(0)
  901. */
  902. smb358_set_command(client,
  903. SMB358_COMMAND_A, 0xC2);
  904. schedule_delayed_work(&charger->slow_work, 0);
  905. }
  906. #if 0
  907. #if (defined(CONFIG_MACH_MILLET3G_EUR) || defined(CONFIG_MACH_MATISSE3G_OPEN) || defined(CONFIG_MACH_BERLUTI3G_EUR))
  908. /* Allow time for AICL to complete */
  909. msleep(1000);
  910. smb358_aicl_calibrate(client);
  911. #endif
  912. #endif
  913. }
  914. static void smb358_charger_otg_control(
  915. struct i2c_client *client)
  916. {
  917. struct sec_charger_info *charger = i2c_get_clientdata(client);
  918. if (charger->cable_type ==
  919. POWER_SUPPLY_TYPE_BATTERY) {
  920. dev_info(&client->dev, "%s : turn off OTG\n", __func__);
  921. /* disable otg */
  922. smb358_clear_reg(client, SMB358_COMMAND_A, 0x10);
  923. } else {
  924. /* Change "OTG output current limit" to 250mA */
  925. smb358_clear_reg(client, SMB358_OTG_TLIM_THERM_CONTROL, 0x0C);
  926. /* OTG Enabled*/
  927. smb358_update_reg(client, SMB358_COMMAND_A, 0x10);
  928. dev_info(&client->dev, "%s : turn on OTG\n", __func__);
  929. smb358_set_command(client, SMB358_COMMAND_B, 0x00);
  930. /* Change "OTG output current limit" to 500mA */
  931. smb358_update_reg(client, SMB358_OTG_TLIM_THERM_CONTROL, 0x84);
  932. }
  933. }
  934. static void smb358_set_charging_current(
  935. struct i2c_client *client, int charging_current)
  936. {
  937. u8 data;
  938. smb358_clear_reg(client, SMB358_COMMAND_A, 0x02);
  939. if (!charging_current)
  940. return;
  941. smb358_i2c_read(client, SMB358_CHARGE_CURRENT, &data);
  942. data &= 0x1f;
  943. data |= smb358_get_fast_charging_current_data(charging_current);
  944. smb358_set_command(client, SMB358_CHARGE_CURRENT, data);
  945. smb358_update_reg(client, SMB358_COMMAND_A, 0x02);
  946. }
  947. static void smb358_set_charging_input_current_limit(
  948. struct i2c_client *client, int input_current_limit)
  949. {
  950. struct sec_charger_info *charger = i2c_get_clientdata(client);
  951. u8 data;
  952. /* Input current limit */
  953. data = 0;
  954. data = smb358_get_input_current_limit_data(
  955. charger, input_current_limit);
  956. smb358_set_command(client, SMB358_INPUT_CURRENTLIMIT, data);
  957. }
  958. void smb358_charger_shutdown(struct i2c_client *client)
  959. {
  960. pr_info("%s: smb358 Charging Disabled\n", __func__);
  961. smb358_set_command(client, SMB358_THERM_CONTROL_A, 0xF0);
  962. smb358_set_command(client, SMB358_COMMAND_A, 0x80);
  963. smb358_volatile_writes(client, SMB358_DISABLE_WRITE);
  964. }
  965. static int smb358_debugfs_show(struct seq_file *s, void *data)
  966. {
  967. struct sec_charger_info *charger = s->private;
  968. u8 reg;
  969. u8 reg_data;
  970. seq_printf(s, "SMB CHARGER IC :\n");
  971. seq_printf(s, "==================\n");
  972. for (reg = 0x00; reg <= 0x0E; reg++) {
  973. smb358_i2c_read(charger->client, reg, &reg_data);
  974. seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
  975. }
  976. for (reg = 0x30; reg <= 0x3F; reg++) {
  977. smb358_i2c_read(charger->client, reg, &reg_data);
  978. seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
  979. }
  980. seq_printf(s, "\n");
  981. return 0;
  982. }
  983. static int smb358_debugfs_open(struct inode *inode, struct file *file)
  984. {
  985. return single_open(file, smb358_debugfs_show, inode->i_private);
  986. }
  987. static const struct file_operations smb358_debugfs_fops = {
  988. .open = smb358_debugfs_open,
  989. .read = seq_read,
  990. .llseek = seq_lseek,
  991. .release = single_release,
  992. };
  993. bool smb358_hal_chg_init(struct i2c_client *client)
  994. {
  995. struct sec_charger_info *charger = i2c_get_clientdata(client);
  996. dev_info(&client->dev,
  997. "%s: SMB358 Charger init(Start)!!\n", __func__);
  998. smb358_volatile_writes(client, SMB358_ENABLE_WRITE);
  999. /*smb358_test_read(client);*/
  1000. (void) debugfs_create_file("smb358_regs",
  1001. S_IRUGO, NULL, (void *)charger, &smb358_debugfs_fops);
  1002. return true;
  1003. }
  1004. bool smb358_hal_chg_suspend(struct i2c_client *client)
  1005. {
  1006. return true;
  1007. }
  1008. bool smb358_hal_chg_resume(struct i2c_client *client)
  1009. {
  1010. return true;
  1011. }
  1012. bool smb358_hal_chg_get_property(struct i2c_client *client,
  1013. enum power_supply_property psp,
  1014. union power_supply_propval *val)
  1015. {
  1016. struct sec_charger_info *charger = i2c_get_clientdata(client);
  1017. u8 data;
  1018. switch (psp) {
  1019. case POWER_SUPPLY_PROP_STATUS:
  1020. val->intval = smb358_get_charging_status(client);
  1021. break;
  1022. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  1023. if (charger->is_charging) {
  1024. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  1025. if (charger->is_slow_charging) {
  1026. val->intval = POWER_SUPPLY_CHARGE_TYPE_SLOW;
  1027. pr_info("%s: slow-charging mode\n", __func__);
  1028. }
  1029. }
  1030. else
  1031. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  1032. break;
  1033. case POWER_SUPPLY_PROP_HEALTH:
  1034. val->intval = smb358_get_charging_health(client);
  1035. break;
  1036. /* calculated input current limit value */
  1037. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1038. case POWER_SUPPLY_PROP_CURRENT_AVG: /* charging current */
  1039. if (charger->charging_current) {
  1040. smb358_i2c_read(client, SMB358_STATUS_B, &data);
  1041. if (data & 0x20)
  1042. switch (data & 0x07) {
  1043. case 0:
  1044. val->intval = 100;
  1045. break;
  1046. case 1:
  1047. val->intval = 200;
  1048. break;
  1049. case 2:
  1050. val->intval = 450;
  1051. break;
  1052. case 3:
  1053. val->intval = 600;
  1054. break;
  1055. case 4:
  1056. val->intval = 900;
  1057. break;
  1058. case 5:
  1059. val->intval = 1300;
  1060. break;
  1061. case 6:
  1062. val->intval = 1500;
  1063. break;
  1064. case 7:
  1065. val->intval = 1800;
  1066. break;
  1067. }
  1068. else
  1069. switch ((data & 0x18) >> 3) {
  1070. case 0:
  1071. val->intval = 100;
  1072. break;
  1073. case 1:
  1074. val->intval = 150;
  1075. break;
  1076. case 2:
  1077. val->intval = 200;
  1078. break;
  1079. case 3:
  1080. val->intval = 250;
  1081. break;
  1082. }
  1083. } else
  1084. val->intval = 0;
  1085. dev_dbg(&client->dev,
  1086. "%s : set-current(%dmA), current now(%dmA)\n",
  1087. __func__, charger->charging_current, val->intval);
  1088. break;
  1089. default:
  1090. return false;
  1091. }
  1092. return true;
  1093. }
  1094. bool smb358_hal_chg_set_property(struct i2c_client *client,
  1095. enum power_supply_property psp,
  1096. const union power_supply_propval *val)
  1097. {
  1098. struct sec_charger_info *charger = i2c_get_clientdata(client);
  1099. switch (psp) {
  1100. /* val->intval : type */
  1101. case POWER_SUPPLY_PROP_ONLINE:
  1102. if (val->intval == POWER_SUPPLY_TYPE_POWER_SHARING) {
  1103. union power_supply_propval ps_status;
  1104. psy_do_property("ps", get,
  1105. POWER_SUPPLY_PROP_STATUS, ps_status);
  1106. if (ps_status.intval) {
  1107. charger->cable_type = POWER_SUPPLY_TYPE_OTG;
  1108. pr_info("%s: ps enable\n", __func__);
  1109. } else {
  1110. charger->cable_type = POWER_SUPPLY_TYPE_BATTERY;
  1111. pr_info("%s: ps disable\n", __func__);
  1112. }
  1113. }
  1114. #if defined(CONFIG_MUIC_SUPPORT_MULTIMEDIA_DOCK)
  1115. if(charger->is_mdock) {
  1116. if(charger->is_smartotg)
  1117. charger->cable_type = POWER_SUPPLY_TYPE_SMART_OTG;
  1118. else
  1119. charger->cable_type = POWER_SUPPLY_TYPE_MDOCK_TA;
  1120. pr_debug("%s: cable type %d\n", __func__, charger->cable_type);
  1121. }
  1122. #endif
  1123. if (charger->cable_type == POWER_SUPPLY_TYPE_OTG) {
  1124. smb358_charger_otg_control(client);
  1125. } else if (charger->cable_type == POWER_SUPPLY_TYPE_BATTERY) {
  1126. smb358_charger_function_control(client);
  1127. smb358_charger_otg_control(client);
  1128. } else {
  1129. smb358_charger_function_control(client);
  1130. }
  1131. /* smb358_test_read(client); */
  1132. break;
  1133. case POWER_SUPPLY_PROP_CURRENT_MAX: /* input current limit set */
  1134. /* calculated input current limit value */
  1135. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1136. smb358_set_charging_input_current_limit(client, val->intval);
  1137. break;
  1138. /* val->intval : charging current */
  1139. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1140. smb358_set_charging_current(client, val->intval);
  1141. break;
  1142. default:
  1143. return false;
  1144. }
  1145. return true;
  1146. }
  1147. ssize_t smb358_hal_chg_show_attrs(struct device *dev,
  1148. const ptrdiff_t offset, char *buf)
  1149. {
  1150. struct power_supply *psy = dev_get_drvdata(dev);
  1151. struct sec_charger_info *chg =
  1152. container_of(psy, struct sec_charger_info, psy_chg);
  1153. int i = 0;
  1154. char *str = NULL;
  1155. switch (offset) {
  1156. case CHG_DATA:
  1157. i += scnprintf(buf + i, PAGE_SIZE - i, "%x\n",
  1158. chg->reg_data);
  1159. break;
  1160. case CHG_REGS:
  1161. str = kzalloc(sizeof(char)*1024, GFP_KERNEL);
  1162. if (!str)
  1163. return -ENOMEM;
  1164. smb358_read_regs(chg->client, str);
  1165. i += scnprintf(buf + i, PAGE_SIZE - i, "%s\n",
  1166. str);
  1167. kfree(str);
  1168. break;
  1169. default:
  1170. i = -EINVAL;
  1171. break;
  1172. }
  1173. return i;
  1174. }
  1175. ssize_t smb358_hal_chg_store_attrs(struct device *dev,
  1176. const ptrdiff_t offset,
  1177. const char *buf, size_t count)
  1178. {
  1179. struct power_supply *psy = dev_get_drvdata(dev);
  1180. struct sec_charger_info *chg =
  1181. container_of(psy, struct sec_charger_info, psy_chg);
  1182. int ret = 0;
  1183. int x = 0;
  1184. u8 data = 0;
  1185. switch (offset) {
  1186. case CHG_REG:
  1187. if (sscanf(buf, "%x\n", &x) == 1) {
  1188. chg->reg_addr = x;
  1189. smb358_i2c_read(chg->client,
  1190. chg->reg_addr, &data);
  1191. chg->reg_data = data;
  1192. dev_dbg(dev, "%s: (read) addr = 0x%x, data = 0x%x\n",
  1193. __func__, chg->reg_addr, chg->reg_data);
  1194. ret = count;
  1195. }
  1196. break;
  1197. case CHG_DATA:
  1198. if (sscanf(buf, "%x\n", &x) == 1) {
  1199. data = (u8)x;
  1200. dev_dbg(dev, "%s: (write) addr = 0x%x, data = 0x%x\n",
  1201. __func__, chg->reg_addr, data);
  1202. smb358_i2c_write(chg->client,
  1203. chg->reg_addr, &data);
  1204. ret = count;
  1205. }
  1206. break;
  1207. default:
  1208. ret = -EINVAL;
  1209. break;
  1210. }
  1211. return ret;
  1212. }
  1213. static struct device_attribute smb358_charger_attrs[] = {
  1214. SMB358_CHARGER_ATTR(reg),
  1215. SMB358_CHARGER_ATTR(data),
  1216. SMB358_CHARGER_ATTR(regs),
  1217. };
  1218. static enum power_supply_property smb358_charger_props[] = {
  1219. POWER_SUPPLY_PROP_STATUS,
  1220. POWER_SUPPLY_PROP_CHARGE_TYPE,
  1221. POWER_SUPPLY_PROP_HEALTH,
  1222. POWER_SUPPLY_PROP_ONLINE,
  1223. POWER_SUPPLY_PROP_CURRENT_MAX,
  1224. POWER_SUPPLY_PROP_CURRENT_AVG,
  1225. POWER_SUPPLY_PROP_CURRENT_NOW,
  1226. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  1227. };
  1228. static int smb358_chg_get_property(struct power_supply *psy,
  1229. enum power_supply_property psp,
  1230. union power_supply_propval *val)
  1231. {
  1232. struct sec_charger_info *charger =
  1233. container_of(psy, struct sec_charger_info, psy_chg);
  1234. u8 data = 0;
  1235. switch (psp) {
  1236. case POWER_SUPPLY_PROP_CURRENT_MAX: /* input current limit set */
  1237. smb358_i2c_read(charger->client, SMB358_STATUS_E, &data);
  1238. if (data & 0x10) {
  1239. int aicl_result = smb358_get_aicl_current(data);
  1240. dev_info(&charger->client->dev,
  1241. "%s : AICL completed (%dmA)\n", __func__, aicl_result);
  1242. charger->charging_current_max = aicl_result;
  1243. } else {
  1244. dev_info(&charger->client->dev,
  1245. "%s : AICL is not completed \n", __func__);
  1246. charger->charging_current_max = 300;
  1247. }
  1248. val->intval = charger->charging_current_max;
  1249. break;
  1250. case POWER_SUPPLY_PROP_ONLINE:
  1251. case POWER_SUPPLY_PROP_STATUS:
  1252. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  1253. case POWER_SUPPLY_PROP_HEALTH:
  1254. case POWER_SUPPLY_PROP_CURRENT_AVG: /* charging current */
  1255. /* calculated input current limit value */
  1256. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1257. if (!smb358_hal_chg_get_property(charger->client, psp, val))
  1258. return -EINVAL;
  1259. break;
  1260. default:
  1261. return -EINVAL;
  1262. }
  1263. return 0;
  1264. }
  1265. static int smb358_chg_set_property(struct power_supply *psy,
  1266. enum power_supply_property psp,
  1267. const union power_supply_propval *val)
  1268. {
  1269. struct sec_charger_info *charger =
  1270. container_of(psy, struct sec_charger_info, psy_chg);
  1271. union power_supply_propval input_value;
  1272. switch (psp) {
  1273. case POWER_SUPPLY_PROP_STATUS:
  1274. charger->status = val->intval;
  1275. break;
  1276. /* val->intval : type */
  1277. case POWER_SUPPLY_PROP_ONLINE:
  1278. charger->cable_type = val->intval;
  1279. if (val->intval == POWER_SUPPLY_TYPE_BATTERY || \
  1280. val->intval == POWER_SUPPLY_TYPE_OTG || \
  1281. val->intval == POWER_SUPPLY_TYPE_POWER_SHARING) {
  1282. charger->is_charging = false;
  1283. charger->is_slow_charging = false;
  1284. #if defined(CONFIG_MUIC_SUPPORT_MULTIMEDIA_DOCK)
  1285. charger->is_mdock = false;
  1286. charger->is_smartotg = false;
  1287. #endif
  1288. }
  1289. else {
  1290. charger->is_charging = true;
  1291. #if defined(CONFIG_MUIC_SUPPORT_MULTIMEDIA_DOCK)
  1292. if(val->intval == POWER_SUPPLY_TYPE_SMART_NOTG)
  1293. charger->is_smartotg = false;
  1294. else if (val->intval == POWER_SUPPLY_TYPE_SMART_OTG)
  1295. charger->is_smartotg = true;
  1296. if (val->intval == POWER_SUPPLY_TYPE_MDOCK_TA)
  1297. charger->is_mdock = true;
  1298. #endif
  1299. }
  1300. if (!smb358_hal_chg_set_property(charger->client, psp, val))
  1301. return -EINVAL;
  1302. break;
  1303. /* val->intval : input current limit set */
  1304. case POWER_SUPPLY_PROP_CURRENT_MAX:
  1305. charger->charging_current_max = val->intval;
  1306. /* to control charging current,
  1307. * use input current limit and set charging current as much as possible
  1308. * so we only control input current limit to control charge current
  1309. */
  1310. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1311. if (!smb358_hal_chg_set_property(charger->client, psp, val))
  1312. return -EINVAL;
  1313. break;
  1314. /* val->intval : charging current */
  1315. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1316. charger->charging_current = val->intval;
  1317. if (!smb358_hal_chg_set_property(charger->client, psp, val))
  1318. return -EINVAL;
  1319. break;
  1320. /* val->intval : SIOP level (%)
  1321. * SIOP charging current setting
  1322. */
  1323. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  1324. charger->siop_level = val->intval;
  1325. if (charger->is_charging) {
  1326. /* change val as charging current by SIOP level
  1327. * do NOT change initial charging current setting
  1328. */
  1329. input_value.intval =
  1330. charger->pdata->charging_current[
  1331. charger->cable_type].fast_charging_current * val->intval / 100;
  1332. /* charging current should be over than USB charging current */
  1333. if (charger->pdata->chg_functions_setting &
  1334. SEC_CHARGER_MINIMUM_SIOP_CHARGING_CURRENT) {
  1335. if (input_value.intval > 0 &&
  1336. input_value.intval <
  1337. charger->pdata->charging_current[
  1338. POWER_SUPPLY_TYPE_USB].fast_charging_current)
  1339. input_value.intval =
  1340. charger->pdata->charging_current[
  1341. POWER_SUPPLY_TYPE_USB].fast_charging_current;
  1342. }
  1343. /* set charging current as new value */
  1344. if (!smb358_hal_chg_set_property(charger->client,
  1345. POWER_SUPPLY_PROP_CURRENT_AVG, &input_value))
  1346. return -EINVAL;
  1347. }
  1348. break;
  1349. default:
  1350. return -EINVAL;
  1351. }
  1352. return 0;
  1353. }
  1354. static void smb358_chg_isr_work(struct work_struct *work)
  1355. {
  1356. struct sec_charger_info *charger =
  1357. container_of(work, struct sec_charger_info, isr_work.work);
  1358. union power_supply_propval val;
  1359. int full_check_type;
  1360. dev_info(&charger->client->dev,
  1361. "%s: Charger Interrupt\n", __func__);
  1362. psy_do_property("battery", get,
  1363. POWER_SUPPLY_PROP_CHARGE_NOW, val);
  1364. if (val.intval == SEC_BATTERY_CHARGING_1ST)
  1365. full_check_type = charger->pdata->full_check_type;
  1366. else
  1367. full_check_type = charger->pdata->full_check_type_2nd;
  1368. if (full_check_type == SEC_BATTERY_FULLCHARGED_CHGINT) {
  1369. if (!smb358_hal_chg_get_property(charger->client,
  1370. POWER_SUPPLY_PROP_STATUS, &val))
  1371. return;
  1372. switch (val.intval) {
  1373. case POWER_SUPPLY_STATUS_DISCHARGING:
  1374. dev_err(&charger->client->dev,
  1375. "%s: Interrupted but Discharging\n", __func__);
  1376. break;
  1377. case POWER_SUPPLY_STATUS_NOT_CHARGING:
  1378. dev_err(&charger->client->dev,
  1379. "%s: Interrupted but NOT Charging\n", __func__);
  1380. break;
  1381. case POWER_SUPPLY_STATUS_FULL:
  1382. dev_info(&charger->client->dev,
  1383. "%s: Interrupted by Full\n", __func__);
  1384. psy_do_property("battery", set,
  1385. POWER_SUPPLY_PROP_STATUS, val);
  1386. break;
  1387. case POWER_SUPPLY_STATUS_CHARGING:
  1388. dev_err(&charger->client->dev,
  1389. "%s: Interrupted but Charging\n", __func__);
  1390. break;
  1391. case POWER_SUPPLY_STATUS_UNKNOWN:
  1392. default:
  1393. dev_err(&charger->client->dev,
  1394. "%s: Invalid Charger Status\n", __func__);
  1395. break;
  1396. }
  1397. }
  1398. if (charger->pdata->ovp_uvlo_check_type ==
  1399. SEC_BATTERY_OVP_UVLO_CHGINT) {
  1400. if (!smb358_hal_chg_get_property(charger->client,
  1401. POWER_SUPPLY_PROP_HEALTH, &val))
  1402. return;
  1403. switch (val.intval) {
  1404. case POWER_SUPPLY_HEALTH_OVERHEAT:
  1405. case POWER_SUPPLY_HEALTH_COLD:
  1406. dev_err(&charger->client->dev,
  1407. "%s: Interrupted but Hot/Cold\n", __func__);
  1408. break;
  1409. case POWER_SUPPLY_HEALTH_DEAD:
  1410. dev_err(&charger->client->dev,
  1411. "%s: Interrupted but Dead\n", __func__);
  1412. break;
  1413. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  1414. case POWER_SUPPLY_HEALTH_UNDERVOLTAGE:
  1415. dev_info(&charger->client->dev,
  1416. "%s: Interrupted by OVP/UVLO\n", __func__);
  1417. /* Do not set POWER_SUPPLY_PROP_HEALTH
  1418. * excute monitor work again.
  1419. * ovp/uvlo is checked by polling
  1420. */
  1421. psy_do_property("battery", set,
  1422. POWER_SUPPLY_PROP_CHARGE_TYPE, val);
  1423. break;
  1424. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  1425. dev_err(&charger->client->dev,
  1426. "%s: Interrupted but Unspec\n", __func__);
  1427. break;
  1428. case POWER_SUPPLY_HEALTH_GOOD:
  1429. dev_err(&charger->client->dev,
  1430. "%s: Interrupted but Good\n", __func__);
  1431. /* Do not set POWER_SUPPLY_PROP_HEALTH
  1432. * excute monitor work again.
  1433. * ovp/uvlo is checked by polling
  1434. */
  1435. psy_do_property("battery", set,
  1436. POWER_SUPPLY_PROP_CHARGE_TYPE, val);
  1437. break;
  1438. case POWER_SUPPLY_HEALTH_UNKNOWN:
  1439. default:
  1440. dev_err(&charger->client->dev,
  1441. "%s: Invalid Charger Health\n", __func__);
  1442. break;
  1443. }
  1444. }
  1445. if (charger->pdata->cable_check_type & SEC_BATTERY_CABLE_CHECK_CHGINT) {
  1446. if (!smb358_hal_chg_get_property(charger->client,
  1447. POWER_SUPPLY_PROP_ONLINE, &val))
  1448. return;
  1449. /* use SEC_BATTERY_CABLE_SOURCE_EXTERNAL for cable_source_type
  1450. * charger would call battery driver to set ONLINE property
  1451. * check battery driver loaded or not
  1452. */
  1453. if (get_power_supply_by_name("battery")) {
  1454. psy_do_property("battery", set,
  1455. POWER_SUPPLY_PROP_ONLINE, val);
  1456. } else {
  1457. if (charger->pdata->check_cable_result_callback)
  1458. charger->pdata->check_cable_result_callback(val.intval);
  1459. }
  1460. }
  1461. }
  1462. static irqreturn_t smb358_chg_irq_thread(int irq, void *irq_data)
  1463. {
  1464. struct sec_charger_info *charger = irq_data;
  1465. schedule_delayed_work(&charger->isr_work, 0);
  1466. return IRQ_HANDLED;
  1467. }
  1468. static int smb358_chg_create_attrs(struct device *dev)
  1469. {
  1470. int i, rc;
  1471. for (i = 0; i < ARRAY_SIZE(smb358_charger_attrs); i++) {
  1472. rc = device_create_file(dev, &smb358_charger_attrs[i]);
  1473. if (rc)
  1474. goto create_attrs_failed;
  1475. }
  1476. goto create_attrs_succeed;
  1477. create_attrs_failed:
  1478. dev_err(dev, "%s: failed (%d)\n", __func__, rc);
  1479. while (i--)
  1480. device_remove_file(dev, &smb358_charger_attrs[i]);
  1481. create_attrs_succeed:
  1482. return rc;
  1483. }
  1484. ssize_t smb358_chg_show_attrs(struct device *dev,
  1485. struct device_attribute *attr, char *buf)
  1486. {
  1487. const ptrdiff_t offset = attr - smb358_charger_attrs;
  1488. int i = 0;
  1489. switch (offset) {
  1490. case CHG_REG:
  1491. case CHG_DATA:
  1492. case CHG_REGS:
  1493. i = smb358_hal_chg_show_attrs(dev, offset, buf);
  1494. break;
  1495. default:
  1496. i = -EINVAL;
  1497. break;
  1498. }
  1499. return i;
  1500. }
  1501. ssize_t smb358_chg_store_attrs(struct device *dev,
  1502. struct device_attribute *attr,
  1503. const char *buf, size_t count)
  1504. {
  1505. const ptrdiff_t offset = attr - smb358_charger_attrs;
  1506. int ret = 0;
  1507. switch (offset) {
  1508. case CHG_REG:
  1509. case CHG_DATA:
  1510. ret = smb358_hal_chg_store_attrs(dev, offset, buf, count);
  1511. break;
  1512. default:
  1513. ret = -EINVAL;
  1514. break;
  1515. }
  1516. return ret;
  1517. }
  1518. #ifdef CONFIG_OF
  1519. static int smb358_charger_read_u32_index_dt(const struct device_node *np,
  1520. const char *propname,
  1521. u32 index, u32 *out_value)
  1522. {
  1523. struct property *prop = of_find_property(np, propname, NULL);
  1524. u32 len = (index + 1) * sizeof(*out_value);
  1525. if (!prop)
  1526. return (-EINVAL);
  1527. if (!prop->value)
  1528. return (-ENODATA);
  1529. if (len > prop->length)
  1530. return (-EOVERFLOW);
  1531. *out_value = be32_to_cpup(((__be32 *)prop->value) + index);
  1532. return 0;
  1533. }
  1534. extern unsigned int system_rev;
  1535. static int smb358_charger_parse_dt(struct sec_charger_info *charger)
  1536. {
  1537. struct device_node *np = of_find_node_by_name(NULL, "charger");
  1538. sec_battery_platform_data_t *pdata = charger->pdata;
  1539. int ret = 0;
  1540. int i, len;
  1541. const u32 *p;
  1542. if (np == NULL) {
  1543. pr_err("%s np NULL\n", __func__);
  1544. return -1;
  1545. } else {
  1546. #if defined(CONFIG_MACH_VIENNAVZW) || defined(CONFIG_MACH_VIENNAATT)
  1547. if (system_rev >= 0xC)
  1548. pdata->vbus_ctrl_gpio = 28;
  1549. else
  1550. pdata->vbus_ctrl_gpio = 0;
  1551. pr_info("%s reading vbus_ctrl_gpio = %d\n",
  1552. __func__, pdata->vbus_ctrl_gpio);
  1553. #else
  1554. ret = of_get_named_gpio(np, "battery,vbus_ctrl_gpio", 0);
  1555. if (ret > 0) {
  1556. pdata->vbus_ctrl_gpio = ret;
  1557. pr_info("%s reading vbus_ctrl_gpio = %d\n", __func__, ret);
  1558. } else {
  1559. pdata->vbus_ctrl_gpio = 0;
  1560. pr_info("%s vbus_ctrl_gpio read fail\n", __func__);
  1561. }
  1562. #endif
  1563. ret = of_property_read_u32(np, "battery,chg_float_voltage",
  1564. &pdata->chg_float_voltage);
  1565. if (ret < 0)
  1566. pr_err("%s: chg_float_voltage read failed (%d)\n", __func__, ret);
  1567. ret = of_property_read_u32(np, "battery,ovp_uvlo_check_type",
  1568. &pdata->ovp_uvlo_check_type);
  1569. if (ret < 0)
  1570. pr_err("%s: ovp_uvlo_check_type read failed (%d)\n", __func__, ret);
  1571. ret = of_get_named_gpio(np, "battery,chg_int", 0);
  1572. if (ret > 0) {
  1573. pdata->chg_irq = gpio_to_irq(ret);
  1574. pr_info("%s reading chg_int_gpio = %d\n", __func__, ret);
  1575. } else {
  1576. pr_info("%s reading chg_int_gpio is empty\n", __func__);
  1577. }
  1578. ret = of_property_read_u32(np, "battery,chg_irq_attr",
  1579. (unsigned int *)&pdata->chg_irq_attr);
  1580. if (ret)
  1581. pr_info("%s: chg_irq_attr is Empty\n", __func__);
  1582. ret = of_property_read_u32(np, "battery,full_check_type",
  1583. &pdata->full_check_type);
  1584. if (ret < 0)
  1585. pr_err("%s: full_check_type read failed (%d)\n", __func__, ret);
  1586. p = of_get_property(np, "battery,input_current_limit", &len);
  1587. len = len / sizeof(u32);
  1588. pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len,
  1589. GFP_KERNEL);
  1590. for(i = 0; i < len; i++) {
  1591. ret = smb358_charger_read_u32_index_dt(np,
  1592. "battery,input_current_limit", i,
  1593. &pdata->charging_current[i].input_current_limit);
  1594. ret = smb358_charger_read_u32_index_dt(np,
  1595. "battery,fast_charging_current", i,
  1596. &pdata->charging_current[i].fast_charging_current);
  1597. ret = smb358_charger_read_u32_index_dt(np,
  1598. "battery,full_check_current_1st", i,
  1599. &pdata->charging_current[i].full_check_current_1st);
  1600. ret = smb358_charger_read_u32_index_dt(np,
  1601. "battery,full_check_current_2nd", i,
  1602. &pdata->charging_current[i].full_check_current_2nd);
  1603. }
  1604. }
  1605. return ret;
  1606. }
  1607. #else
  1608. static int smb358_charger_parse_dt(struct max77803_charger_data *charger)
  1609. {
  1610. return 0;
  1611. }
  1612. #endif
  1613. static int __devinit smb358_charger_probe(
  1614. struct i2c_client *client,
  1615. const struct i2c_device_id *id)
  1616. {
  1617. struct i2c_adapter *adapter =
  1618. to_i2c_adapter(client->dev.parent);
  1619. struct sec_charger_info *charger;
  1620. int ret = 0;
  1621. dev_info(&client->dev,
  1622. "%s: SMB358 Charger Driver Loading\n", __func__);
  1623. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  1624. return -EIO;
  1625. charger = kzalloc(sizeof(*charger), GFP_KERNEL);
  1626. if (!charger)
  1627. return -ENOMEM;
  1628. charger->client = client;
  1629. if (client->dev.of_node) {
  1630. void * pdata = kzalloc(sizeof(sec_battery_platform_data_t), GFP_KERNEL);
  1631. if (!pdata)
  1632. goto err_free1;
  1633. charger->pdata = pdata;
  1634. if (smb358_charger_parse_dt(charger))
  1635. dev_err(&client->dev,
  1636. "%s : Failed to get charger dt\n", __func__);
  1637. } else
  1638. charger->pdata = client->dev.platform_data;
  1639. i2c_set_clientdata(client, charger);
  1640. charger->siop_level = 100;
  1641. charger->psy_chg.name = "smb358";
  1642. charger->psy_chg.type = POWER_SUPPLY_TYPE_UNKNOWN;
  1643. charger->psy_chg.get_property = smb358_chg_get_property;
  1644. charger->psy_chg.set_property = smb358_chg_set_property;
  1645. charger->psy_chg.properties = smb358_charger_props;
  1646. charger->psy_chg.num_properties = ARRAY_SIZE(smb358_charger_props);
  1647. charger->is_slow_charging = false;
  1648. if (charger->pdata->chg_gpio_init) {
  1649. if (!charger->pdata->chg_gpio_init()) {
  1650. dev_err(&client->dev,
  1651. "%s: Failed to Initialize GPIO\n", __func__);
  1652. goto err_free;
  1653. }
  1654. }
  1655. if (!smb358_hal_chg_init(charger->client)) {
  1656. dev_err(&client->dev,
  1657. "%s: Failed to Initialize Charger\n", __func__);
  1658. goto err_free;
  1659. }
  1660. ret = power_supply_register(&client->dev, &charger->psy_chg);
  1661. if (ret) {
  1662. dev_err(&client->dev,
  1663. "%s: Failed to Register psy_chg\n", __func__);
  1664. goto err_free;
  1665. }
  1666. INIT_DELAYED_WORK_DEFERRABLE(&charger->slow_work,
  1667. smb358_check_slow_charging);
  1668. if (charger->pdata->chg_irq) {
  1669. INIT_DELAYED_WORK_DEFERRABLE(
  1670. &charger->isr_work, smb358_chg_isr_work);
  1671. ret = request_threaded_irq(charger->pdata->chg_irq,
  1672. NULL, smb358_chg_irq_thread,
  1673. charger->pdata->chg_irq_attr,
  1674. "charger-irq", charger);
  1675. if (ret) {
  1676. dev_err(&client->dev,
  1677. "%s: Failed to Reqeust IRQ\n", __func__);
  1678. goto err_supply_unreg;
  1679. }
  1680. ret = enable_irq_wake(charger->pdata->chg_irq);
  1681. if (ret < 0)
  1682. dev_err(&client->dev,
  1683. "%s: Failed to Enable Wakeup Source(%d)\n",
  1684. __func__, ret);
  1685. }
  1686. ret = smb358_chg_create_attrs(charger->psy_chg.dev);
  1687. if (ret) {
  1688. dev_err(&client->dev,
  1689. "%s : Failed to create_attrs\n", __func__);
  1690. goto err_req_irq;
  1691. }
  1692. dev_dbg(&client->dev,
  1693. "%s: SMB358 Charger Driver Loaded\n", __func__);
  1694. return 0;
  1695. err_req_irq:
  1696. if (charger->pdata->chg_irq)
  1697. free_irq(charger->pdata->chg_irq, charger);
  1698. err_supply_unreg:
  1699. power_supply_unregister(&charger->psy_chg);
  1700. err_free:
  1701. kfree(charger->pdata);
  1702. err_free1:
  1703. kfree(charger);
  1704. return ret;
  1705. }
  1706. static int __devexit smb358_charger_remove(
  1707. struct i2c_client *client)
  1708. {
  1709. return 0;
  1710. }
  1711. static int smb358_charger_suspend(struct i2c_client *client,
  1712. pm_message_t state)
  1713. {
  1714. if (!smb358_hal_chg_suspend(client))
  1715. dev_err(&client->dev,
  1716. "%s: Failed to Suspend Charger\n", __func__);
  1717. return 0;
  1718. }
  1719. static int smb358_charger_resume(struct i2c_client *client)
  1720. {
  1721. dev_info(&client->dev,"%s: start\n", __func__);
  1722. if (!smb358_hal_chg_resume(client))
  1723. dev_err(&client->dev,
  1724. "%s: Failed to Resume Charger\n", __func__);
  1725. return 0;
  1726. }
  1727. static const struct i2c_device_id smb358_charger_id[] = {
  1728. {"smb358", 0},
  1729. {}
  1730. };
  1731. MODULE_DEVICE_TABLE(i2c, smb358_charger_id);
  1732. static struct of_device_id smb358_i2c_match_table[] = {
  1733. { .compatible = "smb358,i2c", },
  1734. { },
  1735. };
  1736. MODULE_DEVICE_TABLE(i2c, smb358_i2c_match_table);
  1737. static struct i2c_driver smb358_charger_driver = {
  1738. .driver = {
  1739. .name = "smb358",
  1740. .owner = THIS_MODULE,
  1741. .of_match_table = smb358_i2c_match_table,
  1742. },
  1743. .probe = smb358_charger_probe,
  1744. .remove = __devexit_p(smb358_charger_remove),
  1745. .suspend = smb358_charger_suspend,
  1746. .resume = smb358_charger_resume,
  1747. .shutdown = smb358_charger_shutdown,
  1748. .id_table = smb358_charger_id,
  1749. };
  1750. static int __init smb358_charger_init(void)
  1751. {
  1752. return i2c_add_driver(&smb358_charger_driver);
  1753. }
  1754. static void __exit smb358_charger_exit(void)
  1755. {
  1756. i2c_del_driver(&smb358_charger_driver);
  1757. }
  1758. module_init(smb358_charger_init);
  1759. module_exit(smb358_charger_exit);
  1760. MODULE_DESCRIPTION("Samsung SMB358 Charger Driver");
  1761. MODULE_AUTHOR("Samsung Electronics");
  1762. MODULE_LICENSE("GPL");