brintos

brintos / linux-shallow public Read only

0
0
Text · 3.5 KiB · 6ebccb7 Raw
134 lines · c
1// SPDX-License-Identifier: GPL-2.02/*3 * SPI access driver for TI TPS65224/TPS6594/TPS6593/LP8764 PMICs4 *5 * Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/6 */7 8#include <linux/crc8.h>9#include <linux/module.h>10#include <linux/mod_devicetable.h>11#include <linux/of_device.h>12#include <linux/regmap.h>13#include <linux/spi/spi.h>14 15#include <linux/mfd/tps6594.h>16 17#define TPS6594_SPI_PAGE_SHIFT	518#define TPS6594_SPI_READ_BIT	BIT(4)19 20static bool enable_crc;21module_param(enable_crc, bool, 0444);22MODULE_PARM_DESC(enable_crc, "Enable CRC feature for SPI interface");23 24DECLARE_CRC8_TABLE(tps6594_spi_crc_table);25 26static int tps6594_spi_reg_read(void *context, unsigned int reg, unsigned int *val)27{28	struct spi_device *spi = context;29	struct tps6594 *tps = spi_get_drvdata(spi);30	u8 buf[4] = { 0 };31	size_t count_rx = 1;32	int ret;33 34	buf[0] = reg;35	buf[1] = TPS6594_REG_TO_PAGE(reg) << TPS6594_SPI_PAGE_SHIFT | TPS6594_SPI_READ_BIT;36 37	if (tps->use_crc)38		count_rx++;39 40	ret = spi_write_then_read(spi, buf, 2, buf + 2, count_rx);41	if (ret < 0)42		return ret;43 44	if (tps->use_crc && buf[3] != crc8(tps6594_spi_crc_table, buf, 3, CRC8_INIT_VALUE))45		return -EIO;46 47	*val = buf[2];48 49	return 0;50}51 52static int tps6594_spi_reg_write(void *context, unsigned int reg, unsigned int val)53{54	struct spi_device *spi = context;55	struct tps6594 *tps = spi_get_drvdata(spi);56	u8 buf[4] = { 0 };57	size_t count = 3;58 59	buf[0] = reg;60	buf[1] = TPS6594_REG_TO_PAGE(reg) << TPS6594_SPI_PAGE_SHIFT;61	buf[2] = val;62 63	if (tps->use_crc)64		buf[3] = crc8(tps6594_spi_crc_table, buf, count++, CRC8_INIT_VALUE);65 66	return spi_write(spi, buf, count);67}68 69static struct regmap_config tps6594_spi_regmap_config = {70	.reg_bits = 16,71	.val_bits = 8,72	.max_register = TPS6594_REG_DWD_FAIL_CNT_REG,73	.volatile_table = &tps6594_volatile_table,74	.reg_read = tps6594_spi_reg_read,75	.reg_write = tps6594_spi_reg_write,76	.use_single_read = true,77	.use_single_write = true,78};79 80static const struct of_device_id tps6594_spi_of_match_table[] = {81	{ .compatible = "ti,tps6594-q1", .data = (void *)TPS6594, },82	{ .compatible = "ti,tps6593-q1", .data = (void *)TPS6593, },83	{ .compatible = "ti,lp8764-q1",  .data = (void *)LP8764,  },84	{ .compatible = "ti,tps65224-q1", .data = (void *)TPS65224, },85	{}86};87MODULE_DEVICE_TABLE(of, tps6594_spi_of_match_table);88 89static int tps6594_spi_probe(struct spi_device *spi)90{91	struct device *dev = &spi->dev;92	struct tps6594 *tps;93	const struct of_device_id *match;94 95	tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);96	if (!tps)97		return -ENOMEM;98 99	spi_set_drvdata(spi, tps);100 101	tps->dev = dev;102	tps->reg = spi_get_chipselect(spi, 0);103	tps->irq = spi->irq;104 105	match = of_match_device(tps6594_spi_of_match_table, dev);106	if (!match)107		return dev_err_probe(dev, -EINVAL, "Failed to find matching chip ID\n");108	tps->chip_id = (unsigned long)match->data;109 110	if (tps->chip_id == TPS65224)111		tps6594_spi_regmap_config.volatile_table = &tps65224_volatile_table;112 113	tps->regmap = devm_regmap_init(dev, NULL, spi, &tps6594_spi_regmap_config);114	if (IS_ERR(tps->regmap))115		return dev_err_probe(dev, PTR_ERR(tps->regmap), "Failed to init regmap\n");116 117	crc8_populate_msb(tps6594_spi_crc_table, TPS6594_CRC8_POLYNOMIAL);118 119	return tps6594_device_init(tps, enable_crc);120}121 122static struct spi_driver tps6594_spi_driver = {123	.driver	= {124		.name = "tps6594",125		.of_match_table = tps6594_spi_of_match_table,126	},127	.probe = tps6594_spi_probe,128};129module_spi_driver(tps6594_spi_driver);130 131MODULE_AUTHOR("Julien Panis <jpanis@baylibre.com>");132MODULE_DESCRIPTION("SPI Interface Driver for TPS65224, TPS6594/3, and LP8764");133MODULE_LICENSE("GPL");134