#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include "st7571.h"
struct st7571_i2c_transport {
struct i2c_client *client;
bool ignore_nak;
};
static int st7571_i2c_regmap_write(void *context, const void *data, size_t count)
{
struct st7571_i2c_transport *t = context;
int ret;
struct i2c_msg msg = {
.addr = t->client->addr,
.flags = t->ignore_nak ? I2C_M_IGNORE_NAK : 0,
.len = count,
.buf = (u8 *)data
};
ret = i2c_transfer(t->client->adapter, &msg, 1);
if (ret < 0 && t->ignore_nak)
return ret;
return 0;
}
static int st7571_i2c_regmap_read(void *context, const void *reg_buf,
size_t reg_size, void *val_buf, size_t val_size)
{
return -EOPNOTSUPP;
}
static const struct regmap_bus st7571_i2c_regmap_bus = {
.read = st7571_i2c_regmap_read,
.write = st7571_i2c_regmap_write,
};
static const struct regmap_config st7571_i2c_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.use_single_write = true,
};
static int st7571_i2c_probe(struct i2c_client *client)
{
struct st7571_device *st7571;
struct st7571_i2c_transport *t;
struct regmap *regmap;
t = devm_kzalloc(&client->dev, sizeof(*t), GFP_KERNEL);
if (!t)
return -ENOMEM;
t->client = client;
if (i2c_check_functionality(client->adapter, I2C_FUNC_PROTOCOL_MANGLING))
t->ignore_nak = true;
regmap = devm_regmap_init(&client->dev, &st7571_i2c_regmap_bus,
t, &st7571_i2c_regmap_config);
if (IS_ERR(regmap)) {
return dev_err_probe(&client->dev, PTR_ERR(regmap),
"Failed to initialize regmap\n");
}
st7571 = st7571_probe(&client->dev, regmap);
if (IS_ERR(st7571))
return dev_err_probe(&client->dev, PTR_ERR(st7571),
"Failed to initialize regmap\n");
i2c_set_clientdata(client, st7571);
return 0;
}
static void st7571_i2c_remove(struct i2c_client *client)
{
struct st7571_device *st7571 = i2c_get_clientdata(client);
st7571_remove(st7571);
}
static const struct of_device_id st7571_of_match[] = {
{ .compatible = "sitronix,st7567", .data = &st7567_config },
{ .compatible = "sitronix,st7571", .data = &st7571_config },
{},
};
MODULE_DEVICE_TABLE(of, st7571_of_match);
static const struct i2c_device_id st7571_id[] = {
{ "st7567", 0 },
{ "st7571", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, st7571_id);
static struct i2c_driver st7571_i2c_driver = {
.driver = {
.name = "st7571-i2c",
.of_match_table = st7571_of_match,
},
.probe = st7571_i2c_probe,
.remove = st7571_i2c_remove,
.id_table = st7571_id,
};
module_i2c_driver(st7571_i2c_driver);
MODULE_AUTHOR("Marcus Folkesson <marcus.folkesson@gmail.com>");
MODULE_DESCRIPTION("DRM Driver for Sitronix ST7571 LCD controller (I2C)");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("DRM_ST7571");