Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License version 2 as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * h3xxx atmel micro companion support, battery subdevice |
| 7 | * based on previous kernel 2.4 version |
| 8 | * Author : Alessandro Gardich <gremlin@gremlin.it> |
| 9 | * Author : Linus Walleij <linus.walleij@linaro.org> |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/mfd/ipaq-micro.h> |
| 17 | #include <linux/power_supply.h> |
| 18 | #include <linux/workqueue.h> |
| 19 | |
| 20 | #define BATT_PERIOD 100000 /* 100 seconds in milliseconds */ |
| 21 | |
| 22 | #define MICRO_BATT_CHEM_ALKALINE 0x01 |
| 23 | #define MICRO_BATT_CHEM_NICD 0x02 |
| 24 | #define MICRO_BATT_CHEM_NIMH 0x03 |
| 25 | #define MICRO_BATT_CHEM_LION 0x04 |
| 26 | #define MICRO_BATT_CHEM_LIPOLY 0x05 |
| 27 | #define MICRO_BATT_CHEM_NOT_INSTALLED 0x06 |
| 28 | #define MICRO_BATT_CHEM_UNKNOWN 0xff |
| 29 | |
| 30 | #define MICRO_BATT_STATUS_HIGH 0x01 |
| 31 | #define MICRO_BATT_STATUS_LOW 0x02 |
| 32 | #define MICRO_BATT_STATUS_CRITICAL 0x04 |
| 33 | #define MICRO_BATT_STATUS_CHARGING 0x08 |
| 34 | #define MICRO_BATT_STATUS_CHARGEMAIN 0x10 |
| 35 | #define MICRO_BATT_STATUS_DEAD 0x20 /* Battery will not charge */ |
| 36 | #define MICRO_BATT_STATUS_NOTINSTALLED 0x20 /* For expansion pack batteries */ |
| 37 | #define MICRO_BATT_STATUS_FULL 0x40 /* Battery fully charged */ |
| 38 | #define MICRO_BATT_STATUS_NOBATTERY 0x80 |
| 39 | #define MICRO_BATT_STATUS_UNKNOWN 0xff |
| 40 | |
| 41 | struct micro_battery { |
| 42 | struct ipaq_micro *micro; |
| 43 | struct workqueue_struct *wq; |
| 44 | struct delayed_work update; |
| 45 | u8 ac; |
| 46 | u8 chemistry; |
| 47 | unsigned int voltage; |
| 48 | u16 temperature; |
| 49 | u8 flag; |
| 50 | }; |
| 51 | |
| 52 | static void micro_battery_work(struct work_struct *work) |
| 53 | { |
| 54 | struct micro_battery *mb = container_of(work, |
| 55 | struct micro_battery, update.work); |
| 56 | struct ipaq_micro_msg msg_battery = { |
| 57 | .id = MSG_BATTERY, |
| 58 | }; |
| 59 | struct ipaq_micro_msg msg_sensor = { |
| 60 | .id = MSG_THERMAL_SENSOR, |
| 61 | }; |
| 62 | |
| 63 | /* First send battery message */ |
| 64 | ipaq_micro_tx_msg_sync(mb->micro, &msg_battery); |
| 65 | if (msg_battery.rx_len < 4) |
| 66 | pr_info("ERROR"); |
| 67 | |
| 68 | /* |
| 69 | * Returned message format: |
| 70 | * byte 0: 0x00 = Not plugged in |
| 71 | * 0x01 = AC adapter plugged in |
| 72 | * byte 1: chemistry |
| 73 | * byte 2: voltage LSB |
| 74 | * byte 3: voltage MSB |
| 75 | * byte 4: flags |
| 76 | * byte 5-9: same for battery 2 |
| 77 | */ |
| 78 | mb->ac = msg_battery.rx_data[0]; |
| 79 | mb->chemistry = msg_battery.rx_data[1]; |
| 80 | mb->voltage = ((((unsigned short)msg_battery.rx_data[3] << 8) + |
| 81 | msg_battery.rx_data[2]) * 5000L) * 1000 / 1024; |
| 82 | mb->flag = msg_battery.rx_data[4]; |
| 83 | |
| 84 | if (msg_battery.rx_len == 9) |
| 85 | pr_debug("second battery ignored\n"); |
| 86 | |
| 87 | /* Then read the sensor */ |
| 88 | ipaq_micro_tx_msg_sync(mb->micro, &msg_sensor); |
| 89 | mb->temperature = msg_sensor.rx_data[1] << 8 | msg_sensor.rx_data[0]; |
| 90 | |
| 91 | queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD)); |
| 92 | } |
| 93 | |
| 94 | static int get_capacity(struct power_supply *b) |
| 95 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 96 | struct micro_battery *mb = dev_get_drvdata(b->dev.parent); |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 97 | |
| 98 | switch (mb->flag & 0x07) { |
| 99 | case MICRO_BATT_STATUS_HIGH: |
| 100 | return 100; |
| 101 | break; |
| 102 | case MICRO_BATT_STATUS_LOW: |
| 103 | return 50; |
| 104 | break; |
| 105 | case MICRO_BATT_STATUS_CRITICAL: |
| 106 | return 5; |
| 107 | break; |
| 108 | default: |
| 109 | break; |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int get_status(struct power_supply *b) |
| 115 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 116 | struct micro_battery *mb = dev_get_drvdata(b->dev.parent); |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 117 | |
| 118 | if (mb->flag == MICRO_BATT_STATUS_UNKNOWN) |
| 119 | return POWER_SUPPLY_STATUS_UNKNOWN; |
| 120 | |
| 121 | if (mb->flag & MICRO_BATT_STATUS_FULL) |
| 122 | return POWER_SUPPLY_STATUS_FULL; |
| 123 | |
| 124 | if ((mb->flag & MICRO_BATT_STATUS_CHARGING) || |
| 125 | (mb->flag & MICRO_BATT_STATUS_CHARGEMAIN)) |
| 126 | return POWER_SUPPLY_STATUS_CHARGING; |
| 127 | |
| 128 | return POWER_SUPPLY_STATUS_DISCHARGING; |
| 129 | } |
| 130 | |
| 131 | static int micro_batt_get_property(struct power_supply *b, |
| 132 | enum power_supply_property psp, |
| 133 | union power_supply_propval *val) |
| 134 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 135 | struct micro_battery *mb = dev_get_drvdata(b->dev.parent); |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 136 | |
| 137 | switch (psp) { |
| 138 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 139 | switch (mb->chemistry) { |
| 140 | case MICRO_BATT_CHEM_NICD: |
| 141 | val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd; |
| 142 | break; |
| 143 | case MICRO_BATT_CHEM_NIMH: |
| 144 | val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH; |
| 145 | break; |
| 146 | case MICRO_BATT_CHEM_LION: |
| 147 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 148 | break; |
| 149 | case MICRO_BATT_CHEM_LIPOLY: |
| 150 | val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO; |
| 151 | break; |
| 152 | default: |
| 153 | val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; |
| 154 | break; |
| 155 | }; |
| 156 | break; |
| 157 | case POWER_SUPPLY_PROP_STATUS: |
| 158 | val->intval = get_status(b); |
| 159 | break; |
| 160 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 161 | val->intval = 4700000; |
| 162 | break; |
| 163 | case POWER_SUPPLY_PROP_CAPACITY: |
| 164 | val->intval = get_capacity(b); |
| 165 | break; |
| 166 | case POWER_SUPPLY_PROP_TEMP: |
| 167 | val->intval = mb->temperature; |
| 168 | break; |
| 169 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 170 | val->intval = mb->voltage; |
| 171 | break; |
| 172 | default: |
| 173 | return -EINVAL; |
| 174 | }; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int micro_ac_get_property(struct power_supply *b, |
| 180 | enum power_supply_property psp, |
| 181 | union power_supply_propval *val) |
| 182 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 183 | struct micro_battery *mb = dev_get_drvdata(b->dev.parent); |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 184 | |
| 185 | switch (psp) { |
| 186 | case POWER_SUPPLY_PROP_ONLINE: |
| 187 | val->intval = mb->ac; |
| 188 | break; |
| 189 | default: |
| 190 | return -EINVAL; |
| 191 | }; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static enum power_supply_property micro_batt_power_props[] = { |
| 197 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 198 | POWER_SUPPLY_PROP_STATUS, |
| 199 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 200 | POWER_SUPPLY_PROP_CAPACITY, |
| 201 | POWER_SUPPLY_PROP_TEMP, |
| 202 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 203 | }; |
| 204 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 205 | static const struct power_supply_desc micro_batt_power_desc = { |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 206 | .name = "main-battery", |
| 207 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 208 | .properties = micro_batt_power_props, |
| 209 | .num_properties = ARRAY_SIZE(micro_batt_power_props), |
| 210 | .get_property = micro_batt_get_property, |
| 211 | .use_for_apm = 1, |
| 212 | }; |
| 213 | |
| 214 | static enum power_supply_property micro_ac_power_props[] = { |
| 215 | POWER_SUPPLY_PROP_ONLINE, |
| 216 | }; |
| 217 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 218 | static const struct power_supply_desc micro_ac_power_desc = { |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 219 | .name = "ac", |
| 220 | .type = POWER_SUPPLY_TYPE_MAINS, |
| 221 | .properties = micro_ac_power_props, |
| 222 | .num_properties = ARRAY_SIZE(micro_ac_power_props), |
| 223 | .get_property = micro_ac_get_property, |
| 224 | }; |
| 225 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 226 | static struct power_supply *micro_batt_power, *micro_ac_power; |
| 227 | |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 228 | static int micro_batt_probe(struct platform_device *pdev) |
| 229 | { |
| 230 | struct micro_battery *mb; |
Krzysztof Kozlowski | a2c1d53 | 2015-02-20 14:32:24 +0100 | [diff] [blame] | 231 | int ret; |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 232 | |
| 233 | mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL); |
| 234 | if (!mb) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | mb->micro = dev_get_drvdata(pdev->dev.parent); |
| 238 | mb->wq = create_singlethread_workqueue("ipaq-battery-wq"); |
Krzysztof Kozlowski | a2c1d53 | 2015-02-20 14:32:24 +0100 | [diff] [blame] | 239 | if (!mb->wq) |
| 240 | return -ENOMEM; |
| 241 | |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 242 | INIT_DELAYED_WORK(&mb->update, micro_battery_work); |
| 243 | platform_set_drvdata(pdev, mb); |
| 244 | queue_delayed_work(mb->wq, &mb->update, 1); |
Krzysztof Kozlowski | a2c1d53 | 2015-02-20 14:32:24 +0100 | [diff] [blame] | 245 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 246 | micro_batt_power = power_supply_register(&pdev->dev, |
| 247 | µ_batt_power_desc, NULL); |
| 248 | if (IS_ERR(micro_batt_power)) { |
| 249 | ret = PTR_ERR(micro_batt_power); |
Krzysztof Kozlowski | a2c1d53 | 2015-02-20 14:32:24 +0100 | [diff] [blame] | 250 | goto batt_err; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 251 | } |
Krzysztof Kozlowski | a2c1d53 | 2015-02-20 14:32:24 +0100 | [diff] [blame] | 252 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 253 | micro_ac_power = power_supply_register(&pdev->dev, |
| 254 | µ_ac_power_desc, NULL); |
| 255 | if (IS_ERR(micro_ac_power)) { |
| 256 | ret = PTR_ERR(micro_ac_power); |
Krzysztof Kozlowski | a2c1d53 | 2015-02-20 14:32:24 +0100 | [diff] [blame] | 257 | goto ac_err; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 258 | } |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 259 | |
| 260 | dev_info(&pdev->dev, "iPAQ micro battery driver\n"); |
| 261 | return 0; |
Krzysztof Kozlowski | a2c1d53 | 2015-02-20 14:32:24 +0100 | [diff] [blame] | 262 | |
| 263 | ac_err: |
Dan Carpenter | b9223da | 2016-03-18 12:00:51 +0300 | [diff] [blame] | 264 | power_supply_unregister(micro_batt_power); |
Krzysztof Kozlowski | a2c1d53 | 2015-02-20 14:32:24 +0100 | [diff] [blame] | 265 | batt_err: |
| 266 | cancel_delayed_work_sync(&mb->update); |
| 267 | destroy_workqueue(mb->wq); |
| 268 | return ret; |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static int micro_batt_remove(struct platform_device *pdev) |
| 272 | |
| 273 | { |
| 274 | struct micro_battery *mb = platform_get_drvdata(pdev); |
| 275 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 276 | power_supply_unregister(micro_ac_power); |
| 277 | power_supply_unregister(micro_batt_power); |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 278 | cancel_delayed_work_sync(&mb->update); |
Krzysztof Kozlowski | f852ec4 | 2015-02-20 14:32:23 +0100 | [diff] [blame] | 279 | destroy_workqueue(mb->wq); |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
Arnd Bergmann | 298cb0f | 2016-03-02 16:58:55 +0100 | [diff] [blame] | 284 | static int __maybe_unused micro_batt_suspend(struct device *dev) |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 285 | { |
| 286 | struct micro_battery *mb = dev_get_drvdata(dev); |
| 287 | |
| 288 | cancel_delayed_work_sync(&mb->update); |
| 289 | return 0; |
| 290 | } |
| 291 | |
Arnd Bergmann | 298cb0f | 2016-03-02 16:58:55 +0100 | [diff] [blame] | 292 | static int __maybe_unused micro_batt_resume(struct device *dev) |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 293 | { |
| 294 | struct micro_battery *mb = dev_get_drvdata(dev); |
| 295 | |
| 296 | queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD)); |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static const struct dev_pm_ops micro_batt_dev_pm_ops = { |
| 301 | SET_SYSTEM_SLEEP_PM_OPS(micro_batt_suspend, micro_batt_resume) |
| 302 | }; |
| 303 | |
Wei Yongjun | 5e37195 | 2014-07-28 21:14:21 +0800 | [diff] [blame] | 304 | static struct platform_driver micro_batt_device_driver = { |
Dmitry Artamonow | 00a588f | 2014-07-24 16:00:32 +0200 | [diff] [blame] | 305 | .driver = { |
| 306 | .name = "ipaq-micro-battery", |
| 307 | .pm = µ_batt_dev_pm_ops, |
| 308 | }, |
| 309 | .probe = micro_batt_probe, |
| 310 | .remove = micro_batt_remove, |
| 311 | }; |
| 312 | module_platform_driver(micro_batt_device_driver); |
| 313 | |
| 314 | MODULE_LICENSE("GPL"); |
| 315 | MODULE_DESCRIPTION("driver for iPAQ Atmel micro battery"); |
| 316 | MODULE_ALIAS("platform:battery-ipaq-micro"); |