| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* BQ257XX Core Driver |
| 4 |
* Copyright (C) 2025 Chris Morgan <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/device.h> |
| 8 |
#include <linux/i2c.h> |
| 9 |
#include <linux/mfd/bq257xx.h> |
| 10 |
#include <linux/mfd/core.h> |
| 11 |
#include <linux/regmap.h> |
| 12 |
|
| 13 |
static const struct regmap_range bq25703_readonly_reg_ranges[] = { |
| 14 |
regmap_reg_range(BQ25703_CHARGER_STATUS, BQ25703_MANUFACT_DEV_ID), |
| 15 |
}; |
| 16 |
|
| 17 |
static const struct regmap_access_table bq25703_writeable_regs = { |
| 18 |
.no_ranges = bq25703_readonly_reg_ranges, |
| 19 |
.n_no_ranges = ARRAY_SIZE(bq25703_readonly_reg_ranges), |
| 20 |
}; |
| 21 |
|
| 22 |
static const struct regmap_range bq25703_volatile_reg_ranges[] = { |
| 23 |
regmap_reg_range(BQ25703_CHARGE_OPTION_0, BQ25703_IIN_HOST), |
| 24 |
regmap_reg_range(BQ25703_CHARGER_STATUS, BQ25703_ADC_OPTION), |
| 25 |
}; |
| 26 |
|
| 27 |
static const struct regmap_access_table bq25703_volatile_regs = { |
| 28 |
.yes_ranges = bq25703_volatile_reg_ranges, |
| 29 |
.n_yes_ranges = ARRAY_SIZE(bq25703_volatile_reg_ranges), |
| 30 |
}; |
| 31 |
|
| 32 |
static const struct regmap_config bq25703_regmap_config = { |
| 33 |
.reg_bits = 8, |
| 34 |
.val_bits = 16, |
| 35 |
.max_register = BQ25703_ADC_OPTION, |
| 36 |
.cache_type = REGCACHE_MAPLE, |
| 37 |
.wr_table = &bq25703_writeable_regs, |
| 38 |
.volatile_table = &bq25703_volatile_regs, |
| 39 |
.val_format_endian = REGMAP_ENDIAN_LITTLE, |
| 40 |
}; |
| 41 |
|
| 42 |
static const struct mfd_cell cells[] = { |
| 43 |
MFD_CELL_NAME("bq257xx-regulator"), |
| 44 |
MFD_CELL_NAME("bq257xx-charger"), |
| 45 |
}; |
| 46 |
|
| 47 |
static int bq257xx_probe(struct i2c_client *client) |
| 48 |
{ |
| 49 |
struct bq257xx_device *ddata; |
| 50 |
int ret; |
| 51 |
|
| 52 |
ddata = devm_kzalloc(&client->dev, sizeof(*ddata), GFP_KERNEL); |
| 53 |
if (!ddata) |
| 54 |
return -ENOMEM; |
| 55 |
|
| 56 |
ddata->client = client; |
| 57 |
|
| 58 |
ddata->regmap = devm_regmap_init_i2c(client, &bq25703_regmap_config); |
| 59 |
if (IS_ERR(ddata->regmap)) { |
| 60 |
return dev_err_probe(&client->dev, PTR_ERR(ddata->regmap), |
| 61 |
"Failed to allocate register map\n"); |
| 62 |
} |
| 63 |
|
| 64 |
i2c_set_clientdata(client, ddata); |
| 65 |
|
| 66 |
ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_AUTO, |
| 67 |
cells, ARRAY_SIZE(cells), NULL, 0, NULL); |
| 68 |
if (ret) |
| 69 |
return dev_err_probe(&client->dev, ret, |
| 70 |
"Failed to register child devices\n"); |
| 71 |
|
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
static const struct i2c_device_id bq257xx_i2c_ids[] = { |
| 76 |
{ "bq25703a" }, |
| 77 |
{} |
| 78 |
}; |
| 79 |
MODULE_DEVICE_TABLE(i2c, bq257xx_i2c_ids); |
| 80 |
|
| 81 |
static const struct of_device_id bq257xx_of_match[] = { |
| 82 |
{ .compatible = "ti,bq25703a" }, |
| 83 |
{} |
| 84 |
}; |
| 85 |
MODULE_DEVICE_TABLE(of, bq257xx_of_match); |
| 86 |
|
| 87 |
static struct i2c_driver bq257xx_driver = { |
| 88 |
.driver = { |
| 89 |
.name = "bq257xx", |
| 90 |
.of_match_table = bq257xx_of_match, |
| 91 |
}, |
| 92 |
.probe = bq257xx_probe, |
| 93 |
.id_table = bq257xx_i2c_ids, |
| 94 |
}; |
| 95 |
module_i2c_driver(bq257xx_driver); |
| 96 |
|
| 97 |
MODULE_DESCRIPTION("bq257xx buck/boost/charger driver"); |
| 98 |
MODULE_AUTHOR("Chris Morgan <[email protected]>"); |
| 99 |
MODULE_LICENSE("GPL"); |