blob: aacfcd6954a371ab23e47491230818a644b7e772 [file] [log] [blame]
Marc Dietrich162c7d82011-09-27 19:00:40 +02001/*
2 * nvec_power: power supply driver for a NVIDIA compliant embedded controller
3 *
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net>
5 *
6 * Authors: Ilya Petrov <ilya.muromec@gmail.com>
7 * Marc Dietrich <marvin24@gmx.de>
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
11 * for more details.
12 *
13 */
14
Marc Dietrich32890b92011-05-19 16:34:42 +020015#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/err.h>
18#include <linux/power_supply.h>
19#include <linux/slab.h>
20#include <linux/workqueue.h>
21#include <linux/delay.h>
Marc Dietrich162c7d82011-09-27 19:00:40 +020022
Marc Dietrich32890b92011-05-19 16:34:42 +020023#include "nvec.h"
24
Marc Dietrich93eff832013-01-27 17:43:43 +010025#define GET_SYSTEM_STATUS 0x00
26
Marc Dietrich162c7d82011-09-27 19:00:40 +020027struct nvec_power {
Marc Dietrich32890b92011-05-19 16:34:42 +020028 struct notifier_block notifier;
29 struct delayed_work poller;
30 struct nvec_chip *nvec;
31 int on;
32 int bat_present;
33 int bat_status;
34 int bat_voltage_now;
35 int bat_current_now;
36 int bat_current_avg;
37 int time_remain;
38 int charge_full_design;
39 int charge_last_full;
40 int critical_capacity;
41 int capacity_remain;
42 int bat_temperature;
43 int bat_cap;
44 int bat_type_enum;
45 char bat_manu[30];
46 char bat_model[30];
47 char bat_type[30];
48};
49
50enum {
51 SLOT_STATUS,
52 VOLTAGE,
53 TIME_REMAINING,
54 CURRENT,
55 AVERAGE_CURRENT,
56 AVERAGING_TIME_INTERVAL,
57 CAPACITY_REMAINING,
58 LAST_FULL_CHARGE_CAPACITY,
59 DESIGN_CAPACITY,
60 CRITICAL_CAPACITY,
61 TEMPERATURE,
62 MANUFACTURER,
63 MODEL,
64 TYPE,
65};
66
67enum {
68 AC,
69 BAT,
70};
71
72struct bat_response {
73 u8 event_type;
74 u8 length;
75 u8 sub_type;
76 u8 status;
Marc Dietrich162c7d82011-09-27 19:00:40 +020077 /* payload */
78 union {
Marc Dietrich32890b92011-05-19 16:34:42 +020079 char plc[30];
80 u16 plu;
81 s16 pls;
82 };
83};
84
85static struct power_supply nvec_bat_psy;
86static struct power_supply nvec_psy;
87
88static int nvec_power_notifier(struct notifier_block *nb,
Marc Dietrich162c7d82011-09-27 19:00:40 +020089 unsigned long event_type, void *data)
Marc Dietrich32890b92011-05-19 16:34:42 +020090{
Marc Dietrich162c7d82011-09-27 19:00:40 +020091 struct nvec_power *power =
92 container_of(nb, struct nvec_power, notifier);
Marc Dietrich32890b92011-05-19 16:34:42 +020093 struct bat_response *res = (struct bat_response *)data;
94
95 if (event_type != NVEC_SYS)
96 return NOTIFY_DONE;
97
Marc Dietrich162c7d82011-09-27 19:00:40 +020098 if (res->sub_type == 0) {
99 if (power->on != res->plu) {
Marc Dietrich32890b92011-05-19 16:34:42 +0200100 power->on = res->plu;
101 power_supply_changed(&nvec_psy);
102 }
103 return NOTIFY_STOP;
104 }
105 return NOTIFY_OK;
106}
107
Marc Dietrich162c7d82011-09-27 19:00:40 +0200108static const int bat_init[] = {
Marc Dietrich32890b92011-05-19 16:34:42 +0200109 LAST_FULL_CHARGE_CAPACITY, DESIGN_CAPACITY, CRITICAL_CAPACITY,
110 MANUFACTURER, MODEL, TYPE,
111};
112
113static void get_bat_mfg_data(struct nvec_power *power)
114{
115 int i;
Marc Dietrich93eff832013-01-27 17:43:43 +0100116 char buf[] = { NVEC_BAT, SLOT_STATUS };
Marc Dietrich32890b92011-05-19 16:34:42 +0200117
Marc Dietrich162c7d82011-09-27 19:00:40 +0200118 for (i = 0; i < ARRAY_SIZE(bat_init); i++) {
Marc Dietrich32890b92011-05-19 16:34:42 +0200119 buf[1] = bat_init[i];
120 nvec_write_async(power->nvec, buf, 2);
121 }
122}
123
124static int nvec_power_bat_notifier(struct notifier_block *nb,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200125 unsigned long event_type, void *data)
Marc Dietrich32890b92011-05-19 16:34:42 +0200126{
Marc Dietrich162c7d82011-09-27 19:00:40 +0200127 struct nvec_power *power =
128 container_of(nb, struct nvec_power, notifier);
Marc Dietrich32890b92011-05-19 16:34:42 +0200129 struct bat_response *res = (struct bat_response *)data;
130 int status_changed = 0;
131
132 if (event_type != NVEC_BAT)
133 return NOTIFY_DONE;
134
Marc Dietrich162c7d82011-09-27 19:00:40 +0200135 switch (res->sub_type) {
136 case SLOT_STATUS:
137 if (res->plc[0] & 1) {
138 if (power->bat_present == 0) {
139 status_changed = 1;
140 get_bat_mfg_data(power);
141 }
Marc Dietrich32890b92011-05-19 16:34:42 +0200142
Marc Dietrich162c7d82011-09-27 19:00:40 +0200143 power->bat_present = 1;
Marc Dietrich32890b92011-05-19 16:34:42 +0200144
Marc Dietrich162c7d82011-09-27 19:00:40 +0200145 switch ((res->plc[0] >> 1) & 3) {
146 case 0:
147 power->bat_status =
148 POWER_SUPPLY_STATUS_NOT_CHARGING;
149 break;
150 case 1:
151 power->bat_status =
152 POWER_SUPPLY_STATUS_CHARGING;
153 break;
154 case 2:
155 power->bat_status =
156 POWER_SUPPLY_STATUS_DISCHARGING;
157 break;
158 default:
Marc Dietrich32890b92011-05-19 16:34:42 +0200159 power->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
160 }
Marc Dietrich162c7d82011-09-27 19:00:40 +0200161 } else {
162 if (power->bat_present == 1)
163 status_changed = 1;
164
165 power->bat_present = 0;
166 power->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
167 }
168 power->bat_cap = res->plc[1];
169 if (status_changed)
170 power_supply_changed(&nvec_bat_psy);
171 break;
172 case VOLTAGE:
173 power->bat_voltage_now = res->plu * 1000;
174 break;
175 case TIME_REMAINING:
176 power->time_remain = res->plu * 3600;
177 break;
178 case CURRENT:
179 power->bat_current_now = res->pls * 1000;
180 break;
181 case AVERAGE_CURRENT:
182 power->bat_current_avg = res->pls * 1000;
183 break;
184 case CAPACITY_REMAINING:
185 power->capacity_remain = res->plu * 1000;
186 break;
187 case LAST_FULL_CHARGE_CAPACITY:
188 power->charge_last_full = res->plu * 1000;
189 break;
190 case DESIGN_CAPACITY:
191 power->charge_full_design = res->plu * 1000;
192 break;
193 case CRITICAL_CAPACITY:
194 power->critical_capacity = res->plu * 1000;
195 break;
196 case TEMPERATURE:
197 power->bat_temperature = res->plu - 2732;
198 break;
199 case MANUFACTURER:
200 memcpy(power->bat_manu, &res->plc, res->length - 2);
201 power->bat_model[res->length - 2] = '\0';
202 break;
203 case MODEL:
204 memcpy(power->bat_model, &res->plc, res->length - 2);
205 power->bat_model[res->length - 2] = '\0';
206 break;
207 case TYPE:
208 memcpy(power->bat_type, &res->plc, res->length - 2);
209 power->bat_type[res->length - 2] = '\0';
210 /* this differs a little from the spec
211 fill in more if you find some */
212 if (!strncmp(power->bat_type, "Li", 30))
213 power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_LION;
214 else
215 power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
216 break;
217 default:
218 return NOTIFY_STOP;
Marc Dietrich32890b92011-05-19 16:34:42 +0200219 }
220
221 return NOTIFY_STOP;
222}
223
224static int nvec_power_get_property(struct power_supply *psy,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200225 enum power_supply_property psp,
226 union power_supply_propval *val)
Marc Dietrich32890b92011-05-19 16:34:42 +0200227{
228 struct nvec_power *power = dev_get_drvdata(psy->dev->parent);
229 switch (psp) {
230 case POWER_SUPPLY_PROP_ONLINE:
231 val->intval = power->on;
232 break;
233 default:
234 return -EINVAL;
235 }
236 return 0;
237}
238
239static int nvec_battery_get_property(struct power_supply *psy,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200240 enum power_supply_property psp,
241 union power_supply_propval *val)
Marc Dietrich32890b92011-05-19 16:34:42 +0200242{
243 struct nvec_power *power = dev_get_drvdata(psy->dev->parent);
244
Marc Dietrich162c7d82011-09-27 19:00:40 +0200245 switch (psp) {
246 case POWER_SUPPLY_PROP_STATUS:
247 val->intval = power->bat_status;
248 break;
249 case POWER_SUPPLY_PROP_CAPACITY:
250 val->intval = power->bat_cap;
251 break;
252 case POWER_SUPPLY_PROP_PRESENT:
253 val->intval = power->bat_present;
254 break;
255 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
256 val->intval = power->bat_voltage_now;
257 break;
258 case POWER_SUPPLY_PROP_CURRENT_NOW:
259 val->intval = power->bat_current_now;
260 break;
261 case POWER_SUPPLY_PROP_CURRENT_AVG:
262 val->intval = power->bat_current_avg;
263 break;
264 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
265 val->intval = power->time_remain;
266 break;
267 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
268 val->intval = power->charge_full_design;
269 break;
270 case POWER_SUPPLY_PROP_CHARGE_FULL:
271 val->intval = power->charge_last_full;
272 break;
273 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
274 val->intval = power->critical_capacity;
275 break;
276 case POWER_SUPPLY_PROP_CHARGE_NOW:
277 val->intval = power->capacity_remain;
278 break;
279 case POWER_SUPPLY_PROP_TEMP:
280 val->intval = power->bat_temperature;
281 break;
282 case POWER_SUPPLY_PROP_MANUFACTURER:
283 val->strval = power->bat_manu;
284 break;
285 case POWER_SUPPLY_PROP_MODEL_NAME:
286 val->strval = power->bat_model;
287 break;
288 case POWER_SUPPLY_PROP_TECHNOLOGY:
289 val->intval = power->bat_type_enum;
290 break;
291 default:
292 return -EINVAL;
293 }
Marc Dietrich32890b92011-05-19 16:34:42 +0200294 return 0;
295}
296
297static enum power_supply_property nvec_power_props[] = {
298 POWER_SUPPLY_PROP_ONLINE,
299};
300
301static enum power_supply_property nvec_battery_props[] = {
302 POWER_SUPPLY_PROP_STATUS,
303 POWER_SUPPLY_PROP_PRESENT,
304 POWER_SUPPLY_PROP_CAPACITY,
305 POWER_SUPPLY_PROP_VOLTAGE_NOW,
306 POWER_SUPPLY_PROP_CURRENT_NOW,
307#ifdef EC_FULL_DIAG
308 POWER_SUPPLY_PROP_CURRENT_AVG,
309 POWER_SUPPLY_PROP_TEMP,
310 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
311#endif
312 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
313 POWER_SUPPLY_PROP_CHARGE_FULL,
314 POWER_SUPPLY_PROP_CHARGE_EMPTY,
315 POWER_SUPPLY_PROP_CHARGE_NOW,
316 POWER_SUPPLY_PROP_MANUFACTURER,
317 POWER_SUPPLY_PROP_MODEL_NAME,
318 POWER_SUPPLY_PROP_TECHNOLOGY,
319};
320
321static char *nvec_power_supplied_to[] = {
322 "battery",
323};
324
325static struct power_supply nvec_bat_psy = {
Marc Dietrich162c7d82011-09-27 19:00:40 +0200326 .name = "battery",
327 .type = POWER_SUPPLY_TYPE_BATTERY,
328 .properties = nvec_battery_props,
329 .num_properties = ARRAY_SIZE(nvec_battery_props),
330 .get_property = nvec_battery_get_property,
Marc Dietrich32890b92011-05-19 16:34:42 +0200331};
332
333static struct power_supply nvec_psy = {
334 .name = "ac",
335 .type = POWER_SUPPLY_TYPE_MAINS,
336 .supplied_to = nvec_power_supplied_to,
337 .num_supplicants = ARRAY_SIZE(nvec_power_supplied_to),
338 .properties = nvec_power_props,
339 .num_properties = ARRAY_SIZE(nvec_power_props),
340 .get_property = nvec_power_get_property,
341};
342
Marc Dietrich162c7d82011-09-27 19:00:40 +0200343static int counter;
344static int const bat_iter[] = {
Marc Dietrich32890b92011-05-19 16:34:42 +0200345 SLOT_STATUS, VOLTAGE, CURRENT, CAPACITY_REMAINING,
346#ifdef EC_FULL_DIAG
347 AVERAGE_CURRENT, TEMPERATURE, TIME_REMAINING,
348#endif
349};
350
351static void nvec_power_poll(struct work_struct *work)
352{
Marc Dietrich93eff832013-01-27 17:43:43 +0100353 char buf[] = { NVEC_SYS, GET_SYSTEM_STATUS };
Marc Dietrich32890b92011-05-19 16:34:42 +0200354 struct nvec_power *power = container_of(work, struct nvec_power,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200355 poller.work);
Marc Dietrich32890b92011-05-19 16:34:42 +0200356
357 if (counter >= ARRAY_SIZE(bat_iter))
358 counter = 0;
359
360/* AC status via sys req */
361 nvec_write_async(power->nvec, buf, 2);
362 msleep(100);
363
364/* select a battery request function via round robin
365 doing it all at once seems to overload the power supply */
Marc Dietrich93eff832013-01-27 17:43:43 +0100366 buf[0] = NVEC_BAT;
Marc Dietrich162c7d82011-09-27 19:00:40 +0200367 buf[1] = bat_iter[counter++];
Marc Dietrich32890b92011-05-19 16:34:42 +0200368 nvec_write_async(power->nvec, buf, 2);
369
Marc Dietrich32890b92011-05-19 16:34:42 +0200370 schedule_delayed_work(to_delayed_work(work), msecs_to_jiffies(5000));
371};
372
Bill Pemberton46620802012-11-19 13:22:07 -0500373static int nvec_power_probe(struct platform_device *pdev)
Marc Dietrich32890b92011-05-19 16:34:42 +0200374{
375 struct power_supply *psy;
Marc Dietrichf5e33522012-06-24 23:25:16 +0200376 struct nvec_power *power;
Marc Dietrich32890b92011-05-19 16:34:42 +0200377 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
378
Marc Dietrichf5e33522012-06-24 23:25:16 +0200379 power = devm_kzalloc(&pdev->dev, sizeof(struct nvec_power), GFP_NOWAIT);
380 if (power == NULL)
381 return -ENOMEM;
382
Marc Dietrich32890b92011-05-19 16:34:42 +0200383 dev_set_drvdata(&pdev->dev, power);
384 power->nvec = nvec;
385
386 switch (pdev->id) {
387 case AC:
388 psy = &nvec_psy;
389
390 power->notifier.notifier_call = nvec_power_notifier;
391
392 INIT_DELAYED_WORK(&power->poller, nvec_power_poll);
393 schedule_delayed_work(&power->poller, msecs_to_jiffies(5000));
394 break;
395 case BAT:
396 psy = &nvec_bat_psy;
397
Marc Dietrich162c7d82011-09-27 19:00:40 +0200398 power->notifier.notifier_call = nvec_power_bat_notifier;
Marc Dietrich32890b92011-05-19 16:34:42 +0200399 break;
400 default:
Marc Dietrich32890b92011-05-19 16:34:42 +0200401 return -ENODEV;
402 }
403
404 nvec_register_notifier(nvec, &power->notifier, NVEC_SYS);
405
406 if (pdev->id == BAT)
407 get_bat_mfg_data(power);
408
409 return power_supply_register(&pdev->dev, psy);
410}
411
Bill Pemberton1a6a8a82012-11-19 13:26:41 -0500412static int nvec_power_remove(struct platform_device *pdev)
Marc Dietrich3cdde3a2012-06-24 23:25:21 +0200413{
414 struct nvec_power *power = platform_get_drvdata(pdev);
415
416 cancel_delayed_work_sync(&power->poller);
Marc Dietrichc2b62f62013-04-29 23:14:52 +0200417 nvec_unregister_notifier(power->nvec, &power->notifier);
Marc Dietrich3cdde3a2012-06-24 23:25:21 +0200418 switch (pdev->id) {
419 case AC:
420 power_supply_unregister(&nvec_psy);
421 break;
422 case BAT:
423 power_supply_unregister(&nvec_bat_psy);
424 }
425
426 return 0;
427}
428
Marc Dietrich32890b92011-05-19 16:34:42 +0200429static struct platform_driver nvec_power_driver = {
430 .probe = nvec_power_probe,
Bill Pemberton44b90a32012-11-19 13:20:55 -0500431 .remove = nvec_power_remove,
Marc Dietrich32890b92011-05-19 16:34:42 +0200432 .driver = {
Marc Dietrich162c7d82011-09-27 19:00:40 +0200433 .name = "nvec-power",
434 .owner = THIS_MODULE,
435 }
Marc Dietrich32890b92011-05-19 16:34:42 +0200436};
437
Marc Dietrich9891b1c2012-06-24 23:25:18 +0200438module_platform_driver(nvec_power_driver);
Marc Dietrich32890b92011-05-19 16:34:42 +0200439
440MODULE_AUTHOR("Ilya Petrov <ilya.muromec@gmail.com>");
441MODULE_LICENSE("GPL");
442MODULE_DESCRIPTION("NVEC battery and AC driver");
443MODULE_ALIAS("platform:nvec-power");