z2_battery.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Battery measurement code for Zipit Z2
  3. *
  4. * Copyright (C) 2009 Peter Edwards <sweetlilmre@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/gpio.h>
  13. #include <linux/i2c.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/power_supply.h>
  17. #include <linux/slab.h>
  18. #include <linux/z2_battery.h>
  19. #define Z2_DEFAULT_NAME "Z2"
  20. struct z2_charger {
  21. struct z2_battery_info *info;
  22. int bat_status;
  23. struct i2c_client *client;
  24. struct power_supply *batt_ps;
  25. struct power_supply_desc batt_ps_desc;
  26. struct mutex work_lock;
  27. struct work_struct bat_work;
  28. };
  29. static unsigned long z2_read_bat(struct z2_charger *charger)
  30. {
  31. int data;
  32. data = i2c_smbus_read_byte_data(charger->client,
  33. charger->info->batt_I2C_reg);
  34. if (data < 0)
  35. return 0;
  36. return data * charger->info->batt_mult / charger->info->batt_div;
  37. }
  38. static int z2_batt_get_property(struct power_supply *batt_ps,
  39. enum power_supply_property psp,
  40. union power_supply_propval *val)
  41. {
  42. struct z2_charger *charger = power_supply_get_drvdata(batt_ps);
  43. struct z2_battery_info *info = charger->info;
  44. switch (psp) {
  45. case POWER_SUPPLY_PROP_STATUS:
  46. val->intval = charger->bat_status;
  47. break;
  48. case POWER_SUPPLY_PROP_TECHNOLOGY:
  49. val->intval = info->batt_tech;
  50. break;
  51. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  52. if (info->batt_I2C_reg >= 0)
  53. val->intval = z2_read_bat(charger);
  54. else
  55. return -EINVAL;
  56. break;
  57. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  58. if (info->max_voltage >= 0)
  59. val->intval = info->max_voltage;
  60. else
  61. return -EINVAL;
  62. break;
  63. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  64. if (info->min_voltage >= 0)
  65. val->intval = info->min_voltage;
  66. else
  67. return -EINVAL;
  68. break;
  69. case POWER_SUPPLY_PROP_PRESENT:
  70. val->intval = 1;
  71. break;
  72. default:
  73. return -EINVAL;
  74. }
  75. return 0;
  76. }
  77. static void z2_batt_ext_power_changed(struct power_supply *batt_ps)
  78. {
  79. struct z2_charger *charger = power_supply_get_drvdata(batt_ps);
  80. schedule_work(&charger->bat_work);
  81. }
  82. static void z2_batt_update(struct z2_charger *charger)
  83. {
  84. int old_status = charger->bat_status;
  85. struct z2_battery_info *info;
  86. info = charger->info;
  87. mutex_lock(&charger->work_lock);
  88. charger->bat_status = (info->charge_gpio >= 0) ?
  89. (gpio_get_value(info->charge_gpio) ?
  90. POWER_SUPPLY_STATUS_CHARGING :
  91. POWER_SUPPLY_STATUS_DISCHARGING) :
  92. POWER_SUPPLY_STATUS_UNKNOWN;
  93. if (old_status != charger->bat_status) {
  94. pr_debug("%s: %i -> %i\n", charger->batt_ps->desc->name,
  95. old_status,
  96. charger->bat_status);
  97. power_supply_changed(charger->batt_ps);
  98. }
  99. mutex_unlock(&charger->work_lock);
  100. }
  101. static void z2_batt_work(struct work_struct *work)
  102. {
  103. struct z2_charger *charger;
  104. charger = container_of(work, struct z2_charger, bat_work);
  105. z2_batt_update(charger);
  106. }
  107. static irqreturn_t z2_charge_switch_irq(int irq, void *devid)
  108. {
  109. struct z2_charger *charger = devid;
  110. schedule_work(&charger->bat_work);
  111. return IRQ_HANDLED;
  112. }
  113. static int z2_batt_ps_init(struct z2_charger *charger, int props)
  114. {
  115. int i = 0;
  116. enum power_supply_property *prop;
  117. struct z2_battery_info *info = charger->info;
  118. if (info->charge_gpio >= 0)
  119. props++; /* POWER_SUPPLY_PROP_STATUS */
  120. if (info->batt_tech >= 0)
  121. props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
  122. if (info->batt_I2C_reg >= 0)
  123. props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
  124. if (info->max_voltage >= 0)
  125. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
  126. if (info->min_voltage >= 0)
  127. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
  128. prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
  129. if (!prop)
  130. return -ENOMEM;
  131. prop[i++] = POWER_SUPPLY_PROP_PRESENT;
  132. if (info->charge_gpio >= 0)
  133. prop[i++] = POWER_SUPPLY_PROP_STATUS;
  134. if (info->batt_tech >= 0)
  135. prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
  136. if (info->batt_I2C_reg >= 0)
  137. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
  138. if (info->max_voltage >= 0)
  139. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
  140. if (info->min_voltage >= 0)
  141. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
  142. if (!info->batt_name) {
  143. dev_info(&charger->client->dev,
  144. "Please consider setting proper battery "
  145. "name in platform definition file, falling "
  146. "back to name \" Z2_DEFAULT_NAME \"\n");
  147. charger->batt_ps_desc.name = Z2_DEFAULT_NAME;
  148. } else
  149. charger->batt_ps_desc.name = info->batt_name;
  150. charger->batt_ps_desc.properties = prop;
  151. charger->batt_ps_desc.num_properties = props;
  152. charger->batt_ps_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  153. charger->batt_ps_desc.get_property = z2_batt_get_property;
  154. charger->batt_ps_desc.external_power_changed =
  155. z2_batt_ext_power_changed;
  156. charger->batt_ps_desc.use_for_apm = 1;
  157. return 0;
  158. }
  159. static int z2_batt_probe(struct i2c_client *client,
  160. const struct i2c_device_id *id)
  161. {
  162. int ret = 0;
  163. int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
  164. struct z2_charger *charger;
  165. struct z2_battery_info *info = client->dev.platform_data;
  166. struct power_supply_config psy_cfg = {};
  167. if (info == NULL) {
  168. dev_err(&client->dev,
  169. "Please set platform device platform_data"
  170. " to a valid z2_battery_info pointer!\n");
  171. return -EINVAL;
  172. }
  173. charger = kzalloc(sizeof(*charger), GFP_KERNEL);
  174. if (charger == NULL)
  175. return -ENOMEM;
  176. charger->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
  177. charger->info = info;
  178. charger->client = client;
  179. i2c_set_clientdata(client, charger);
  180. psy_cfg.drv_data = charger;
  181. mutex_init(&charger->work_lock);
  182. if (info->charge_gpio >= 0 && gpio_is_valid(info->charge_gpio)) {
  183. ret = gpio_request(info->charge_gpio, "BATT CHRG");
  184. if (ret)
  185. goto err;
  186. ret = gpio_direction_input(info->charge_gpio);
  187. if (ret)
  188. goto err2;
  189. irq_set_irq_type(gpio_to_irq(info->charge_gpio),
  190. IRQ_TYPE_EDGE_BOTH);
  191. ret = request_irq(gpio_to_irq(info->charge_gpio),
  192. z2_charge_switch_irq, 0,
  193. "AC Detect", charger);
  194. if (ret)
  195. goto err3;
  196. }
  197. ret = z2_batt_ps_init(charger, props);
  198. if (ret)
  199. goto err3;
  200. INIT_WORK(&charger->bat_work, z2_batt_work);
  201. charger->batt_ps = power_supply_register(&client->dev,
  202. &charger->batt_ps_desc,
  203. &psy_cfg);
  204. if (IS_ERR(charger->batt_ps)) {
  205. ret = PTR_ERR(charger->batt_ps);
  206. goto err4;
  207. }
  208. schedule_work(&charger->bat_work);
  209. return 0;
  210. err4:
  211. kfree(charger->batt_ps_desc.properties);
  212. err3:
  213. if (info->charge_gpio >= 0 && gpio_is_valid(info->charge_gpio))
  214. free_irq(gpio_to_irq(info->charge_gpio), charger);
  215. err2:
  216. if (info->charge_gpio >= 0 && gpio_is_valid(info->charge_gpio))
  217. gpio_free(info->charge_gpio);
  218. err:
  219. kfree(charger);
  220. return ret;
  221. }
  222. static int z2_batt_remove(struct i2c_client *client)
  223. {
  224. struct z2_charger *charger = i2c_get_clientdata(client);
  225. struct z2_battery_info *info = charger->info;
  226. cancel_work_sync(&charger->bat_work);
  227. power_supply_unregister(charger->batt_ps);
  228. kfree(charger->batt_ps_desc.properties);
  229. if (info->charge_gpio >= 0 && gpio_is_valid(info->charge_gpio)) {
  230. free_irq(gpio_to_irq(info->charge_gpio), charger);
  231. gpio_free(info->charge_gpio);
  232. }
  233. kfree(charger);
  234. return 0;
  235. }
  236. #ifdef CONFIG_PM
  237. static int z2_batt_suspend(struct device *dev)
  238. {
  239. struct i2c_client *client = to_i2c_client(dev);
  240. struct z2_charger *charger = i2c_get_clientdata(client);
  241. flush_work(&charger->bat_work);
  242. return 0;
  243. }
  244. static int z2_batt_resume(struct device *dev)
  245. {
  246. struct i2c_client *client = to_i2c_client(dev);
  247. struct z2_charger *charger = i2c_get_clientdata(client);
  248. schedule_work(&charger->bat_work);
  249. return 0;
  250. }
  251. static const struct dev_pm_ops z2_battery_pm_ops = {
  252. .suspend = z2_batt_suspend,
  253. .resume = z2_batt_resume,
  254. };
  255. #define Z2_BATTERY_PM_OPS (&z2_battery_pm_ops)
  256. #else
  257. #define Z2_BATTERY_PM_OPS (NULL)
  258. #endif
  259. static const struct i2c_device_id z2_batt_id[] = {
  260. { "aer915", 0 },
  261. { }
  262. };
  263. MODULE_DEVICE_TABLE(i2c, z2_batt_id);
  264. static struct i2c_driver z2_batt_driver = {
  265. .driver = {
  266. .name = "z2-battery",
  267. .pm = Z2_BATTERY_PM_OPS
  268. },
  269. .probe = z2_batt_probe,
  270. .remove = z2_batt_remove,
  271. .id_table = z2_batt_id,
  272. };
  273. module_i2c_driver(z2_batt_driver);
  274. MODULE_LICENSE("GPL");
  275. MODULE_AUTHOR("Peter Edwards <sweetlilmre@gmail.com>");
  276. MODULE_DESCRIPTION("Zipit Z2 battery driver");