| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* I2C access driver for the following TI PMICs: |
| 4 |
* - LP8764 |
| 5 |
* - TPS65224 |
| 6 |
* - TPS652G1 |
| 7 |
* - TPS6593 |
| 8 |
* - TPS6594 |
| 9 |
* |
| 10 |
* Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/ |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <linux/crc8.h> |
| 14 |
#include <linux/i2c.h> |
| 15 |
#include <linux/module.h> |
| 16 |
#include <linux/mod_devicetable.h> |
| 17 |
#include <linux/of_device.h> |
| 18 |
#include <linux/regmap.h> |
| 19 |
|
| 20 |
#include <linux/mfd/tps6594.h> |
| 21 |
|
| 22 |
static bool enable_crc; |
| 23 |
module_param(enable_crc, bool, 0444); |
| 24 |
MODULE_PARM_DESC(enable_crc, "Enable CRC feature for I2C interface"); |
| 25 |
|
| 26 |
DECLARE_CRC8_TABLE(tps6594_i2c_crc_table); |
| 27 |
|
| 28 |
static int tps6594_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
| 29 |
{ |
| 30 |
int ret = i2c_transfer(adap, msgs, num); |
| 31 |
|
| 32 |
if (ret == num) |
| 33 |
return 0; |
| 34 |
else if (ret < 0) |
| 35 |
return ret; |
| 36 |
else |
| 37 |
return -EIO; |
| 38 |
} |
| 39 |
|
| 40 |
static int tps6594_i2c_reg_read_with_crc(struct i2c_client *client, u8 page, u8 reg, u8 *val) |
| 41 |
{ |
| 42 |
struct i2c_msg msgs[2]; |
| 43 |
u8 buf_rx[] = { 0, 0 }; |
| 44 |
/* I2C address = I2C base address + Page index */ |
| 45 |
const u8 addr = client->addr + page; |
| 46 |
/* |
| 47 |
* CRC is calculated from every bit included in the protocol |
| 48 |
* except the ACK bits from the target. Byte stream is: |
| 49 |
* - B0: (I2C_addr_7bits << 1) | WR_bit, with WR_bit = 0 |
| 50 |
* - B1: reg |
| 51 |
* - B2: (I2C_addr_7bits << 1) | RD_bit, with RD_bit = 1 |
| 52 |
* - B3: val |
| 53 |
* - B4: CRC from B0-B1-B2-B3 |
| 54 |
*/ |
| 55 |
u8 crc_data[] = { addr << 1, reg, addr << 1 | 1, 0 }; |
| 56 |
int ret; |
| 57 |
|
| 58 |
/* Write register */ |
| 59 |
msgs[0].addr = addr; |
| 60 |
msgs[0].flags = 0; |
| 61 |
msgs[0].len = 1; |
| 62 |
msgs[0].buf = ® |
| 63 |
|
| 64 |
/* Read data and CRC */ |
| 65 |
msgs[1].addr = msgs[0].addr; |
| 66 |
msgs[1].flags = I2C_M_RD; |
| 67 |
msgs[1].len = 2; |
| 68 |
msgs[1].buf = buf_rx; |
| 69 |
|
| 70 |
ret = tps6594_i2c_transfer(client->adapter, msgs, 2); |
| 71 |
if (ret < 0) |
| 72 |
return ret; |
| 73 |
|
| 74 |
crc_data[sizeof(crc_data) - 1] = *val = buf_rx[0]; |
| 75 |
if (buf_rx[1] != crc8(tps6594_i2c_crc_table, crc_data, sizeof(crc_data), CRC8_INIT_VALUE)) |
| 76 |
return -EIO; |
| 77 |
|
| 78 |
return ret; |
| 79 |
} |
| 80 |
|
| 81 |
static int tps6594_i2c_reg_write_with_crc(struct i2c_client *client, u8 page, u8 reg, u8 val) |
| 82 |
{ |
| 83 |
struct i2c_msg msg; |
| 84 |
u8 buf[] = { reg, val, 0 }; |
| 85 |
/* I2C address = I2C base address + Page index */ |
| 86 |
const u8 addr = client->addr + page; |
| 87 |
/* |
| 88 |
* CRC is calculated from every bit included in the protocol |
| 89 |
* except the ACK bits from the target. Byte stream is: |
| 90 |
* - B0: (I2C_addr_7bits << 1) | WR_bit, with WR_bit = 0 |
| 91 |
* - B1: reg |
| 92 |
* - B2: val |
| 93 |
* - B3: CRC from B0-B1-B2 |
| 94 |
*/ |
| 95 |
const u8 crc_data[] = { addr << 1, reg, val }; |
| 96 |
|
| 97 |
/* Write register, data and CRC */ |
| 98 |
msg.addr = addr; |
| 99 |
msg.flags = client->flags & I2C_M_TEN; |
| 100 |
msg.len = sizeof(buf); |
| 101 |
msg.buf = buf; |
| 102 |
|
| 103 |
buf[msg.len - 1] = crc8(tps6594_i2c_crc_table, crc_data, sizeof(crc_data), CRC8_INIT_VALUE); |
| 104 |
|
| 105 |
return tps6594_i2c_transfer(client->adapter, &msg, 1); |
| 106 |
} |
| 107 |
|
| 108 |
static int tps6594_i2c_read(void *context, const void *reg_buf, size_t reg_size, |
| 109 |
void *val_buf, size_t val_size) |
| 110 |
{ |
| 111 |
struct i2c_client *client = context; |
| 112 |
struct tps6594 *tps = i2c_get_clientdata(client); |
| 113 |
struct i2c_msg msgs[2]; |
| 114 |
const u8 *reg_bytes = reg_buf; |
| 115 |
u8 *val_bytes = val_buf; |
| 116 |
const u8 page = reg_bytes[1]; |
| 117 |
u8 reg = reg_bytes[0]; |
| 118 |
int ret = 0; |
| 119 |
int i; |
| 120 |
|
| 121 |
if (tps->use_crc) { |
| 122 |
/* |
| 123 |
* Auto-increment feature does not support CRC protocol. |
| 124 |
* Converts the bulk read operation into a series of single read operations. |
| 125 |
*/ |
| 126 |
for (i = 0 ; ret == 0 && i < val_size ; i++) |
| 127 |
ret = tps6594_i2c_reg_read_with_crc(client, page, reg + i, val_bytes + i); |
| 128 |
|
| 129 |
return ret; |
| 130 |
} |
| 131 |
|
| 132 |
/* Write register: I2C address = I2C base address + Page index */ |
| 133 |
msgs[0].addr = client->addr + page; |
| 134 |
msgs[0].flags = 0; |
| 135 |
msgs[0].len = 1; |
| 136 |
msgs[0].buf = ® |
| 137 |
|
| 138 |
/* Read data */ |
| 139 |
msgs[1].addr = msgs[0].addr; |
| 140 |
msgs[1].flags = I2C_M_RD; |
| 141 |
msgs[1].len = val_size; |
| 142 |
msgs[1].buf = val_bytes; |
| 143 |
|
| 144 |
return tps6594_i2c_transfer(client->adapter, msgs, 2); |
| 145 |
} |
| 146 |
|
| 147 |
static int tps6594_i2c_write(void *context, const void *data, size_t count) |
| 148 |
{ |
| 149 |
struct i2c_client *client = context; |
| 150 |
struct tps6594 *tps = i2c_get_clientdata(client); |
| 151 |
struct i2c_msg msg; |
| 152 |
const u8 *bytes = data; |
| 153 |
u8 *buf; |
| 154 |
const u8 page = bytes[1]; |
| 155 |
const u8 reg = bytes[0]; |
| 156 |
int ret = 0; |
| 157 |
int i; |
| 158 |
|
| 159 |
if (tps->use_crc) { |
| 160 |
/* |
| 161 |
* Auto-increment feature does not support CRC protocol. |
| 162 |
* Converts the bulk write operation into a series of single write operations. |
| 163 |
*/ |
| 164 |
for (i = 0 ; ret == 0 && i < count - 2 ; i++) |
| 165 |
ret = tps6594_i2c_reg_write_with_crc(client, page, reg + i, bytes[i + 2]); |
| 166 |
|
| 167 |
return ret; |
| 168 |
} |
| 169 |
|
| 170 |
/* Setup buffer: page byte is not sent */ |
| 171 |
buf = kzalloc(--count, GFP_KERNEL); |
| 172 |
if (!buf) |
| 173 |
return -ENOMEM; |
| 174 |
|
| 175 |
buf[0] = reg; |
| 176 |
for (i = 0 ; i < count - 1 ; i++) |
| 177 |
buf[i + 1] = bytes[i + 2]; |
| 178 |
|
| 179 |
/* Write register and data: I2C address = I2C base address + Page index */ |
| 180 |
msg.addr = client->addr + page; |
| 181 |
msg.flags = client->flags & I2C_M_TEN; |
| 182 |
msg.len = count; |
| 183 |
msg.buf = buf; |
| 184 |
|
| 185 |
ret = tps6594_i2c_transfer(client->adapter, &msg, 1); |
| 186 |
|
| 187 |
kfree(buf); |
| 188 |
return ret; |
| 189 |
} |
| 190 |
|
| 191 |
static struct regmap_config tps6594_i2c_regmap_config = { |
| 192 |
.reg_bits = 16, |
| 193 |
.val_bits = 8, |
| 194 |
.max_register = TPS6594_REG_DWD_FAIL_CNT_REG, |
| 195 |
.volatile_table = &tps6594_volatile_table, |
| 196 |
.read = tps6594_i2c_read, |
| 197 |
.write = tps6594_i2c_write, |
| 198 |
}; |
| 199 |
|
| 200 |
static const struct of_device_id tps6594_i2c_of_match_table[] = { |
| 201 |
{ .compatible = "ti,tps6594-q1", .data = (void *)TPS6594, }, |
| 202 |
{ .compatible = "ti,tps6593-q1", .data = (void *)TPS6593, }, |
| 203 |
{ .compatible = "ti,lp8764-q1", .data = (void *)LP8764, }, |
| 204 |
{ .compatible = "ti,tps65224-q1", .data = (void *)TPS65224, }, |
| 205 |
{ .compatible = "ti,tps652g1", .data = (void *)TPS652G1, }, |
| 206 |
{} |
| 207 |
}; |
| 208 |
MODULE_DEVICE_TABLE(of, tps6594_i2c_of_match_table); |
| 209 |
|
| 210 |
static int tps6594_i2c_probe(struct i2c_client *client) |
| 211 |
{ |
| 212 |
struct device *dev = &client->dev; |
| 213 |
struct tps6594 *tps; |
| 214 |
const struct of_device_id *match; |
| 215 |
|
| 216 |
tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL); |
| 217 |
if (!tps) |
| 218 |
return -ENOMEM; |
| 219 |
|
| 220 |
i2c_set_clientdata(client, tps); |
| 221 |
|
| 222 |
tps->dev = dev; |
| 223 |
tps->reg = client->addr; |
| 224 |
tps->irq = client->irq; |
| 225 |
|
| 226 |
match = of_match_device(tps6594_i2c_of_match_table, dev); |
| 227 |
if (!match) |
| 228 |
return dev_err_probe(dev, -EINVAL, "Failed to find matching chip ID\n"); |
| 229 |
tps->chip_id = (unsigned long)match->data; |
| 230 |
|
| 231 |
if (tps->chip_id == TPS65224 || tps->chip_id == TPS652G1) |
| 232 |
tps6594_i2c_regmap_config.volatile_table = &tps65224_volatile_table; |
| 233 |
|
| 234 |
tps->regmap = devm_regmap_init(dev, NULL, client, &tps6594_i2c_regmap_config); |
| 235 |
if (IS_ERR(tps->regmap)) |
| 236 |
return dev_err_probe(dev, PTR_ERR(tps->regmap), "Failed to init regmap\n"); |
| 237 |
|
| 238 |
crc8_populate_msb(tps6594_i2c_crc_table, TPS6594_CRC8_POLYNOMIAL); |
| 239 |
|
| 240 |
return tps6594_device_init(tps, enable_crc); |
| 241 |
} |
| 242 |
|
| 243 |
static struct i2c_driver tps6594_i2c_driver = { |
| 244 |
.driver = { |
| 245 |
.name = "tps6594", |
| 246 |
.of_match_table = tps6594_i2c_of_match_table, |
| 247 |
}, |
| 248 |
.probe = tps6594_i2c_probe, |
| 249 |
}; |
| 250 |
module_i2c_driver(tps6594_i2c_driver); |
| 251 |
|
| 252 |
MODULE_AUTHOR("Julien Panis <[email protected]>"); |
| 253 |
MODULE_DESCRIPTION("I2C Interface Driver for TPS65224, TPS6594/3, and LP8764"); |
| 254 |
MODULE_LICENSE("GPL"); |