Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v3.15
  1/*
  2 * Gas Gauge driver for SBS Compliant Batteries
  3 *
  4 * Copyright (c) 2010, NVIDIA Corporation.
  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 as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful, but WITHOUT
 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 14 * more details.
 15 *
 16 * You should have received a copy of the GNU General Public License along
 17 * with this program; if not, write to the Free Software Foundation, Inc.,
 18 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 19 */
 20
 21#include <linux/init.h>
 22#include <linux/module.h>
 23#include <linux/kernel.h>
 24#include <linux/err.h>
 25#include <linux/power_supply.h>
 26#include <linux/i2c.h>
 27#include <linux/slab.h>
 28#include <linux/interrupt.h>
 29#include <linux/gpio.h>
 30#include <linux/of.h>
 
 31
 32#include <linux/power/sbs-battery.h>
 33
 34enum {
 35	REG_MANUFACTURER_DATA,
 36	REG_TEMPERATURE,
 37	REG_VOLTAGE,
 38	REG_CURRENT,
 39	REG_CAPACITY,
 40	REG_TIME_TO_EMPTY,
 41	REG_TIME_TO_FULL,
 42	REG_STATUS,
 43	REG_CYCLE_COUNT,
 44	REG_SERIAL_NUMBER,
 45	REG_REMAINING_CAPACITY,
 46	REG_REMAINING_CAPACITY_CHARGE,
 47	REG_FULL_CHARGE_CAPACITY,
 48	REG_FULL_CHARGE_CAPACITY_CHARGE,
 49	REG_DESIGN_CAPACITY,
 50	REG_DESIGN_CAPACITY_CHARGE,
 51	REG_DESIGN_VOLTAGE,
 
 
 
 52};
 53
 54/* Battery Mode defines */
 55#define BATTERY_MODE_OFFSET		0x03
 56#define BATTERY_MODE_MASK		0x8000
 57enum sbs_battery_mode {
 58	BATTERY_MODE_AMPS,
 59	BATTERY_MODE_WATTS
 60};
 61
 62/* manufacturer access defines */
 63#define MANUFACTURER_ACCESS_STATUS	0x0006
 64#define MANUFACTURER_ACCESS_SLEEP	0x0011
 65
 66/* battery status value bits */
 67#define BATTERY_DISCHARGING		0x40
 68#define BATTERY_FULL_CHARGED		0x20
 69#define BATTERY_FULL_DISCHARGED		0x10
 70
 
 71#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
 72	.psp = _psp, \
 73	.addr = _addr, \
 74	.min_value = _min_value, \
 75	.max_value = _max_value, \
 76}
 77
 78static const struct chip_data {
 79	enum power_supply_property psp;
 80	u8 addr;
 81	int min_value;
 82	int max_value;
 83} sbs_data[] = {
 84	[REG_MANUFACTURER_DATA] =
 85		SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
 86	[REG_TEMPERATURE] =
 87		SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
 88	[REG_VOLTAGE] =
 89		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
 90	[REG_CURRENT] =
 91		SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
 92	[REG_CAPACITY] =
 93		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
 94	[REG_REMAINING_CAPACITY] =
 95		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
 96	[REG_REMAINING_CAPACITY_CHARGE] =
 97		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
 98	[REG_FULL_CHARGE_CAPACITY] =
 99		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
100	[REG_FULL_CHARGE_CAPACITY_CHARGE] =
101		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
102	[REG_TIME_TO_EMPTY] =
103		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
104	[REG_TIME_TO_FULL] =
105		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
106	[REG_STATUS] =
107		SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
108	[REG_CYCLE_COUNT] =
109		SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
110	[REG_DESIGN_CAPACITY] =
111		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
112	[REG_DESIGN_CAPACITY_CHARGE] =
113		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
114	[REG_DESIGN_VOLTAGE] =
 
 
115		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
116	[REG_SERIAL_NUMBER] =
117		SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
 
 
 
 
 
118};
119
120static enum power_supply_property sbs_properties[] = {
121	POWER_SUPPLY_PROP_STATUS,
122	POWER_SUPPLY_PROP_HEALTH,
123	POWER_SUPPLY_PROP_PRESENT,
124	POWER_SUPPLY_PROP_TECHNOLOGY,
125	POWER_SUPPLY_PROP_CYCLE_COUNT,
126	POWER_SUPPLY_PROP_VOLTAGE_NOW,
127	POWER_SUPPLY_PROP_CURRENT_NOW,
128	POWER_SUPPLY_PROP_CAPACITY,
129	POWER_SUPPLY_PROP_TEMP,
130	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
131	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
132	POWER_SUPPLY_PROP_SERIAL_NUMBER,
 
133	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
134	POWER_SUPPLY_PROP_ENERGY_NOW,
135	POWER_SUPPLY_PROP_ENERGY_FULL,
136	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
137	POWER_SUPPLY_PROP_CHARGE_NOW,
138	POWER_SUPPLY_PROP_CHARGE_FULL,
139	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 
 
 
140};
141
142struct sbs_info {
143	struct i2c_client		*client;
144	struct power_supply		power_supply;
145	struct sbs_platform_data	*pdata;
146	bool				is_present;
147	bool				gpio_detect;
148	bool				enable_detection;
149	int				irq;
150	int				last_state;
151	int				poll_time;
152	struct delayed_work		work;
153	int				ignore_changes;
154};
155
 
 
 
 
156static int sbs_read_word_data(struct i2c_client *client, u8 address)
157{
158	struct sbs_info *chip = i2c_get_clientdata(client);
159	s32 ret = 0;
160	int retries = 1;
161
162	if (chip->pdata)
163		retries = max(chip->pdata->i2c_retry_count + 1, 1);
164
165	while (retries > 0) {
166		ret = i2c_smbus_read_word_data(client, address);
167		if (ret >= 0)
168			break;
169		retries--;
170	}
171
172	if (ret < 0) {
173		dev_dbg(&client->dev,
174			"%s: i2c read at address 0x%x failed\n",
175			__func__, address);
176		return ret;
177	}
178
179	return le16_to_cpu(ret);
180}
181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182static int sbs_write_word_data(struct i2c_client *client, u8 address,
183	u16 value)
184{
185	struct sbs_info *chip = i2c_get_clientdata(client);
186	s32 ret = 0;
187	int retries = 1;
188
189	if (chip->pdata)
190		retries = max(chip->pdata->i2c_retry_count + 1, 1);
191
192	while (retries > 0) {
193		ret = i2c_smbus_write_word_data(client, address,
194			le16_to_cpu(value));
195		if (ret >= 0)
196			break;
197		retries--;
198	}
199
200	if (ret < 0) {
201		dev_dbg(&client->dev,
202			"%s: i2c write to address 0x%x failed\n",
203			__func__, address);
204		return ret;
205	}
206
207	return 0;
208}
209
210static int sbs_get_battery_presence_and_health(
211	struct i2c_client *client, enum power_supply_property psp,
212	union power_supply_propval *val)
213{
214	s32 ret;
215	struct sbs_info *chip = i2c_get_clientdata(client);
216
217	if (psp == POWER_SUPPLY_PROP_PRESENT &&
218		chip->gpio_detect) {
219		ret = gpio_get_value(chip->pdata->battery_detect);
220		if (ret == chip->pdata->battery_detect_present)
221			val->intval = 1;
222		else
223			val->intval = 0;
224		chip->is_present = val->intval;
225		return ret;
226	}
227
228	/* Write to ManufacturerAccess with
229	 * ManufacturerAccess command and then
230	 * read the status */
231	ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
232					MANUFACTURER_ACCESS_STATUS);
233	if (ret < 0) {
234		if (psp == POWER_SUPPLY_PROP_PRESENT)
235			val->intval = 0; /* battery removed */
236		return ret;
237	}
238
239	ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
240	if (ret < 0)
241		return ret;
242
243	if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
244	    ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
245		val->intval = 0;
246		return 0;
247	}
248
249	/* Mask the upper nibble of 2nd byte and
250	 * lower byte of response then
251	 * shift the result by 8 to get status*/
252	ret &= 0x0F00;
253	ret >>= 8;
254	if (psp == POWER_SUPPLY_PROP_PRESENT) {
255		if (ret == 0x0F)
256			/* battery removed */
257			val->intval = 0;
258		else
259			val->intval = 1;
260	} else if (psp == POWER_SUPPLY_PROP_HEALTH) {
261		if (ret == 0x09)
262			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
263		else if (ret == 0x0B)
264			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
265		else if (ret == 0x0C)
266			val->intval = POWER_SUPPLY_HEALTH_DEAD;
267		else
268			val->intval = POWER_SUPPLY_HEALTH_GOOD;
269	}
270
271	return 0;
272}
273
274static int sbs_get_battery_property(struct i2c_client *client,
275	int reg_offset, enum power_supply_property psp,
276	union power_supply_propval *val)
277{
278	struct sbs_info *chip = i2c_get_clientdata(client);
279	s32 ret;
280
281	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
282	if (ret < 0)
283		return ret;
284
285	/* returned values are 16 bit */
286	if (sbs_data[reg_offset].min_value < 0)
287		ret = (s16)ret;
288
289	if (ret >= sbs_data[reg_offset].min_value &&
290	    ret <= sbs_data[reg_offset].max_value) {
291		val->intval = ret;
292		if (psp != POWER_SUPPLY_PROP_STATUS)
293			return 0;
294
295		if (ret & BATTERY_FULL_CHARGED)
296			val->intval = POWER_SUPPLY_STATUS_FULL;
297		else if (ret & BATTERY_FULL_DISCHARGED)
298			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
299		else if (ret & BATTERY_DISCHARGING)
300			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
301		else
302			val->intval = POWER_SUPPLY_STATUS_CHARGING;
303
304		if (chip->poll_time == 0)
305			chip->last_state = val->intval;
306		else if (chip->last_state != val->intval) {
307			cancel_delayed_work_sync(&chip->work);
308			power_supply_changed(&chip->power_supply);
309			chip->poll_time = 0;
310		}
311	} else {
312		if (psp == POWER_SUPPLY_PROP_STATUS)
313			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
314		else
315			val->intval = 0;
316	}
317
318	return 0;
319}
320
 
 
 
 
 
 
 
 
 
 
 
 
 
321static void  sbs_unit_adjustment(struct i2c_client *client,
322	enum power_supply_property psp, union power_supply_propval *val)
323{
324#define BASE_UNIT_CONVERSION		1000
325#define BATTERY_MODE_CAP_MULT_WATT	(10 * BASE_UNIT_CONVERSION)
326#define TIME_UNIT_CONVERSION		60
327#define TEMP_KELVIN_TO_CELSIUS		2731
328	switch (psp) {
329	case POWER_SUPPLY_PROP_ENERGY_NOW:
330	case POWER_SUPPLY_PROP_ENERGY_FULL:
331	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
332		/* sbs provides energy in units of 10mWh.
333		 * Convert to µWh
334		 */
335		val->intval *= BATTERY_MODE_CAP_MULT_WATT;
336		break;
337
338	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 
339	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
340	case POWER_SUPPLY_PROP_CURRENT_NOW:
341	case POWER_SUPPLY_PROP_CHARGE_NOW:
342	case POWER_SUPPLY_PROP_CHARGE_FULL:
343	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
344		val->intval *= BASE_UNIT_CONVERSION;
345		break;
346
347	case POWER_SUPPLY_PROP_TEMP:
348		/* sbs provides battery temperature in 0.1K
349		 * so convert it to 0.1°C
350		 */
351		val->intval -= TEMP_KELVIN_TO_CELSIUS;
352		break;
353
354	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
355	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
356		/* sbs provides time to empty and time to full in minutes.
357		 * Convert to seconds
358		 */
359		val->intval *= TIME_UNIT_CONVERSION;
360		break;
361
362	default:
363		dev_dbg(&client->dev,
364			"%s: no need for unit conversion %d\n", __func__, psp);
365	}
366}
367
368static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
369	enum sbs_battery_mode mode)
370{
371	int ret, original_val;
372
373	original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
374	if (original_val < 0)
375		return original_val;
376
377	if ((original_val & BATTERY_MODE_MASK) == mode)
378		return mode;
379
380	if (mode == BATTERY_MODE_AMPS)
381		ret = original_val & ~BATTERY_MODE_MASK;
382	else
383		ret = original_val | BATTERY_MODE_MASK;
384
385	ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
386	if (ret < 0)
387		return ret;
388
389	return original_val & BATTERY_MODE_MASK;
390}
391
392static int sbs_get_battery_capacity(struct i2c_client *client,
393	int reg_offset, enum power_supply_property psp,
394	union power_supply_propval *val)
395{
396	s32 ret;
397	enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
398
399	if (power_supply_is_amp_property(psp))
400		mode = BATTERY_MODE_AMPS;
401
402	mode = sbs_set_battery_mode(client, mode);
403	if (mode < 0)
404		return mode;
405
406	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
407	if (ret < 0)
408		return ret;
409
410	if (psp == POWER_SUPPLY_PROP_CAPACITY) {
411		/* sbs spec says that this can be >100 %
412		* even if max value is 100 % */
413		val->intval = min(ret, 100);
414	} else
415		val->intval = ret;
416
417	ret = sbs_set_battery_mode(client, mode);
418	if (ret < 0)
419		return ret;
420
421	return 0;
422}
423
424static char sbs_serial[5];
425static int sbs_get_battery_serial_number(struct i2c_client *client,
426	union power_supply_propval *val)
427{
428	int ret;
429
430	ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
431	if (ret < 0)
432		return ret;
433
434	ret = sprintf(sbs_serial, "%04x", ret);
435	val->strval = sbs_serial;
436
437	return 0;
438}
439
440static int sbs_get_property_index(struct i2c_client *client,
441	enum power_supply_property psp)
442{
443	int count;
444	for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
445		if (psp == sbs_data[count].psp)
446			return count;
447
448	dev_warn(&client->dev,
449		"%s: Invalid Property - %d\n", __func__, psp);
450
451	return -EINVAL;
452}
453
454static int sbs_get_property(struct power_supply *psy,
455	enum power_supply_property psp,
456	union power_supply_propval *val)
457{
458	int ret = 0;
459	struct sbs_info *chip = container_of(psy,
460				struct sbs_info, power_supply);
461	struct i2c_client *client = chip->client;
462
463	switch (psp) {
464	case POWER_SUPPLY_PROP_PRESENT:
465	case POWER_SUPPLY_PROP_HEALTH:
466		ret = sbs_get_battery_presence_and_health(client, psp, val);
467		if (psp == POWER_SUPPLY_PROP_PRESENT)
468			return 0;
469		break;
470
471	case POWER_SUPPLY_PROP_TECHNOLOGY:
472		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
473		goto done; /* don't trigger power_supply_changed()! */
474
475	case POWER_SUPPLY_PROP_ENERGY_NOW:
476	case POWER_SUPPLY_PROP_ENERGY_FULL:
477	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
478	case POWER_SUPPLY_PROP_CHARGE_NOW:
479	case POWER_SUPPLY_PROP_CHARGE_FULL:
480	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
481	case POWER_SUPPLY_PROP_CAPACITY:
482		ret = sbs_get_property_index(client, psp);
483		if (ret < 0)
484			break;
485
486		ret = sbs_get_battery_capacity(client, ret, psp, val);
487		break;
488
489	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
490		ret = sbs_get_battery_serial_number(client, val);
491		break;
492
493	case POWER_SUPPLY_PROP_STATUS:
494	case POWER_SUPPLY_PROP_CYCLE_COUNT:
495	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
496	case POWER_SUPPLY_PROP_CURRENT_NOW:
497	case POWER_SUPPLY_PROP_TEMP:
498	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
499	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
 
500	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
501		ret = sbs_get_property_index(client, psp);
502		if (ret < 0)
503			break;
504
505		ret = sbs_get_battery_property(client, ret, psp, val);
506		break;
507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508	default:
509		dev_err(&client->dev,
510			"%s: INVALID property\n", __func__);
511		return -EINVAL;
512	}
513
514	if (!chip->enable_detection)
515		goto done;
516
517	if (!chip->gpio_detect &&
518		chip->is_present != (ret >= 0)) {
519		chip->is_present = (ret >= 0);
520		power_supply_changed(&chip->power_supply);
521	}
522
523done:
524	if (!ret) {
525		/* Convert units to match requirements for power supply class */
526		sbs_unit_adjustment(client, psp, val);
527	}
528
529	dev_dbg(&client->dev,
530		"%s: property = %d, value = %x\n", __func__, psp, val->intval);
531
532	if (ret && chip->is_present)
533		return ret;
534
535	/* battery not present, so return NODATA for properties */
536	if (ret)
537		return -ENODATA;
538
539	return 0;
540}
541
542static irqreturn_t sbs_irq(int irq, void *devid)
543{
544	struct power_supply *battery = devid;
545
546	power_supply_changed(battery);
547
548	return IRQ_HANDLED;
549}
550
551static void sbs_external_power_changed(struct power_supply *psy)
552{
553	struct sbs_info *chip;
554
555	chip = container_of(psy, struct sbs_info, power_supply);
556
557	if (chip->ignore_changes > 0) {
558		chip->ignore_changes--;
559		return;
560	}
561
562	/* cancel outstanding work */
563	cancel_delayed_work_sync(&chip->work);
564
565	schedule_delayed_work(&chip->work, HZ);
566	chip->poll_time = chip->pdata->poll_retry_count;
567}
568
569static void sbs_delayed_work(struct work_struct *work)
570{
571	struct sbs_info *chip;
572	s32 ret;
573
574	chip = container_of(work, struct sbs_info, work.work);
575
576	ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
577	/* if the read failed, give up on this work */
578	if (ret < 0) {
579		chip->poll_time = 0;
580		return;
581	}
582
583	if (ret & BATTERY_FULL_CHARGED)
584		ret = POWER_SUPPLY_STATUS_FULL;
585	else if (ret & BATTERY_FULL_DISCHARGED)
586		ret = POWER_SUPPLY_STATUS_NOT_CHARGING;
587	else if (ret & BATTERY_DISCHARGING)
588		ret = POWER_SUPPLY_STATUS_DISCHARGING;
589	else
590		ret = POWER_SUPPLY_STATUS_CHARGING;
591
592	if (chip->last_state != ret) {
593		chip->poll_time = 0;
594		power_supply_changed(&chip->power_supply);
595		return;
596	}
597	if (chip->poll_time > 0) {
598		schedule_delayed_work(&chip->work, HZ);
599		chip->poll_time--;
600		return;
601	}
602}
603
604#if defined(CONFIG_OF)
605
606#include <linux/of_device.h>
607#include <linux/of_gpio.h>
608
609static const struct of_device_id sbs_dt_ids[] = {
610	{ .compatible = "sbs,sbs-battery" },
611	{ .compatible = "ti,bq20z75" },
612	{ }
613};
614MODULE_DEVICE_TABLE(of, sbs_dt_ids);
615
616static struct sbs_platform_data *sbs_of_populate_pdata(
617		struct i2c_client *client)
618{
619	struct device_node *of_node = client->dev.of_node;
620	struct sbs_platform_data *pdata = client->dev.platform_data;
621	enum of_gpio_flags gpio_flags;
622	int rc;
623	u32 prop;
624
625	/* verify this driver matches this device */
626	if (!of_node)
627		return NULL;
628
629	/* if platform data is set, honor it */
630	if (pdata)
631		return pdata;
632
633	/* first make sure at least one property is set, otherwise
634	 * it won't change behavior from running without pdata.
635	 */
636	if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) &&
637		!of_get_property(of_node, "sbs,poll-retry-count", NULL) &&
638		!of_get_property(of_node, "sbs,battery-detect-gpios", NULL))
639		goto of_out;
640
641	pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data),
642				GFP_KERNEL);
643	if (!pdata)
644		goto of_out;
645
646	rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop);
647	if (!rc)
648		pdata->i2c_retry_count = prop;
649
650	rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop);
651	if (!rc)
652		pdata->poll_retry_count = prop;
653
654	if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) {
655		pdata->battery_detect = -1;
656		goto of_out;
657	}
658
659	pdata->battery_detect = of_get_named_gpio_flags(of_node,
660			"sbs,battery-detect-gpios", 0, &gpio_flags);
661
662	if (gpio_flags & OF_GPIO_ACTIVE_LOW)
663		pdata->battery_detect_present = 0;
664	else
665		pdata->battery_detect_present = 1;
666
667of_out:
668	return pdata;
669}
670#else
671static struct sbs_platform_data *sbs_of_populate_pdata(
672	struct i2c_client *client)
673{
674	return client->dev.platform_data;
675}
676#endif
677
 
 
 
 
 
 
 
 
678static int sbs_probe(struct i2c_client *client,
679	const struct i2c_device_id *id)
680{
681	struct sbs_info *chip;
 
682	struct sbs_platform_data *pdata = client->dev.platform_data;
 
683	int rc;
684	int irq;
685	char *name;
686
687	name = kasprintf(GFP_KERNEL, "sbs-%s", dev_name(&client->dev));
688	if (!name) {
689		dev_err(&client->dev, "Failed to allocate device name\n");
 
 
 
 
 
690		return -ENOMEM;
691	}
692
693	chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL);
694	if (!chip) {
695		rc = -ENOMEM;
696		goto exit_free_name;
697	}
698
699	chip->client = client;
700	chip->enable_detection = false;
701	chip->gpio_detect = false;
702	chip->power_supply.name = name;
703	chip->power_supply.type = POWER_SUPPLY_TYPE_BATTERY;
704	chip->power_supply.properties = sbs_properties;
705	chip->power_supply.num_properties = ARRAY_SIZE(sbs_properties);
706	chip->power_supply.get_property = sbs_get_property;
707	chip->power_supply.of_node = client->dev.of_node;
708	/* ignore first notification of external change, it is generated
709	 * from the power_supply_register call back
710	 */
711	chip->ignore_changes = 1;
712	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
713	chip->power_supply.external_power_changed = sbs_external_power_changed;
714
715	pdata = sbs_of_populate_pdata(client);
716
717	if (pdata) {
718		chip->gpio_detect = gpio_is_valid(pdata->battery_detect);
719		chip->pdata = pdata;
720	}
721
722	i2c_set_clientdata(client, chip);
723
724	if (!chip->gpio_detect)
725		goto skip_gpio;
726
727	rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
728	if (rc) {
729		dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
730		chip->gpio_detect = false;
731		goto skip_gpio;
732	}
733
734	rc = gpio_direction_input(pdata->battery_detect);
735	if (rc) {
736		dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
737		gpio_free(pdata->battery_detect);
738		chip->gpio_detect = false;
739		goto skip_gpio;
740	}
741
742	irq = gpio_to_irq(pdata->battery_detect);
743	if (irq <= 0) {
744		dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
745		gpio_free(pdata->battery_detect);
746		chip->gpio_detect = false;
747		goto skip_gpio;
748	}
749
750	rc = request_irq(irq, sbs_irq,
751		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
752		dev_name(&client->dev), &chip->power_supply);
753	if (rc) {
754		dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
755		gpio_free(pdata->battery_detect);
756		chip->gpio_detect = false;
757		goto skip_gpio;
758	}
759
760	chip->irq = irq;
761
762skip_gpio:
763	/*
764	 * Before we register, we need to make sure we can actually talk
765	 * to the battery.
766	 */
767	rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
768	if (rc < 0) {
769		dev_err(&client->dev, "%s: Failed to get device status\n",
770			__func__);
771		goto exit_psupply;
 
 
 
772	}
773
774	rc = power_supply_register(&client->dev, &chip->power_supply);
775	if (rc) {
 
776		dev_err(&client->dev,
777			"%s: Failed to register power supply\n", __func__);
 
778		goto exit_psupply;
779	}
780
781	dev_info(&client->dev,
782		"%s: battery gas gauge device registered\n", client->name);
783
784	INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
785
786	chip->enable_detection = true;
787
788	return 0;
789
790exit_psupply:
791	if (chip->irq)
792		free_irq(chip->irq, &chip->power_supply);
793	if (chip->gpio_detect)
794		gpio_free(pdata->battery_detect);
795
796	kfree(chip);
797
798exit_free_name:
799	kfree(name);
800
801	return rc;
802}
803
804static int sbs_remove(struct i2c_client *client)
805{
806	struct sbs_info *chip = i2c_get_clientdata(client);
807
808	if (chip->irq)
809		free_irq(chip->irq, &chip->power_supply);
810	if (chip->gpio_detect)
811		gpio_free(chip->pdata->battery_detect);
812
813	power_supply_unregister(&chip->power_supply);
814
815	cancel_delayed_work_sync(&chip->work);
816
817	kfree(chip->power_supply.name);
818	kfree(chip);
819	chip = NULL;
820
821	return 0;
822}
823
824#if defined CONFIG_PM_SLEEP
825
826static int sbs_suspend(struct device *dev)
827{
828	struct i2c_client *client = to_i2c_client(dev);
829	struct sbs_info *chip = i2c_get_clientdata(client);
830	s32 ret;
831
832	if (chip->poll_time > 0)
833		cancel_delayed_work_sync(&chip->work);
834
835	/* write to manufacturer access with sleep command */
836	ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
837		MANUFACTURER_ACCESS_SLEEP);
838	if (chip->is_present && ret < 0)
839		return ret;
840
841	return 0;
842}
843
844static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
845#define SBS_PM_OPS (&sbs_pm_ops)
846
847#else
848#define SBS_PM_OPS NULL
849#endif
850
851static const struct i2c_device_id sbs_id[] = {
852	{ "bq20z75", 0 },
853	{ "sbs-battery", 1 },
854	{}
855};
856MODULE_DEVICE_TABLE(i2c, sbs_id);
857
858static struct i2c_driver sbs_battery_driver = {
859	.probe		= sbs_probe,
860	.remove		= sbs_remove,
861	.id_table	= sbs_id,
862	.driver = {
863		.name	= "sbs-battery",
864		.of_match_table = of_match_ptr(sbs_dt_ids),
865		.pm	= SBS_PM_OPS,
866	},
867};
868module_i2c_driver(sbs_battery_driver);
869
870MODULE_DESCRIPTION("SBS battery monitor driver");
871MODULE_LICENSE("GPL");
v4.6
   1/*
   2 * Gas Gauge driver for SBS Compliant Batteries
   3 *
   4 * Copyright (c) 2010, NVIDIA Corporation.
   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 as published by
   8 * the Free Software Foundation; either version 2 of the License, or
   9 * (at your option) any later version.
  10 *
  11 * This program is distributed in the hope that it will be useful, but WITHOUT
  12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  14 * more details.
  15 *
  16 * You should have received a copy of the GNU General Public License along
  17 * with this program; if not, write to the Free Software Foundation, Inc.,
  18 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  19 */
  20
  21#include <linux/init.h>
  22#include <linux/module.h>
  23#include <linux/kernel.h>
  24#include <linux/err.h>
  25#include <linux/power_supply.h>
  26#include <linux/i2c.h>
  27#include <linux/slab.h>
  28#include <linux/interrupt.h>
  29#include <linux/gpio.h>
  30#include <linux/of.h>
  31#include <linux/stat.h>
  32
  33#include <linux/power/sbs-battery.h>
  34
  35enum {
  36	REG_MANUFACTURER_DATA,
  37	REG_TEMPERATURE,
  38	REG_VOLTAGE,
  39	REG_CURRENT,
  40	REG_CAPACITY,
  41	REG_TIME_TO_EMPTY,
  42	REG_TIME_TO_FULL,
  43	REG_STATUS,
  44	REG_CYCLE_COUNT,
  45	REG_SERIAL_NUMBER,
  46	REG_REMAINING_CAPACITY,
  47	REG_REMAINING_CAPACITY_CHARGE,
  48	REG_FULL_CHARGE_CAPACITY,
  49	REG_FULL_CHARGE_CAPACITY_CHARGE,
  50	REG_DESIGN_CAPACITY,
  51	REG_DESIGN_CAPACITY_CHARGE,
  52	REG_DESIGN_VOLTAGE_MIN,
  53	REG_DESIGN_VOLTAGE_MAX,
  54	REG_MANUFACTURER,
  55	REG_MODEL_NAME,
  56};
  57
  58/* Battery Mode defines */
  59#define BATTERY_MODE_OFFSET		0x03
  60#define BATTERY_MODE_MASK		0x8000
  61enum sbs_battery_mode {
  62	BATTERY_MODE_AMPS,
  63	BATTERY_MODE_WATTS
  64};
  65
  66/* manufacturer access defines */
  67#define MANUFACTURER_ACCESS_STATUS	0x0006
  68#define MANUFACTURER_ACCESS_SLEEP	0x0011
  69
  70/* battery status value bits */
  71#define BATTERY_DISCHARGING		0x40
  72#define BATTERY_FULL_CHARGED		0x20
  73#define BATTERY_FULL_DISCHARGED		0x10
  74
  75/* min_value and max_value are only valid for numerical data */
  76#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
  77	.psp = _psp, \
  78	.addr = _addr, \
  79	.min_value = _min_value, \
  80	.max_value = _max_value, \
  81}
  82
  83static const struct chip_data {
  84	enum power_supply_property psp;
  85	u8 addr;
  86	int min_value;
  87	int max_value;
  88} sbs_data[] = {
  89	[REG_MANUFACTURER_DATA] =
  90		SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
  91	[REG_TEMPERATURE] =
  92		SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
  93	[REG_VOLTAGE] =
  94		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
  95	[REG_CURRENT] =
  96		SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
  97	[REG_CAPACITY] =
  98		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
  99	[REG_REMAINING_CAPACITY] =
 100		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
 101	[REG_REMAINING_CAPACITY_CHARGE] =
 102		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
 103	[REG_FULL_CHARGE_CAPACITY] =
 104		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
 105	[REG_FULL_CHARGE_CAPACITY_CHARGE] =
 106		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
 107	[REG_TIME_TO_EMPTY] =
 108		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
 109	[REG_TIME_TO_FULL] =
 110		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
 111	[REG_STATUS] =
 112		SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
 113	[REG_CYCLE_COUNT] =
 114		SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
 115	[REG_DESIGN_CAPACITY] =
 116		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
 117	[REG_DESIGN_CAPACITY_CHARGE] =
 118		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
 119	[REG_DESIGN_VOLTAGE_MIN] =
 120		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
 121	[REG_DESIGN_VOLTAGE_MAX] =
 122		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
 123	[REG_SERIAL_NUMBER] =
 124		SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
 125	/* Properties of type `const char *' */
 126	[REG_MANUFACTURER] =
 127		SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
 128	[REG_MODEL_NAME] =
 129		SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
 130};
 131
 132static enum power_supply_property sbs_properties[] = {
 133	POWER_SUPPLY_PROP_STATUS,
 134	POWER_SUPPLY_PROP_HEALTH,
 135	POWER_SUPPLY_PROP_PRESENT,
 136	POWER_SUPPLY_PROP_TECHNOLOGY,
 137	POWER_SUPPLY_PROP_CYCLE_COUNT,
 138	POWER_SUPPLY_PROP_VOLTAGE_NOW,
 139	POWER_SUPPLY_PROP_CURRENT_NOW,
 140	POWER_SUPPLY_PROP_CAPACITY,
 141	POWER_SUPPLY_PROP_TEMP,
 142	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
 143	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
 144	POWER_SUPPLY_PROP_SERIAL_NUMBER,
 145	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
 146	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
 147	POWER_SUPPLY_PROP_ENERGY_NOW,
 148	POWER_SUPPLY_PROP_ENERGY_FULL,
 149	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
 150	POWER_SUPPLY_PROP_CHARGE_NOW,
 151	POWER_SUPPLY_PROP_CHARGE_FULL,
 152	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 153	/* Properties of type `const char *' */
 154	POWER_SUPPLY_PROP_MANUFACTURER,
 155	POWER_SUPPLY_PROP_MODEL_NAME
 156};
 157
 158struct sbs_info {
 159	struct i2c_client		*client;
 160	struct power_supply		*power_supply;
 161	struct sbs_platform_data	*pdata;
 162	bool				is_present;
 163	bool				gpio_detect;
 164	bool				enable_detection;
 165	int				irq;
 166	int				last_state;
 167	int				poll_time;
 168	struct delayed_work		work;
 169	int				ignore_changes;
 170};
 171
 172static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
 173static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
 174static bool force_load;
 175
 176static int sbs_read_word_data(struct i2c_client *client, u8 address)
 177{
 178	struct sbs_info *chip = i2c_get_clientdata(client);
 179	s32 ret = 0;
 180	int retries = 1;
 181
 182	if (chip->pdata)
 183		retries = max(chip->pdata->i2c_retry_count + 1, 1);
 184
 185	while (retries > 0) {
 186		ret = i2c_smbus_read_word_data(client, address);
 187		if (ret >= 0)
 188			break;
 189		retries--;
 190	}
 191
 192	if (ret < 0) {
 193		dev_dbg(&client->dev,
 194			"%s: i2c read at address 0x%x failed\n",
 195			__func__, address);
 196		return ret;
 197	}
 198
 199	return le16_to_cpu(ret);
 200}
 201
 202static int sbs_read_string_data(struct i2c_client *client, u8 address,
 203				char *values)
 204{
 205	struct sbs_info *chip = i2c_get_clientdata(client);
 206	s32 ret = 0, block_length = 0;
 207	int retries_length = 1, retries_block = 1;
 208	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
 209
 210	if (chip->pdata) {
 211		retries_length = max(chip->pdata->i2c_retry_count + 1, 1);
 212		retries_block = max(chip->pdata->i2c_retry_count + 1, 1);
 213	}
 214
 215	/* Adapter needs to support these two functions */
 216	if (!i2c_check_functionality(client->adapter,
 217				     I2C_FUNC_SMBUS_BYTE_DATA |
 218				     I2C_FUNC_SMBUS_I2C_BLOCK)){
 219		return -ENODEV;
 220	}
 221
 222	/* Get the length of block data */
 223	while (retries_length > 0) {
 224		ret = i2c_smbus_read_byte_data(client, address);
 225		if (ret >= 0)
 226			break;
 227		retries_length--;
 228	}
 229
 230	if (ret < 0) {
 231		dev_dbg(&client->dev,
 232			"%s: i2c read at address 0x%x failed\n",
 233			__func__, address);
 234		return ret;
 235	}
 236
 237	/* block_length does not include NULL terminator */
 238	block_length = ret;
 239	if (block_length > I2C_SMBUS_BLOCK_MAX) {
 240		dev_err(&client->dev,
 241			"%s: Returned block_length is longer than 0x%x\n",
 242			__func__, I2C_SMBUS_BLOCK_MAX);
 243		return -EINVAL;
 244	}
 245
 246	/* Get the block data */
 247	while (retries_block > 0) {
 248		ret = i2c_smbus_read_i2c_block_data(
 249				client, address,
 250				block_length + 1, block_buffer);
 251		if (ret >= 0)
 252			break;
 253		retries_block--;
 254	}
 255
 256	if (ret < 0) {
 257		dev_dbg(&client->dev,
 258			"%s: i2c read at address 0x%x failed\n",
 259			__func__, address);
 260		return ret;
 261	}
 262
 263	/* block_buffer[0] == block_length */
 264	memcpy(values, block_buffer + 1, block_length);
 265	values[block_length] = '\0';
 266
 267	return le16_to_cpu(ret);
 268}
 269
 270static int sbs_write_word_data(struct i2c_client *client, u8 address,
 271	u16 value)
 272{
 273	struct sbs_info *chip = i2c_get_clientdata(client);
 274	s32 ret = 0;
 275	int retries = 1;
 276
 277	if (chip->pdata)
 278		retries = max(chip->pdata->i2c_retry_count + 1, 1);
 279
 280	while (retries > 0) {
 281		ret = i2c_smbus_write_word_data(client, address,
 282			le16_to_cpu(value));
 283		if (ret >= 0)
 284			break;
 285		retries--;
 286	}
 287
 288	if (ret < 0) {
 289		dev_dbg(&client->dev,
 290			"%s: i2c write to address 0x%x failed\n",
 291			__func__, address);
 292		return ret;
 293	}
 294
 295	return 0;
 296}
 297
 298static int sbs_get_battery_presence_and_health(
 299	struct i2c_client *client, enum power_supply_property psp,
 300	union power_supply_propval *val)
 301{
 302	s32 ret;
 303	struct sbs_info *chip = i2c_get_clientdata(client);
 304
 305	if (psp == POWER_SUPPLY_PROP_PRESENT &&
 306		chip->gpio_detect) {
 307		ret = gpio_get_value(chip->pdata->battery_detect);
 308		if (ret == chip->pdata->battery_detect_present)
 309			val->intval = 1;
 310		else
 311			val->intval = 0;
 312		chip->is_present = val->intval;
 313		return ret;
 314	}
 315
 316	/* Write to ManufacturerAccess with
 317	 * ManufacturerAccess command and then
 318	 * read the status */
 319	ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
 320					MANUFACTURER_ACCESS_STATUS);
 321	if (ret < 0) {
 322		if (psp == POWER_SUPPLY_PROP_PRESENT)
 323			val->intval = 0; /* battery removed */
 324		return ret;
 325	}
 326
 327	ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
 328	if (ret < 0)
 329		return ret;
 330
 331	if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
 332	    ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
 333		val->intval = 0;
 334		return 0;
 335	}
 336
 337	/* Mask the upper nibble of 2nd byte and
 338	 * lower byte of response then
 339	 * shift the result by 8 to get status*/
 340	ret &= 0x0F00;
 341	ret >>= 8;
 342	if (psp == POWER_SUPPLY_PROP_PRESENT) {
 343		if (ret == 0x0F)
 344			/* battery removed */
 345			val->intval = 0;
 346		else
 347			val->intval = 1;
 348	} else if (psp == POWER_SUPPLY_PROP_HEALTH) {
 349		if (ret == 0x09)
 350			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 351		else if (ret == 0x0B)
 352			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
 353		else if (ret == 0x0C)
 354			val->intval = POWER_SUPPLY_HEALTH_DEAD;
 355		else
 356			val->intval = POWER_SUPPLY_HEALTH_GOOD;
 357	}
 358
 359	return 0;
 360}
 361
 362static int sbs_get_battery_property(struct i2c_client *client,
 363	int reg_offset, enum power_supply_property psp,
 364	union power_supply_propval *val)
 365{
 366	struct sbs_info *chip = i2c_get_clientdata(client);
 367	s32 ret;
 368
 369	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
 370	if (ret < 0)
 371		return ret;
 372
 373	/* returned values are 16 bit */
 374	if (sbs_data[reg_offset].min_value < 0)
 375		ret = (s16)ret;
 376
 377	if (ret >= sbs_data[reg_offset].min_value &&
 378	    ret <= sbs_data[reg_offset].max_value) {
 379		val->intval = ret;
 380		if (psp != POWER_SUPPLY_PROP_STATUS)
 381			return 0;
 382
 383		if (ret & BATTERY_FULL_CHARGED)
 384			val->intval = POWER_SUPPLY_STATUS_FULL;
 385		else if (ret & BATTERY_FULL_DISCHARGED)
 386			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
 387		else if (ret & BATTERY_DISCHARGING)
 388			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
 389		else
 390			val->intval = POWER_SUPPLY_STATUS_CHARGING;
 391
 392		if (chip->poll_time == 0)
 393			chip->last_state = val->intval;
 394		else if (chip->last_state != val->intval) {
 395			cancel_delayed_work_sync(&chip->work);
 396			power_supply_changed(chip->power_supply);
 397			chip->poll_time = 0;
 398		}
 399	} else {
 400		if (psp == POWER_SUPPLY_PROP_STATUS)
 401			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
 402		else
 403			val->intval = 0;
 404	}
 405
 406	return 0;
 407}
 408
 409static int sbs_get_battery_string_property(struct i2c_client *client,
 410	int reg_offset, enum power_supply_property psp, char *val)
 411{
 412	s32 ret;
 413
 414	ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
 415
 416	if (ret < 0)
 417		return ret;
 418
 419	return 0;
 420}
 421
 422static void  sbs_unit_adjustment(struct i2c_client *client,
 423	enum power_supply_property psp, union power_supply_propval *val)
 424{
 425#define BASE_UNIT_CONVERSION		1000
 426#define BATTERY_MODE_CAP_MULT_WATT	(10 * BASE_UNIT_CONVERSION)
 427#define TIME_UNIT_CONVERSION		60
 428#define TEMP_KELVIN_TO_CELSIUS		2731
 429	switch (psp) {
 430	case POWER_SUPPLY_PROP_ENERGY_NOW:
 431	case POWER_SUPPLY_PROP_ENERGY_FULL:
 432	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
 433		/* sbs provides energy in units of 10mWh.
 434		 * Convert to µWh
 435		 */
 436		val->intval *= BATTERY_MODE_CAP_MULT_WATT;
 437		break;
 438
 439	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 440	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 441	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
 442	case POWER_SUPPLY_PROP_CURRENT_NOW:
 443	case POWER_SUPPLY_PROP_CHARGE_NOW:
 444	case POWER_SUPPLY_PROP_CHARGE_FULL:
 445	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
 446		val->intval *= BASE_UNIT_CONVERSION;
 447		break;
 448
 449	case POWER_SUPPLY_PROP_TEMP:
 450		/* sbs provides battery temperature in 0.1K
 451		 * so convert it to 0.1°C
 452		 */
 453		val->intval -= TEMP_KELVIN_TO_CELSIUS;
 454		break;
 455
 456	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
 457	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
 458		/* sbs provides time to empty and time to full in minutes.
 459		 * Convert to seconds
 460		 */
 461		val->intval *= TIME_UNIT_CONVERSION;
 462		break;
 463
 464	default:
 465		dev_dbg(&client->dev,
 466			"%s: no need for unit conversion %d\n", __func__, psp);
 467	}
 468}
 469
 470static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
 471	enum sbs_battery_mode mode)
 472{
 473	int ret, original_val;
 474
 475	original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
 476	if (original_val < 0)
 477		return original_val;
 478
 479	if ((original_val & BATTERY_MODE_MASK) == mode)
 480		return mode;
 481
 482	if (mode == BATTERY_MODE_AMPS)
 483		ret = original_val & ~BATTERY_MODE_MASK;
 484	else
 485		ret = original_val | BATTERY_MODE_MASK;
 486
 487	ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
 488	if (ret < 0)
 489		return ret;
 490
 491	return original_val & BATTERY_MODE_MASK;
 492}
 493
 494static int sbs_get_battery_capacity(struct i2c_client *client,
 495	int reg_offset, enum power_supply_property psp,
 496	union power_supply_propval *val)
 497{
 498	s32 ret;
 499	enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
 500
 501	if (power_supply_is_amp_property(psp))
 502		mode = BATTERY_MODE_AMPS;
 503
 504	mode = sbs_set_battery_mode(client, mode);
 505	if (mode < 0)
 506		return mode;
 507
 508	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
 509	if (ret < 0)
 510		return ret;
 511
 512	if (psp == POWER_SUPPLY_PROP_CAPACITY) {
 513		/* sbs spec says that this can be >100 %
 514		* even if max value is 100 % */
 515		val->intval = min(ret, 100);
 516	} else
 517		val->intval = ret;
 518
 519	ret = sbs_set_battery_mode(client, mode);
 520	if (ret < 0)
 521		return ret;
 522
 523	return 0;
 524}
 525
 526static char sbs_serial[5];
 527static int sbs_get_battery_serial_number(struct i2c_client *client,
 528	union power_supply_propval *val)
 529{
 530	int ret;
 531
 532	ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
 533	if (ret < 0)
 534		return ret;
 535
 536	ret = sprintf(sbs_serial, "%04x", ret);
 537	val->strval = sbs_serial;
 538
 539	return 0;
 540}
 541
 542static int sbs_get_property_index(struct i2c_client *client,
 543	enum power_supply_property psp)
 544{
 545	int count;
 546	for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
 547		if (psp == sbs_data[count].psp)
 548			return count;
 549
 550	dev_warn(&client->dev,
 551		"%s: Invalid Property - %d\n", __func__, psp);
 552
 553	return -EINVAL;
 554}
 555
 556static int sbs_get_property(struct power_supply *psy,
 557	enum power_supply_property psp,
 558	union power_supply_propval *val)
 559{
 560	int ret = 0;
 561	struct sbs_info *chip = power_supply_get_drvdata(psy);
 
 562	struct i2c_client *client = chip->client;
 563
 564	switch (psp) {
 565	case POWER_SUPPLY_PROP_PRESENT:
 566	case POWER_SUPPLY_PROP_HEALTH:
 567		ret = sbs_get_battery_presence_and_health(client, psp, val);
 568		if (psp == POWER_SUPPLY_PROP_PRESENT)
 569			return 0;
 570		break;
 571
 572	case POWER_SUPPLY_PROP_TECHNOLOGY:
 573		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
 574		goto done; /* don't trigger power_supply_changed()! */
 575
 576	case POWER_SUPPLY_PROP_ENERGY_NOW:
 577	case POWER_SUPPLY_PROP_ENERGY_FULL:
 578	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
 579	case POWER_SUPPLY_PROP_CHARGE_NOW:
 580	case POWER_SUPPLY_PROP_CHARGE_FULL:
 581	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
 582	case POWER_SUPPLY_PROP_CAPACITY:
 583		ret = sbs_get_property_index(client, psp);
 584		if (ret < 0)
 585			break;
 586
 587		ret = sbs_get_battery_capacity(client, ret, psp, val);
 588		break;
 589
 590	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
 591		ret = sbs_get_battery_serial_number(client, val);
 592		break;
 593
 594	case POWER_SUPPLY_PROP_STATUS:
 595	case POWER_SUPPLY_PROP_CYCLE_COUNT:
 596	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 597	case POWER_SUPPLY_PROP_CURRENT_NOW:
 598	case POWER_SUPPLY_PROP_TEMP:
 599	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
 600	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
 601	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 602	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
 603		ret = sbs_get_property_index(client, psp);
 604		if (ret < 0)
 605			break;
 606
 607		ret = sbs_get_battery_property(client, ret, psp, val);
 608		break;
 609
 610	case POWER_SUPPLY_PROP_MODEL_NAME:
 611		ret = sbs_get_property_index(client, psp);
 612		if (ret < 0)
 613			break;
 614
 615		ret = sbs_get_battery_string_property(client, ret, psp,
 616						      model_name);
 617		val->strval = model_name;
 618		break;
 619
 620	case POWER_SUPPLY_PROP_MANUFACTURER:
 621		ret = sbs_get_property_index(client, psp);
 622		if (ret < 0)
 623			break;
 624
 625		ret = sbs_get_battery_string_property(client, ret, psp,
 626						      manufacturer);
 627		val->strval = manufacturer;
 628		break;
 629
 630	default:
 631		dev_err(&client->dev,
 632			"%s: INVALID property\n", __func__);
 633		return -EINVAL;
 634	}
 635
 636	if (!chip->enable_detection)
 637		goto done;
 638
 639	if (!chip->gpio_detect &&
 640		chip->is_present != (ret >= 0)) {
 641		chip->is_present = (ret >= 0);
 642		power_supply_changed(chip->power_supply);
 643	}
 644
 645done:
 646	if (!ret) {
 647		/* Convert units to match requirements for power supply class */
 648		sbs_unit_adjustment(client, psp, val);
 649	}
 650
 651	dev_dbg(&client->dev,
 652		"%s: property = %d, value = %x\n", __func__, psp, val->intval);
 653
 654	if (ret && chip->is_present)
 655		return ret;
 656
 657	/* battery not present, so return NODATA for properties */
 658	if (ret)
 659		return -ENODATA;
 660
 661	return 0;
 662}
 663
 664static irqreturn_t sbs_irq(int irq, void *devid)
 665{
 666	struct power_supply *battery = devid;
 667
 668	power_supply_changed(battery);
 669
 670	return IRQ_HANDLED;
 671}
 672
 673static void sbs_external_power_changed(struct power_supply *psy)
 674{
 675	struct sbs_info *chip = power_supply_get_drvdata(psy);
 
 
 676
 677	if (chip->ignore_changes > 0) {
 678		chip->ignore_changes--;
 679		return;
 680	}
 681
 682	/* cancel outstanding work */
 683	cancel_delayed_work_sync(&chip->work);
 684
 685	schedule_delayed_work(&chip->work, HZ);
 686	chip->poll_time = chip->pdata->poll_retry_count;
 687}
 688
 689static void sbs_delayed_work(struct work_struct *work)
 690{
 691	struct sbs_info *chip;
 692	s32 ret;
 693
 694	chip = container_of(work, struct sbs_info, work.work);
 695
 696	ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
 697	/* if the read failed, give up on this work */
 698	if (ret < 0) {
 699		chip->poll_time = 0;
 700		return;
 701	}
 702
 703	if (ret & BATTERY_FULL_CHARGED)
 704		ret = POWER_SUPPLY_STATUS_FULL;
 705	else if (ret & BATTERY_FULL_DISCHARGED)
 706		ret = POWER_SUPPLY_STATUS_NOT_CHARGING;
 707	else if (ret & BATTERY_DISCHARGING)
 708		ret = POWER_SUPPLY_STATUS_DISCHARGING;
 709	else
 710		ret = POWER_SUPPLY_STATUS_CHARGING;
 711
 712	if (chip->last_state != ret) {
 713		chip->poll_time = 0;
 714		power_supply_changed(chip->power_supply);
 715		return;
 716	}
 717	if (chip->poll_time > 0) {
 718		schedule_delayed_work(&chip->work, HZ);
 719		chip->poll_time--;
 720		return;
 721	}
 722}
 723
 724#if defined(CONFIG_OF)
 725
 726#include <linux/of_device.h>
 727#include <linux/of_gpio.h>
 728
 729static const struct of_device_id sbs_dt_ids[] = {
 730	{ .compatible = "sbs,sbs-battery" },
 731	{ .compatible = "ti,bq20z75" },
 732	{ }
 733};
 734MODULE_DEVICE_TABLE(of, sbs_dt_ids);
 735
 736static struct sbs_platform_data *sbs_of_populate_pdata(
 737		struct i2c_client *client)
 738{
 739	struct device_node *of_node = client->dev.of_node;
 740	struct sbs_platform_data *pdata = client->dev.platform_data;
 741	enum of_gpio_flags gpio_flags;
 742	int rc;
 743	u32 prop;
 744
 745	/* verify this driver matches this device */
 746	if (!of_node)
 747		return NULL;
 748
 749	/* if platform data is set, honor it */
 750	if (pdata)
 751		return pdata;
 752
 753	/* first make sure at least one property is set, otherwise
 754	 * it won't change behavior from running without pdata.
 755	 */
 756	if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) &&
 757		!of_get_property(of_node, "sbs,poll-retry-count", NULL) &&
 758		!of_get_property(of_node, "sbs,battery-detect-gpios", NULL))
 759		goto of_out;
 760
 761	pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data),
 762				GFP_KERNEL);
 763	if (!pdata)
 764		goto of_out;
 765
 766	rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop);
 767	if (!rc)
 768		pdata->i2c_retry_count = prop;
 769
 770	rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop);
 771	if (!rc)
 772		pdata->poll_retry_count = prop;
 773
 774	if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) {
 775		pdata->battery_detect = -1;
 776		goto of_out;
 777	}
 778
 779	pdata->battery_detect = of_get_named_gpio_flags(of_node,
 780			"sbs,battery-detect-gpios", 0, &gpio_flags);
 781
 782	if (gpio_flags & OF_GPIO_ACTIVE_LOW)
 783		pdata->battery_detect_present = 0;
 784	else
 785		pdata->battery_detect_present = 1;
 786
 787of_out:
 788	return pdata;
 789}
 790#else
 791static struct sbs_platform_data *sbs_of_populate_pdata(
 792	struct i2c_client *client)
 793{
 794	return client->dev.platform_data;
 795}
 796#endif
 797
 798static const struct power_supply_desc sbs_default_desc = {
 799	.type = POWER_SUPPLY_TYPE_BATTERY,
 800	.properties = sbs_properties,
 801	.num_properties = ARRAY_SIZE(sbs_properties),
 802	.get_property = sbs_get_property,
 803	.external_power_changed = sbs_external_power_changed,
 804};
 805
 806static int sbs_probe(struct i2c_client *client,
 807	const struct i2c_device_id *id)
 808{
 809	struct sbs_info *chip;
 810	struct power_supply_desc *sbs_desc;
 811	struct sbs_platform_data *pdata = client->dev.platform_data;
 812	struct power_supply_config psy_cfg = {};
 813	int rc;
 814	int irq;
 
 815
 816	sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
 817			sizeof(*sbs_desc), GFP_KERNEL);
 818	if (!sbs_desc)
 819		return -ENOMEM;
 820
 821	sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
 822			dev_name(&client->dev));
 823	if (!sbs_desc->name)
 824		return -ENOMEM;
 
 825
 826	chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL);
 827	if (!chip)
 828		return -ENOMEM;
 
 
 829
 830	chip->client = client;
 831	chip->enable_detection = false;
 832	chip->gpio_detect = false;
 833	psy_cfg.of_node = client->dev.of_node;
 834	psy_cfg.drv_data = chip;
 
 
 
 
 835	/* ignore first notification of external change, it is generated
 836	 * from the power_supply_register call back
 837	 */
 838	chip->ignore_changes = 1;
 839	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
 
 840
 841	pdata = sbs_of_populate_pdata(client);
 842
 843	if (pdata) {
 844		chip->gpio_detect = gpio_is_valid(pdata->battery_detect);
 845		chip->pdata = pdata;
 846	}
 847
 848	i2c_set_clientdata(client, chip);
 849
 850	if (!chip->gpio_detect)
 851		goto skip_gpio;
 852
 853	rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
 854	if (rc) {
 855		dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
 856		chip->gpio_detect = false;
 857		goto skip_gpio;
 858	}
 859
 860	rc = gpio_direction_input(pdata->battery_detect);
 861	if (rc) {
 862		dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
 863		gpio_free(pdata->battery_detect);
 864		chip->gpio_detect = false;
 865		goto skip_gpio;
 866	}
 867
 868	irq = gpio_to_irq(pdata->battery_detect);
 869	if (irq <= 0) {
 870		dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
 871		gpio_free(pdata->battery_detect);
 872		chip->gpio_detect = false;
 873		goto skip_gpio;
 874	}
 875
 876	rc = request_irq(irq, sbs_irq,
 877		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 878		dev_name(&client->dev), chip->power_supply);
 879	if (rc) {
 880		dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
 881		gpio_free(pdata->battery_detect);
 882		chip->gpio_detect = false;
 883		goto skip_gpio;
 884	}
 885
 886	chip->irq = irq;
 887
 888skip_gpio:
 889	/*
 890	 * Before we register, we might need to make sure we can actually talk
 891	 * to the battery.
 892	 */
 893	if (!force_load) {
 894		rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
 895
 896		if (rc < 0) {
 897			dev_err(&client->dev, "%s: Failed to get device status\n",
 898				__func__);
 899			goto exit_psupply;
 900		}
 901	}
 902
 903	chip->power_supply = power_supply_register(&client->dev, sbs_desc,
 904						   &psy_cfg);
 905	if (IS_ERR(chip->power_supply)) {
 906		dev_err(&client->dev,
 907			"%s: Failed to register power supply\n", __func__);
 908		rc = PTR_ERR(chip->power_supply);
 909		goto exit_psupply;
 910	}
 911
 912	dev_info(&client->dev,
 913		"%s: battery gas gauge device registered\n", client->name);
 914
 915	INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
 916
 917	chip->enable_detection = true;
 918
 919	return 0;
 920
 921exit_psupply:
 922	if (chip->irq)
 923		free_irq(chip->irq, chip->power_supply);
 924	if (chip->gpio_detect)
 925		gpio_free(pdata->battery_detect);
 926
 927	kfree(chip);
 928
 
 
 
 929	return rc;
 930}
 931
 932static int sbs_remove(struct i2c_client *client)
 933{
 934	struct sbs_info *chip = i2c_get_clientdata(client);
 935
 936	if (chip->irq)
 937		free_irq(chip->irq, chip->power_supply);
 938	if (chip->gpio_detect)
 939		gpio_free(chip->pdata->battery_detect);
 940
 941	power_supply_unregister(chip->power_supply);
 942
 943	cancel_delayed_work_sync(&chip->work);
 944
 
 945	kfree(chip);
 946	chip = NULL;
 947
 948	return 0;
 949}
 950
 951#if defined CONFIG_PM_SLEEP
 952
 953static int sbs_suspend(struct device *dev)
 954{
 955	struct i2c_client *client = to_i2c_client(dev);
 956	struct sbs_info *chip = i2c_get_clientdata(client);
 957	s32 ret;
 958
 959	if (chip->poll_time > 0)
 960		cancel_delayed_work_sync(&chip->work);
 961
 962	/* write to manufacturer access with sleep command */
 963	ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
 964		MANUFACTURER_ACCESS_SLEEP);
 965	if (chip->is_present && ret < 0)
 966		return ret;
 967
 968	return 0;
 969}
 970
 971static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
 972#define SBS_PM_OPS (&sbs_pm_ops)
 973
 974#else
 975#define SBS_PM_OPS NULL
 976#endif
 977
 978static const struct i2c_device_id sbs_id[] = {
 979	{ "bq20z75", 0 },
 980	{ "sbs-battery", 1 },
 981	{}
 982};
 983MODULE_DEVICE_TABLE(i2c, sbs_id);
 984
 985static struct i2c_driver sbs_battery_driver = {
 986	.probe		= sbs_probe,
 987	.remove		= sbs_remove,
 988	.id_table	= sbs_id,
 989	.driver = {
 990		.name	= "sbs-battery",
 991		.of_match_table = of_match_ptr(sbs_dt_ids),
 992		.pm	= SBS_PM_OPS,
 993	},
 994};
 995module_i2c_driver(sbs_battery_driver);
 996
 997MODULE_DESCRIPTION("SBS battery monitor driver");
 998MODULE_LICENSE("GPL");
 999
1000module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
1001MODULE_PARM_DESC(force_load,
1002		 "Attempt to load the driver even if no battery is connected");