act8xxx.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /*
  2. * PMU driver for ACT8xxx
  3. *
  4. * Copyright (c) 2010-2011 Amlogic Ltd.
  5. * Elvis Yu <elvis.yu@amlogic.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.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/param.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/delay.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/idr.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #include <asm/unaligned.h>
  22. #include <linux/cdev.h>
  23. #include <linux/fs.h>
  24. #include <linux/fcntl.h>
  25. #include <asm/uaccess.h>
  26. #include <mach/am_regs.h>
  27. #include <mach/pinmux.h>
  28. #include <mach/gpio.h>
  29. #include <linux/saradc.h>
  30. #include <linux/act8xxx.h>
  31. #define DRIVER_VERSION "0.1.0"
  32. static int dbg_enable = 0;
  33. static int refresh = 0;
  34. #define act8xxx_dbg(level, fmt, args...) { if(level) \
  35. printk("[goodix]: " fmt, ## args); }
  36. static int usb_status = 0;
  37. static int new_usb_status = 0;
  38. /* If the system has several batteries we need a different name for each
  39. * of them...
  40. */
  41. static DEFINE_IDR(pmu_id);
  42. static DEFINE_MUTEX(pmu_mutex);
  43. struct act8xxx_device_info *act8xxx_dev; //Elvis Fool
  44. #ifdef CONFIG_PMU_ACT8942
  45. struct act8942_operations* act8942_opts = NULL;
  46. #endif
  47. static dev_t act8xxx_devno;
  48. typedef struct pmu_dev_s {
  49. /* ... */
  50. struct cdev cdev; /* The cdev structure */
  51. } pmu_dev_t;
  52. static pmu_dev_t *act8xxx_pmu_dev = NULL;
  53. struct act8xxx_device_info {
  54. struct device *dev;
  55. int id;
  56. struct i2c_client *client;
  57. #ifdef CONFIG_PMU_ACT8942
  58. struct power_supply bat;
  59. struct power_supply ac;
  60. struct power_supply usb;
  61. struct timer_list polling_timer;
  62. struct work_struct work_update;
  63. int ac_status;
  64. int capacity;
  65. int bat_status;
  66. #endif
  67. };
  68. static struct i2c_client *this_client = NULL;
  69. static int act8xxx_read_i2c(struct i2c_client *client, u8 reg, u8 *val);
  70. static int act8xxx_write_i2c(struct i2c_client *client, u8 reg, u8 *val);
  71. #ifdef CONFIG_USB_ANDROID
  72. #ifdef CONFIG_PMU_ACT8942
  73. int pc_connect(int status)
  74. {
  75. new_usb_status = status;
  76. if(new_usb_status == usb_status)
  77. return 1;
  78. usb_status = new_usb_status;
  79. power_supply_changed(&act8xxx_dev->usb);
  80. return 0;
  81. }
  82. EXPORT_SYMBOL(pc_connect);
  83. #endif
  84. #endif
  85. /*
  86. * ACINSTAT
  87. * ACIN Status. Indicates the state of the ACIN input, typically
  88. * in order to identify the type of input supply connected. Value
  89. * is 1 when ACIN is above the 1.2V precision threshold, value
  90. * is 0 when ACIN is below this threshold.
  91. */
  92. static inline int is_ac_online(void)
  93. {
  94. u8 val;
  95. act8xxx_read_i2c(this_client, (ACT8942_APCH_ADDR+0xa), &val);
  96. logd("%s: get from pmu is %d.\n", __FUNCTION__, val);
  97. return (val & 0x2) ? 1 : 0;
  98. }
  99. static inline int is_usb_online(void)
  100. {
  101. return usb_status;
  102. }
  103. /*
  104. * Charging Status Indication
  105. *
  106. * CSTATE[1] CSTATE[0] STATE MACHINE STATUS
  107. *
  108. * 1 1 PRECONDITION State
  109. * 1 0 FAST-CHARGE / TOP-OFF State
  110. * 0 1 END-OF-CHARGE State
  111. * 0 0 SUSPEND/DISABLED / FAULT State
  112. *
  113. */
  114. static inline int get_charge_status(void)
  115. {
  116. u8 val;
  117. act8xxx_read_i2c(this_client, (ACT8942_APCH_ADDR+0xa), &val);
  118. logd("%s: get from pmu is %d.\n", __FUNCTION__, val);
  119. return ((val>>4) & 0x3);
  120. }
  121. //export this interface for other driver
  122. int act8xxx_is_ac_online(void)
  123. {
  124. if(this_client == NULL)
  125. {
  126. pr_err("act8xxx: have not init now, wait.... \n");
  127. return 1;
  128. }
  129. return is_ac_online();
  130. }
  131. EXPORT_SYMBOL(act8xxx_is_ac_online);
  132. //export this interface for other driver
  133. void act8xxx_set_outn_poweroff(int outn)
  134. {
  135. u8 val;
  136. u32 out_on_reg[] = {ACT8xxx_REG1_ADDR + 2,
  137. ACT8xxx_REG2_ADDR + 2,
  138. ACT8xxx_REG3_ADDR + 2,
  139. ACT8xxx_REG4_ADDR + 1,
  140. ACT8xxx_REG5_ADDR + 1,
  141. ACT8xxx_REG6_ADDR + 1,
  142. ACT8xxx_REG7_ADDR + 1};
  143. u32 reg = 0;
  144. if(this_client != NULL)
  145. {
  146. if ((outn >= ACT8xxx_REG1) && (outn <= ACT8xxx_REG7))
  147. {
  148. reg = out_on_reg[outn - ACT8xxx_REG1];
  149. act8xxx_read_i2c(this_client, reg, &val);
  150. //set ON bit to 1
  151. val |= (1<<7);
  152. act8xxx_write_i2c(this_client, reg, &val);
  153. //set ON bit to 0
  154. val &= ~(1<<7);
  155. act8xxx_write_i2c(this_client, reg, &val);
  156. }
  157. else
  158. {
  159. pr_err("act8xxx: Wrong REG number!%d REG number in [1:7]\n", outn);
  160. }
  161. }
  162. else
  163. {
  164. pr_err("act8xxx: have not init now, wait.... \n");
  165. }
  166. }
  167. EXPORT_SYMBOL(act8xxx_set_outn_poweroff);
  168. #ifdef CONFIG_PMU_ACT8942
  169. static inline int get_bat_status(void)
  170. {
  171. int ret,status;
  172. if(act8942_opts->is_ac_online()){
  173. status = act8942_opts->get_charge_status();
  174. if(act8942_opts->get_charge_status == get_charge_status){
  175. if(status == 0x1){
  176. ret = POWER_SUPPLY_STATUS_FULL;
  177. }
  178. else if(status == 0x0){
  179. ret = POWER_SUPPLY_STATUS_NOT_CHARGING;
  180. }
  181. else{
  182. ret = POWER_SUPPLY_STATUS_CHARGING;
  183. }
  184. }
  185. else{//if(act8942_opts->get_charge_status == get_charge_status)
  186. if(status == 0x1){
  187. ret = POWER_SUPPLY_STATUS_FULL;
  188. }
  189. else if(status == 0x0){
  190. ret = POWER_SUPPLY_STATUS_CHARGING;
  191. }
  192. }
  193. }//if(act8942_opts->is_ac_online())
  194. else{
  195. ret = POWER_SUPPLY_STATUS_DISCHARGING;
  196. }
  197. return ret;
  198. }
  199. static enum power_supply_property bat_power_props[] = {
  200. POWER_SUPPLY_PROP_STATUS,
  201. POWER_SUPPLY_PROP_HEALTH,
  202. POWER_SUPPLY_PROP_PRESENT,
  203. POWER_SUPPLY_PROP_TECHNOLOGY,
  204. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  205. POWER_SUPPLY_PROP_CURRENT_NOW,
  206. POWER_SUPPLY_PROP_CAPACITY,
  207. POWER_SUPPLY_PROP_TEMP,
  208. // POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  209. // POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  210. // POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  211. };
  212. static enum power_supply_property ac_power_props[] = {
  213. POWER_SUPPLY_PROP_ONLINE,
  214. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  215. };
  216. static enum power_supply_property usb_power_props[] = {
  217. POWER_SUPPLY_PROP_ONLINE,
  218. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  219. };
  220. inline static int measure_capacity_advanced(void);
  221. #define to_act8942_device_info(x) container_of((x), \
  222. struct act8xxx_device_info, bat);
  223. static int bat_power_get_property(struct power_supply *psy,
  224. enum power_supply_property psp,
  225. union power_supply_propval *val)
  226. {
  227. int ret = 0;
  228. u8 status;
  229. //struct act8xxx_device_info *act8942_dev = to_act8942_device_info(psy);
  230. switch (psp)
  231. {
  232. case POWER_SUPPLY_PROP_STATUS:
  233. val->intval = act8xxx_dev->bat_status;
  234. break;
  235. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  236. val->intval = act8942_opts->measure_voltage();
  237. break;
  238. case POWER_SUPPLY_PROP_PRESENT:
  239. val->intval = val->intval <= 0 ? 0 : 1;
  240. break;
  241. case POWER_SUPPLY_PROP_CURRENT_NOW:
  242. val->intval = act8942_opts->measure_current();
  243. break;
  244. case POWER_SUPPLY_PROP_CAPACITY:
  245. if((act8xxx_dev->bat_status == POWER_SUPPLY_STATUS_CHARGING)
  246. &&(act8xxx_dev->capacity == 0)){
  247. val->intval = 1;
  248. }
  249. else{
  250. val->intval = act8xxx_dev->capacity;
  251. }
  252. break;
  253. case POWER_SUPPLY_PROP_TEMP:
  254. val->intval = 0; //temporary
  255. break;
  256. // case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  257. // ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
  258. // break;
  259. // case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  260. // ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
  261. // break;
  262. // case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  263. // ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
  264. // break;
  265. case POWER_SUPPLY_PROP_TECHNOLOGY:
  266. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  267. break;
  268. case POWER_SUPPLY_PROP_HEALTH:
  269. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  270. break;
  271. default:
  272. return -EINVAL;
  273. }
  274. return ret;
  275. }
  276. static int ac_power_get_property(struct power_supply *psy,
  277. enum power_supply_property psp, union power_supply_propval *val)
  278. {
  279. int retval = 0;
  280. switch (psp)
  281. {
  282. case POWER_SUPPLY_PROP_ONLINE:
  283. val->intval = act8942_opts->is_ac_online();
  284. break;
  285. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  286. val->intval = act8942_opts->measure_voltage();
  287. break;
  288. default:
  289. return -EINVAL;
  290. }
  291. return retval;
  292. }
  293. static int usb_power_get_property(struct power_supply *psy,
  294. enum power_supply_property psp, union power_supply_propval *val)
  295. {
  296. int retval = 0;
  297. switch (psp)
  298. {
  299. case POWER_SUPPLY_PROP_ONLINE:
  300. val->intval = act8942_opts->is_usb_online(); //temporary
  301. break;
  302. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  303. val->intval = act8942_opts->measure_voltage();
  304. break;
  305. default:
  306. return -EINVAL;
  307. }
  308. return retval;
  309. }
  310. static char *power_supply_list[] = {
  311. "Battery",
  312. };
  313. static void act8942_powersupply_init(struct act8xxx_device_info *act8942_dev)
  314. {
  315. act8942_dev->bat.name = "bat";
  316. act8942_dev->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  317. act8942_dev->bat.properties = bat_power_props;
  318. act8942_dev->bat.num_properties = ARRAY_SIZE(bat_power_props);
  319. act8942_dev->bat.get_property = bat_power_get_property;
  320. act8942_dev->bat.external_power_changed = NULL;
  321. act8942_dev->ac.name = "ac";
  322. act8942_dev->ac.type = POWER_SUPPLY_TYPE_MAINS;
  323. act8942_dev->ac.supplied_to = power_supply_list,
  324. act8942_dev->ac.num_supplicants = ARRAY_SIZE(power_supply_list),
  325. act8942_dev->ac.properties = ac_power_props;
  326. act8942_dev->ac.num_properties = ARRAY_SIZE(ac_power_props);
  327. act8942_dev->ac.get_property = ac_power_get_property;
  328. act8942_dev->usb.name = "usb";
  329. act8942_dev->usb.type = POWER_SUPPLY_TYPE_USB;
  330. act8942_dev->usb.supplied_to = power_supply_list,
  331. act8942_dev->usb.num_supplicants = ARRAY_SIZE(power_supply_list),
  332. act8942_dev->usb.properties = usb_power_props;
  333. act8942_dev->usb.num_properties = ARRAY_SIZE(usb_power_props);
  334. act8942_dev->usb.get_property = usb_power_get_property;
  335. }
  336. static void update_work_func(struct work_struct *work)
  337. {
  338. int capacity,bat_status;
  339. capacity = measure_capacity_advanced();
  340. if (capacity < 0)
  341. return;
  342. bat_status = get_bat_status();
  343. if(act8xxx_dev->ac_status != is_ac_online()){
  344. power_supply_changed(&act8xxx_dev->ac);
  345. }
  346. if(act8xxx_dev->capacity != capacity){
  347. act8xxx_dev->capacity = capacity;
  348. power_supply_changed(&act8xxx_dev->bat);
  349. }
  350. if(act8xxx_dev->bat_status != bat_status){
  351. act8xxx_dev->bat_status = bat_status;
  352. power_supply_changed(&act8xxx_dev->bat);
  353. }
  354. }
  355. static void polling_func(unsigned long arg)
  356. {
  357. struct act8xxx_device_info *act8942_dev = (struct act8xxx_device_info *)arg;
  358. schedule_work(&(act8942_dev->work_update));
  359. mod_timer(&(act8942_dev->polling_timer), jiffies + msecs_to_jiffies(act8942_opts->update_period));
  360. }
  361. #endif
  362. /*
  363. * i2c specific code
  364. */
  365. static int act8xxx_read_i2c(struct i2c_client *client, u8 reg, u8 *val)
  366. {
  367. struct i2c_msg msgs[] = {
  368. {
  369. .addr = client->addr,
  370. .flags = 0,
  371. .len = 1,
  372. .buf = &reg,
  373. },
  374. {
  375. .addr = client->addr,
  376. .flags = I2C_M_RD,
  377. .len = 1,
  378. .buf = val,
  379. }
  380. };
  381. if (!client->adapter)
  382. return -ENODEV;
  383. if(i2c_transfer(client->adapter, msgs, 2) == 2)
  384. {
  385. return 0;
  386. }
  387. return -EBUSY;
  388. }
  389. static int act8xxx_write_i2c(struct i2c_client *client, u8 reg, u8 *val)
  390. {
  391. unsigned char buff[2];
  392. struct i2c_msg msgs[] = {
  393. {
  394. .addr = client->addr,
  395. .flags = 0,
  396. .len = 2,
  397. .buf = buff,
  398. }
  399. };
  400. buff[0] = reg;
  401. buff[1] = *val;
  402. if(i2c_transfer(client->adapter, msgs, 1) == 1)
  403. {
  404. return 0;
  405. }
  406. return -EBUSY;
  407. }
  408. int get_vsel(void) //Elvis Fool
  409. {
  410. return 0;
  411. }
  412. void set_vsel(int level) //Elvis Fool
  413. {
  414. return;
  415. }
  416. /*
  417. * REGx/VSET[ ] Output Voltage Setting
  418. *
  419. * REGx/VSET[5:3]
  420. * REGx/VSET[2:0] 000 001 010 011 100 101 110 111
  421. * 000 0.600 0.800 1.000 1.200 1.600 2.000 2.400 3.200
  422. * 001 0.625 0.825 1.025 1.250 1.650 2.050 2.500 3.300
  423. * 010 0.650 0.850 1.050 1.300 1.700 2.100 2.600 3.400
  424. * 011 0.675 0.875 1.075 1.350 1.750 2.150 2.700 3.500
  425. * 100 0.700 0.900 1.100 1.400 1.800 2.200 2.800 3.600
  426. * 101 0.725 0.925 1.125 1.450 1.850 2.250 2.900 3.700
  427. * 110 0.750 0.950 1.150 1.500 1.900 2.300 3.000 3.800
  428. * 111 0.775 0.975 1.175 1.550 1.950 2.350 3.100 3.900
  429. *
  430. */
  431. static const unsigned long vset_table[] = {
  432. 600, 625, 650, 675, 700, 725, 750, 775,
  433. 800, 825, 850, 875, 900, 925, 950, 975,
  434. 1000, 1025, 1050, 1075, 1100, 1125, 1150, 1175,
  435. 1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550,
  436. 1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950,
  437. 2000, 2050, 2100, 2150, 2200, 2250, 2300, 2350,
  438. 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100,
  439. 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900,
  440. }; //unit is mV
  441. static inline int get_vset_from_table(unsigned long voltage, uint8_t *val)
  442. {
  443. uint8_t i;
  444. *val = 0;
  445. if((voltage<600) || (voltage > 3900))
  446. {
  447. pr_err("Wrong VSET range! VSET range in [600:3900]mV\n");
  448. return -EINVAL;
  449. }
  450. for(i=0; i<ARRAY_SIZE(vset_table); i++)
  451. {
  452. if(voltage == vset_table[i])
  453. {
  454. *val = i;
  455. return 0;
  456. }
  457. }
  458. pr_err("voltage invalid! Please notice vset_table!\n");
  459. return -EINVAL;
  460. }
  461. static int set_reg_voltage(act8xxx_regx regx, unsigned long *voltage)
  462. {
  463. int ret = 0;
  464. static u32 reg_addr[] = {ACT8xxx_REG1_ADDR, ACT8xxx_REG2_ADDR, ACT8xxx_REG3_ADDR,
  465. ACT8xxx_REG4_ADDR, ACT8xxx_REG5_ADDR, ACT8xxx_REG6_ADDR, ACT8xxx_REG7_ADDR};
  466. act8xxx_register_data_t register_data = { 0 };
  467. if((regx<1) || (regx>7))
  468. {
  469. pr_err("Wrong REG number! REG number in [1:7]\n");
  470. return -EINVAL;
  471. }
  472. get_vset_from_table(*voltage, &register_data.d8);
  473. //pr_err("set_reg_voltage: reg_addr = 0x%02x, data = 0x%x\n", reg_addr[regx-1], register_data.d8);
  474. ret = act8xxx_write_i2c(this_client, reg_addr[regx-1], &register_data.d8); //regx-1 for compatible act8xxx_regx
  475. return ret;
  476. }
  477. static int get_reg_voltage(act8xxx_regx regx, unsigned long *voltage)
  478. {
  479. int ret = 0;
  480. static u32 reg_addr[] = {ACT8xxx_REG1_ADDR, ACT8xxx_REG2_ADDR, ACT8xxx_REG3_ADDR,
  481. ACT8xxx_REG4_ADDR, ACT8xxx_REG5_ADDR, ACT8xxx_REG6_ADDR, ACT8xxx_REG7_ADDR};
  482. act8xxx_register_data_t register_data = { 0 };
  483. if((regx<1) || (regx>7))
  484. {
  485. pr_err("Wrong REG number! REG number in [1:7]\n");
  486. return -EINVAL;
  487. }
  488. ret = act8xxx_read_i2c(this_client, reg_addr[regx-1], &register_data.d8); //regx-1 for compatible act8xxx_regx
  489. *voltage = vset_table[register_data.REGx_VSET];
  490. return ret;
  491. }
  492. static inline void act8xxx_dump(struct i2c_client *client)
  493. {
  494. u8 val = 0;
  495. int ret = 0;
  496. ret = act8xxx_read_i2c(client, ACT8xxx_SYS_ADDR, &val);
  497. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_SYS_ADDR, val);
  498. ret = act8xxx_read_i2c(client, (ACT8xxx_SYS_ADDR+1), &val);
  499. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_SYS_ADDR+1, val);
  500. ret = act8xxx_read_i2c(client, ACT8xxx_REG1_ADDR, &val);
  501. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG1_ADDR, val);
  502. ret = act8xxx_read_i2c(client, (ACT8xxx_REG1_ADDR+1), &val);
  503. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG1_ADDR+1, val);
  504. ret = act8xxx_read_i2c(client, (ACT8xxx_REG1_ADDR+2), &val);
  505. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG1_ADDR+2, val);
  506. ret = act8xxx_read_i2c(client, ACT8xxx_REG2_ADDR, &val);
  507. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG2_ADDR, val);
  508. ret = act8xxx_read_i2c(client, (ACT8xxx_REG2_ADDR+1), &val);
  509. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG2_ADDR+1, val);
  510. ret = act8xxx_read_i2c(client, (ACT8xxx_REG2_ADDR+2), &val);
  511. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG2_ADDR+2, val);
  512. ret = act8xxx_read_i2c(client, ACT8xxx_REG3_ADDR, &val);
  513. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG3_ADDR, val);
  514. ret = act8xxx_read_i2c(client, (ACT8xxx_REG3_ADDR+1), &val);
  515. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG3_ADDR+1, val);
  516. ret = act8xxx_read_i2c(client, (ACT8xxx_REG3_ADDR+2), &val);
  517. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG3_ADDR+2, val);
  518. ret = act8xxx_read_i2c(client, ACT8xxx_REG4_ADDR, &val);
  519. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG4_ADDR, val);
  520. ret = act8xxx_read_i2c(client, (ACT8xxx_REG4_ADDR+1), &val);
  521. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG4_ADDR+1, val);
  522. ret = act8xxx_read_i2c(client, ACT8xxx_REG5_ADDR, &val);
  523. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG5_ADDR, val);
  524. ret = act8xxx_read_i2c(client, (ACT8xxx_REG5_ADDR+1), &val);
  525. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG5_ADDR+1, val);
  526. ret = act8xxx_read_i2c(client, ACT8xxx_REG6_ADDR, &val);
  527. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG6_ADDR, val);
  528. ret = act8xxx_read_i2c(client, (ACT8xxx_REG6_ADDR+1), &val);
  529. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG6_ADDR+1, val);
  530. ret = act8xxx_read_i2c(client, ACT8xxx_REG7_ADDR, &val);
  531. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG7_ADDR, val);
  532. ret = act8xxx_read_i2c(client, (ACT8xxx_REG7_ADDR+1), &val);
  533. pr_info("act8xxx: [0x%x] : 0x%x\n", ACT8xxx_REG7_ADDR+1, val);
  534. #ifdef CONFIG_PMU_ACT8942
  535. ret = act8xxx_read_i2c(client, ACT8942_APCH_ADDR, &val);
  536. pr_info("act8942: [0x%x] : 0x%x\n", ACT8942_APCH_ADDR, val);
  537. ret = act8xxx_read_i2c(client, (ACT8942_APCH_ADDR+1), &val);
  538. pr_info("act8942: [0x%x] : 0x%x\n", ACT8942_APCH_ADDR+1, val);
  539. ret = act8xxx_read_i2c(client, (ACT8942_APCH_ADDR+8), &val);
  540. pr_info("act8942: [0x%x] : 0x%x\n", ACT8942_APCH_ADDR+8, val);
  541. ret = act8xxx_read_i2c(client, (ACT8942_APCH_ADDR+9), &val);
  542. pr_info("act8942: [0x%x] : 0x%x\n", ACT8942_APCH_ADDR+9, val);
  543. ret = act8xxx_read_i2c(client, (ACT8942_APCH_ADDR+0xa), &val);
  544. pr_info("act8942: [0x%x] : 0x%x\n", ACT8942_APCH_ADDR+0xa, val);
  545. #endif
  546. }
  547. /****************************************************************************/
  548. /* max args accepted for monitor commands */
  549. #define CONFIG_SYS_MAXARGS 16
  550. //#define DEBUG_PARSER
  551. static int parse_line (char *line, char *argv[])
  552. {
  553. int nargs = 0;
  554. #ifdef DEBUG_PARSER
  555. pr_info ("parse_line: \"%s\"\n", line);
  556. #endif
  557. while (nargs < CONFIG_SYS_MAXARGS) {
  558. /* skip any white space */
  559. while ((*line == ' ') || (*line == '\t')) {
  560. ++line;
  561. }
  562. if (*line == '\0') { /* end of line, no more args */
  563. argv[nargs] = NULL;
  564. #ifdef DEBUG_PARSER
  565. pr_info ("parse_line: nargs=%d\n", nargs);
  566. #endif
  567. return (nargs);
  568. }
  569. argv[nargs++] = line; /* begin of argument string */
  570. /* find end of string */
  571. while (*line && (*line != ' ') && (*line != '\t')) {
  572. ++line;
  573. }
  574. if (*line == '\0') { /* end of line, no more args */
  575. argv[nargs] = NULL;
  576. #ifdef DEBUG_PARSER
  577. pr_info ("parse_line: nargs=%d\n", nargs);
  578. #endif
  579. return (nargs);
  580. }
  581. *line++ = '\0'; /* terminate current arg */
  582. }
  583. pr_info ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
  584. #ifdef DEBUG_PARSER
  585. pr_info ("parse_line: nargs=%d\n", nargs);
  586. #endif
  587. return (nargs);
  588. }
  589. /****************************************************************************/
  590. ssize_t act8xxx_register_dump(struct class *class, struct class_attribute *attr, char *buf)
  591. {
  592. act8xxx_dump(this_client);
  593. return 0;
  594. }
  595. ssize_t act8xxx_test_show(struct class *class, struct class_attribute *attr, char *buf)
  596. {
  597. unsigned long voltage;
  598. if(get_reg_voltage(ACT8xxx_REG3, &voltage))
  599. {
  600. return -1;
  601. }
  602. pr_info("test:get %ldmV\n", voltage);
  603. return strlen(buf);
  604. }
  605. ssize_t act8xxx_test_store(struct class *class, struct class_attribute *attr, char *buf)
  606. {
  607. unsigned long voltage = 3100;
  608. if(set_reg_voltage(ACT8xxx_REG3, &voltage))
  609. {
  610. return -1;
  611. }
  612. pr_info("test:set %ldmV\n", voltage);
  613. return strlen(buf);
  614. }
  615. ssize_t act8xxx_debug_store(struct class *class, struct class_attribute *attr, char *buf)
  616. {
  617. dbg_enable = !dbg_enable;
  618. if(dbg_enable)
  619. pr_info("enable act8xxx log \n");
  620. else
  621. pr_info("disable act8xxx log \n");
  622. return strlen(buf);
  623. }
  624. ssize_t bat_refresh(struct class *class, struct class_attribute *attr, char *buf)
  625. {
  626. int re = 0;
  627. if(buf[0] == '1')
  628. re = 1;
  629. if(re == 1)
  630. refresh = 1;
  631. else
  632. refresh = 0;
  633. return strlen(buf);
  634. }
  635. ssize_t act8xxx_voltage_handle(struct class *class, struct class_attribute *attr, const char *buf, size_t count)
  636. {
  637. char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
  638. int argc, ret, regx;
  639. unsigned long voltage;
  640. argc = parse_line(buf, argv);
  641. if(argc < 2)
  642. {
  643. return -EINVAL;
  644. //ToDo help
  645. }
  646. if(!strcmp(argv[0], "get"))
  647. {
  648. ret = sscanf(argv[1], "reg%d", &regx);
  649. if(get_reg_voltage(regx, &voltage))
  650. {
  651. return -1;
  652. }
  653. pr_info("act8xxx_voltage_handle: Get reg%d voltage is %lumV\n", regx,voltage);
  654. }
  655. else if(!strcmp(argv[0], "set"))
  656. {
  657. if(argc < 3)
  658. {
  659. return -EINVAL;
  660. //ToDo help
  661. }
  662. ret = sscanf(argv[1], "reg%d", &regx);
  663. ret = sscanf(argv[2], "%ld", &voltage);
  664. if(set_reg_voltage(regx, &voltage))
  665. {
  666. return -1;
  667. }
  668. pr_info("act8xxx_voltage_handle: Set reg%d voltage is %lumV\n", regx,voltage);
  669. }
  670. else
  671. {
  672. return -EINVAL;
  673. //ToDo help
  674. }
  675. return count;
  676. }
  677. static struct class_attribute act8xxx_class_attrs[] = {
  678. __ATTR(register_dump, S_IRUGO | S_IWUSR, act8xxx_register_dump, NULL),
  679. __ATTR(voltage, S_IRUGO | S_IWUSR, NULL, act8xxx_voltage_handle),
  680. __ATTR(test, S_IRUGO | S_IWUSR, act8xxx_test_show, act8xxx_test_store),
  681. __ATTR(debug, S_IRUGO | S_IWUSR, NULL, act8xxx_debug_store),
  682. __ATTR(bat_refresh, S_IRUGO | S_IWUSR, NULL, bat_refresh),
  683. __ATTR_NULL
  684. };
  685. static struct class act8xxx_class = {
  686. .name = ACT8xxx_CLASS_NAME,
  687. .class_attrs = act8xxx_class_attrs,
  688. };
  689. #ifdef CONFIG_PMU_ACT8942
  690. static int act8942_operations_init(struct act8942_operations* pdata)
  691. {
  692. act8942_opts = pdata;
  693. if(act8942_opts->is_ac_online == NULL)
  694. {
  695. act8942_opts->is_ac_online = is_ac_online;
  696. }
  697. if(act8942_opts->is_usb_online == NULL)
  698. {
  699. act8942_opts->is_usb_online = is_usb_online;
  700. }
  701. if(act8942_opts->set_bat_off== NULL)
  702. {
  703. pr_err("act8942_opts->measure_voltage is NULL!\n");
  704. return -1;
  705. }
  706. if(act8942_opts->get_charge_status == NULL)
  707. {
  708. act8942_opts->get_charge_status = get_charge_status;
  709. }
  710. if(act8942_opts->set_charge_current == NULL)
  711. {
  712. pr_err("act8942_opts->set_charge_current is NULL!\n");
  713. return -1;
  714. }
  715. if(act8942_opts->measure_voltage == NULL)
  716. {
  717. pr_err("act8942_opts->measure_voltage is NULL!\n");
  718. return -1;
  719. }
  720. if(act8942_opts->measure_current == NULL)
  721. {
  722. pr_err("act8942_opts->measure_current is NULL!\n");
  723. return -1;
  724. }
  725. if(act8942_opts->measure_capacity_charging == NULL)
  726. {
  727. pr_err("act8942_opts->measure_capacity is NULL!\n");
  728. return -1;
  729. }
  730. if(act8942_opts->measure_capacity_battery== NULL)
  731. {
  732. pr_err("act8942_opts->measure_capacity is NULL!\n");
  733. return -1;
  734. }
  735. if(act8942_opts->update_period <= 0)
  736. {
  737. act8942_opts->update_period = 5000;
  738. }
  739. return 0;
  740. }
  741. #define SAMPLE_SIZE 20
  742. static int capacity_sample_array[SAMPLE_SIZE]={
  743. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  744. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  745. };
  746. static int capacity_sample_pointer = 0;
  747. inline static int measure_capacity_advanced(void)
  748. {
  749. int current_capacity, i,capacity;
  750. int num,sum,min,max;
  751. int current_status = act8942_opts->is_ac_online();
  752. if(current_status)
  753. {
  754. current_capacity = act8942_opts->measure_capacity_charging();
  755. }
  756. else
  757. {
  758. current_capacity = act8942_opts->measure_capacity_battery();
  759. }
  760. act8xxx_dbg(dbg_enable, "current_status=%d,current_capacity = %d\n", current_status,current_capacity);
  761. if(act8942_opts->asn < 2)
  762. {
  763. return current_capacity;
  764. }
  765. capacity_sample_array[capacity_sample_pointer] = current_capacity;
  766. capacity_sample_pointer ++;
  767. if(capacity_sample_pointer >= act8942_opts->asn){
  768. capacity_sample_pointer = 0;
  769. }
  770. sum = 0;
  771. num = 0;
  772. min = 100;
  773. max = 0;
  774. for(i = 0;i<act8942_opts->asn;i++){
  775. if(capacity_sample_array[i] != -1){
  776. sum += capacity_sample_array[i];
  777. num ++;
  778. if(max < capacity_sample_array[i])
  779. max = capacity_sample_array[i];
  780. if(min > capacity_sample_array[i])
  781. min = capacity_sample_array[i];
  782. }
  783. }
  784. // drop max and min capacity
  785. if(num>2){
  786. sum = sum - max -min;
  787. }
  788. else{
  789. if(refresh)
  790. return current_capacity;
  791. else
  792. return -1;
  793. }
  794. current_capacity = sum/(num-2);
  795. if(act8942_opts->rvp&&act8xxx_dev->capacity != -1 && !refresh)
  796. {
  797. if (current_status) {
  798. //ac online don't report percentage smaller than prev
  799. capacity = (current_capacity > act8xxx_dev->capacity) ? current_capacity : act8xxx_dev->capacity;
  800. } else {
  801. //ac not online don't report percentage bigger than prev
  802. capacity = (current_capacity < act8xxx_dev->capacity) ? current_capacity : act8xxx_dev->capacity;
  803. }
  804. }
  805. else
  806. {
  807. if (refresh) {
  808. refresh = 0;
  809. printk("bat data refresh\n");
  810. }
  811. capacity = current_capacity;
  812. }
  813. act8xxx_dbg(dbg_enable, "sum=%d,num =%d,max=%d,min=%d,real_capacity =%d,capacity =%d \n", sum,num,max,min,current_capacity,capacity);
  814. return capacity;
  815. }
  816. #endif
  817. #ifdef CONFIG_HAS_EARLYSUSPEND
  818. #include <linux/earlysuspend.h>
  819. #ifdef CONFIG_PMU_ACT8942
  820. static void act8xxx_early_suspend(struct early_suspend *h)
  821. {
  822. act8942_opts->set_charge_current(1);
  823. pr_info("fast charger on early_suspend\n\n");
  824. }
  825. static void act8xxx_late_resume(struct early_suspend *h)
  826. {
  827. act8942_opts->set_charge_current(0);
  828. pr_info("slow charger on resume\n\n");
  829. }
  830. #elif defined(CONFIG_PMU_ACT8862)
  831. static void act8xxx_early_suspend(struct early_suspend *h)
  832. {
  833. pr_info("fast charger on early_suspend\n\n");
  834. }
  835. static void act8xxx_late_resume(struct early_suspend *h)
  836. {
  837. pr_info("slow charger on resume\n\n");
  838. }
  839. #endif
  840. static struct early_suspend act8xxx_suspend = {
  841. .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN,
  842. .suspend = act8xxx_early_suspend,
  843. .resume = act8xxx_late_resume,
  844. .param = NULL,
  845. };
  846. #endif
  847. static int act8xxx_i2c_resume(struct i2c_client *client)
  848. { int i;
  849. for(i = 0;i< SAMPLE_SIZE;i++){
  850. capacity_sample_array[i] = -1;
  851. }
  852. capacity_sample_pointer = 0;
  853. refresh = 1;
  854. }
  855. static int act8xxx_i2c_probe(struct i2c_client *client,
  856. const struct i2c_device_id *id)
  857. {
  858. //struct act8xxx_device_info *act8xxx_dev; //Elvis Fool
  859. int num;
  860. int retval = 0;
  861. pr_info("act8xxx_i2c_probe\n");
  862. #ifdef CONFIG_PMU_ACT8942
  863. if(act8942_operations_init((struct act8942_operations*)client->dev.platform_data))
  864. {
  865. dev_err(&client->dev, "failed to init act8942_opts!\n");
  866. return -EINVAL;
  867. }
  868. #endif
  869. /* Get new ID for the new PMU device */
  870. retval = idr_pre_get(&pmu_id, GFP_KERNEL);
  871. if (retval == 0)
  872. {
  873. return -ENOMEM;
  874. }
  875. mutex_lock(&pmu_mutex);
  876. retval = idr_get_new(&pmu_id, client, &num);
  877. mutex_unlock(&pmu_mutex);
  878. if (retval < 0)
  879. {
  880. return retval;
  881. }
  882. act8xxx_dev = kzalloc(sizeof(*act8xxx_dev), GFP_KERNEL);
  883. if (!act8xxx_dev) {
  884. dev_err(&client->dev, "failed to allocate device info data\n");
  885. retval = -ENOMEM;
  886. goto act8xxx_failed_2;
  887. }
  888. act8xxx_dev->id = num;
  889. #ifdef CONFIG_PMU_ACT8942
  890. act8xxx_dev->capacity = -1;
  891. #endif
  892. //act8xxx_dev->chip = id->driver_data; //elvis
  893. this_client = client;
  894. i2c_set_clientdata(client, act8xxx_dev);
  895. act8xxx_dev->dev = &client->dev;
  896. act8xxx_dev->client = client;
  897. #ifdef CONFIG_PMU_ACT8942
  898. act8942_powersupply_init(act8xxx_dev);
  899. retval = power_supply_register(&client->dev, &act8xxx_dev->bat);
  900. if (retval) {
  901. dev_err(&client->dev, "failed to register battery\n");
  902. goto act8xxx_failed_2;
  903. }
  904. retval = power_supply_register(&client->dev, &act8xxx_dev->ac);
  905. if (retval) {
  906. dev_err(&client->dev, "failed to register ac\n");
  907. goto act8xxx_failed_2;
  908. }
  909. retval = power_supply_register(&client->dev, &act8xxx_dev->usb);
  910. if (retval) {
  911. dev_err(&client->dev, "failed to register usb\n");
  912. goto act8xxx_failed_2;
  913. }
  914. INIT_WORK(&(act8xxx_dev->work_update), update_work_func);
  915. init_timer(&(act8xxx_dev->polling_timer));
  916. act8xxx_dev->polling_timer.expires = jiffies + msecs_to_jiffies(act8942_opts->update_period);
  917. act8xxx_dev->polling_timer.function = polling_func;
  918. act8xxx_dev->polling_timer.data = (unsigned long)act8xxx_dev;
  919. add_timer(&(act8xxx_dev->polling_timer));
  920. #endif
  921. #ifdef CONFIG_HAS_EARLYSUSPEND
  922. register_early_suspend(&act8xxx_suspend);
  923. #endif
  924. dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  925. return 0;
  926. act8xxx_failed_2:
  927. kfree(act8xxx_dev);
  928. act8xxx_failed_1:
  929. mutex_lock(&pmu_mutex);
  930. idr_remove(&pmu_id, num);
  931. mutex_unlock(&pmu_mutex);
  932. return retval;
  933. }
  934. static int act8xxx_i2c_remove(struct i2c_client *client)
  935. {
  936. struct act8xxx_device_info *act8xxx_dev = i2c_get_clientdata(client);
  937. pr_info("act8xxx_i2c_remove\n");
  938. #ifdef CONFIG_PMU_ACT8942
  939. power_supply_unregister(&act8xxx_dev->bat);
  940. del_timer(&(act8xxx_dev->polling_timer));
  941. kfree(act8xxx_dev->bat.name);
  942. #endif
  943. mutex_lock(&pmu_mutex);
  944. idr_remove(&pmu_id, act8xxx_dev->id);
  945. mutex_unlock(&pmu_mutex);
  946. kfree(act8xxx_dev);
  947. #ifdef CONFIG_HAS_EARLYSUSPEND
  948. unregister_early_suspend(&act8xxx_suspend);
  949. #endif
  950. return 0;
  951. }
  952. static int act8xxx_open(struct inode *inode, struct file *file)
  953. {
  954. pmu_dev_t *pmu_dev;
  955. pr_info("act8xxx_open\n");
  956. /* Get the per-device structure that contains this cdev */
  957. pmu_dev = container_of(inode->i_cdev, pmu_dev_t, cdev);
  958. file->private_data = pmu_dev;
  959. return 0;
  960. }
  961. static int act8xxx_release(struct inode *inode, struct file *file)
  962. {
  963. file->private_data = NULL;
  964. return 0;
  965. }
  966. static long act8xxx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  967. {
  968. long ret = 0;
  969. int size, i;
  970. act8xxx_i2c_msg_t *msgs = NULL;
  971. //struct i2c_client *client = (struct i2c_client *)file->private_data;
  972. struct i2c_client *client = this_client;
  973. if(_IOC_DIR(cmd) == _IOC_READ)
  974. {
  975. size = _IOC_SIZE(cmd);
  976. if(size)
  977. {
  978. msgs = kmalloc((sizeof(act8xxx_i2c_msg_t)*size), GFP_KERNEL);
  979. if(!msgs)
  980. {
  981. pr_err("act8xxx_ioctl: failed to allocate memory for act8xxx_i2c_msgs.\n");
  982. return -ENOMEM;
  983. }
  984. ret = copy_from_user(msgs, (void __user *)arg, (sizeof(act8xxx_i2c_msg_t)*size));
  985. if(ret)
  986. {
  987. pr_err("act8xxx_ioctl: copy_from_user failed!\n ");
  988. kfree(msgs);
  989. return ret;
  990. }
  991. for(i=0; i<size; i++)
  992. {
  993. act8xxx_read_i2c(client, msgs[i].reg, &msgs[i].val);
  994. }
  995. ret = copy_to_user((void __user *)arg, msgs, (sizeof(act8xxx_i2c_msg_t)*size));
  996. kfree(msgs);
  997. return 0;
  998. }
  999. else
  1000. {
  1001. return -EINVAL;
  1002. }
  1003. }
  1004. if(_IOC_DIR(cmd) == _IOC_WRITE)
  1005. {
  1006. size = _IOC_SIZE(cmd);
  1007. if(size)
  1008. {
  1009. msgs = kmalloc((sizeof(act8xxx_i2c_msg_t)*size), GFP_KERNEL);
  1010. if(!msgs)
  1011. {
  1012. pr_err("act8xxx_ioctl: failed to allocate memory for act8xxx_i2c_msgs.\n");
  1013. return -ENOMEM;
  1014. }
  1015. ret = copy_from_user(msgs, (void __user *)arg, (sizeof(act8xxx_i2c_msg_t)*size));
  1016. if(ret)
  1017. {
  1018. pr_err("act8xxx_ioctl: copy_from_user failed!\n ");
  1019. kfree(msgs);
  1020. return ret;
  1021. }
  1022. for(i=0; i<size; i++)
  1023. {
  1024. act8xxx_write_i2c(client, msgs[i].reg, &msgs[i].val);
  1025. }
  1026. kfree(msgs);
  1027. return 0;
  1028. }
  1029. else
  1030. {
  1031. return -EINVAL;
  1032. }
  1033. }
  1034. return ret;
  1035. }
  1036. static struct file_operations act8xxx_fops = {
  1037. .owner = THIS_MODULE,
  1038. .open = act8xxx_open,
  1039. .release = act8xxx_release,
  1040. .unlocked_ioctl = act8xxx_ioctl,
  1041. };
  1042. #ifdef CONFIG_PMU_ACT8942
  1043. static void pmu_power_off(void)
  1044. {
  1045. u8 val = 0;
  1046. int ret = 0;
  1047. if(act8942_opts->is_ac_online()){ //AC in after power off press
  1048. arm_pm_restart("","charging_reboot");
  1049. }
  1050. ret = act8xxx_read_i2c(this_client, ACT8xxx_REG4_ADDR+1, &val);
  1051. val = val|0x80;
  1052. //printk("val = %x\n",val);
  1053. ret = act8xxx_write_i2c(this_client, ACT8xxx_REG4_ADDR+1, &val);
  1054. val = val&(~(0x80));
  1055. //printk("val = %x\n",val);
  1056. ret = act8xxx_write_i2c(this_client, ACT8xxx_REG4_ADDR+1, &val);
  1057. }
  1058. #endif
  1059. static int act8xxx_probe(struct platform_device *pdev)
  1060. {
  1061. int ret;
  1062. struct device *dev_p;
  1063. pr_info("act8xxx_probe\n");
  1064. act8xxx_pmu_dev = kmalloc(sizeof(pmu_dev_t), GFP_KERNEL);
  1065. if (!act8xxx_pmu_dev)
  1066. {
  1067. pr_err("act8xxx: failed to allocate memory for pmu device\n");
  1068. return -ENOMEM;
  1069. }
  1070. ret = alloc_chrdev_region(&act8xxx_devno, 0, 1, ACT8xxx_DEVICE_NAME);
  1071. if (ret < 0) {
  1072. pr_err("act8xxx: failed to allocate chrdev. \n");
  1073. return 0;
  1074. }
  1075. /* connect the file operations with cdev */
  1076. cdev_init(&act8xxx_pmu_dev->cdev, &act8xxx_fops);
  1077. act8xxx_pmu_dev->cdev.owner = THIS_MODULE;
  1078. /* connect the major/minor number to the cdev */
  1079. ret = cdev_add(&act8xxx_pmu_dev->cdev, act8xxx_devno, 1);
  1080. if (ret) {
  1081. pr_err("act8xxx: failed to add device. \n");
  1082. /* @todo do with error */
  1083. return ret;
  1084. }
  1085. ret = class_register(&act8xxx_class);
  1086. if(ret)
  1087. {
  1088. printk(" class register i2c_class fail!\n");
  1089. return ret;
  1090. }
  1091. /* create /dev nodes */
  1092. dev_p = device_create(&act8xxx_class, NULL, MKDEV(MAJOR(act8xxx_devno), 0),
  1093. NULL, "act8xxx");
  1094. if (IS_ERR(dev_p)) {
  1095. pr_err("act8xxx: failed to create device node\n");
  1096. /* @todo do with error */
  1097. return PTR_ERR(dev_p);;
  1098. }
  1099. printk( "act8xxx: driver initialized ok\n");
  1100. #ifdef CONFIG_PMU_ACT8942
  1101. if(pm_power_off == NULL)
  1102. pm_power_off = pmu_power_off;
  1103. #endif
  1104. return ret;
  1105. }
  1106. static int act8xxx_remove(struct platform_device *pdev)
  1107. {
  1108. pr_info("act8xxx_remove\n");
  1109. cdev_del(&act8xxx_pmu_dev->cdev);
  1110. unregister_chrdev_region(act8xxx_devno, 1);
  1111. kfree(act8xxx_pmu_dev);
  1112. return 0;
  1113. }
  1114. static const struct i2c_device_id act8xxx_i2c_id[] = {
  1115. { ACT8xxx_I2C_NAME, 0 },
  1116. {},
  1117. };
  1118. static struct i2c_driver act8xxx_i2c_driver = {
  1119. .driver = {
  1120. .name = "ACT8xxx-PMU",
  1121. },
  1122. .probe = act8xxx_i2c_probe,
  1123. .remove = act8xxx_i2c_remove,
  1124. .resume = act8xxx_i2c_resume,
  1125. .id_table = act8xxx_i2c_id,
  1126. };
  1127. static struct platform_driver ACT8xxx_platform_driver = {
  1128. .probe = act8xxx_probe,
  1129. .remove = act8xxx_remove,
  1130. .driver = {
  1131. .name = ACT8xxx_DEVICE_NAME,
  1132. },
  1133. };
  1134. static int __init act8xxx_pmu_init(void)
  1135. {
  1136. int ret;
  1137. pr_info("act8xxx_pmu_init\n");
  1138. ret = platform_driver_register(&ACT8xxx_platform_driver);
  1139. if (ret) {
  1140. printk(KERN_ERR "failed to register ACT8xxx module, error %d\n", ret);
  1141. return -ENODEV;
  1142. }
  1143. ret = i2c_add_driver(&act8xxx_i2c_driver);
  1144. if (ret < 0)
  1145. {
  1146. pr_err("act8xxx: failed to add i2c driver. \n");
  1147. ret = -ENOTSUPP;
  1148. }
  1149. return ret;
  1150. }
  1151. module_init(act8xxx_pmu_init);
  1152. static void __exit act8xxx_pmu_exit(void)
  1153. {
  1154. pr_info("act8xxx_pmu_exit\n");
  1155. i2c_del_driver(&act8xxx_i2c_driver);
  1156. platform_driver_unregister(&ACT8xxx_platform_driver);
  1157. }
  1158. module_exit(act8xxx_pmu_exit);
  1159. MODULE_AUTHOR("Elvis Yu <elvis.yu@amlogic.com>");
  1160. MODULE_DESCRIPTION("ACT8xxx PMU driver");
  1161. MODULE_LICENSE("GPL");