Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / humidity / hts221_i2c.c
blob2c97350a0f760dd46f3586a1fa124d65515b18cd
1 /*
2 * STMicroelectronics hts221 i2c driver
4 * Copyright 2016 STMicroelectronics Inc.
6 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
8 * Licensed under the GPL-2.
9 */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/acpi.h>
14 #include <linux/i2c.h>
15 #include <linux/slab.h>
16 #include "hts221.h"
18 #define I2C_AUTO_INCREMENT 0x80
20 static int hts221_i2c_read(struct device *dev, u8 addr, int len, u8 *data)
22 struct i2c_msg msg[2];
23 struct i2c_client *client = to_i2c_client(dev);
25 if (len > 1)
26 addr |= I2C_AUTO_INCREMENT;
28 msg[0].addr = client->addr;
29 msg[0].flags = client->flags;
30 msg[0].len = 1;
31 msg[0].buf = &addr;
33 msg[1].addr = client->addr;
34 msg[1].flags = client->flags | I2C_M_RD;
35 msg[1].len = len;
36 msg[1].buf = data;
38 return i2c_transfer(client->adapter, msg, 2);
41 static int hts221_i2c_write(struct device *dev, u8 addr, int len, u8 *data)
43 u8 send[len + 1];
44 struct i2c_msg msg;
45 struct i2c_client *client = to_i2c_client(dev);
47 if (len > 1)
48 addr |= I2C_AUTO_INCREMENT;
50 send[0] = addr;
51 memcpy(&send[1], data, len * sizeof(u8));
53 msg.addr = client->addr;
54 msg.flags = client->flags;
55 msg.len = len + 1;
56 msg.buf = send;
58 return i2c_transfer(client->adapter, &msg, 1);
61 static const struct hts221_transfer_function hts221_transfer_fn = {
62 .read = hts221_i2c_read,
63 .write = hts221_i2c_write,
66 static int hts221_i2c_probe(struct i2c_client *client,
67 const struct i2c_device_id *id)
69 return hts221_probe(&client->dev, client->irq,
70 client->name, &hts221_transfer_fn);
73 static const struct acpi_device_id hts221_acpi_match[] = {
74 {"SMO9100", 0},
75 { },
77 MODULE_DEVICE_TABLE(acpi, hts221_acpi_match);
79 static const struct of_device_id hts221_i2c_of_match[] = {
80 { .compatible = "st,hts221", },
81 {},
83 MODULE_DEVICE_TABLE(of, hts221_i2c_of_match);
85 static const struct i2c_device_id hts221_i2c_id_table[] = {
86 { HTS221_DEV_NAME },
87 {},
89 MODULE_DEVICE_TABLE(i2c, hts221_i2c_id_table);
91 static struct i2c_driver hts221_driver = {
92 .driver = {
93 .name = "hts221_i2c",
94 .pm = &hts221_pm_ops,
95 .of_match_table = of_match_ptr(hts221_i2c_of_match),
96 .acpi_match_table = ACPI_PTR(hts221_acpi_match),
98 .probe = hts221_i2c_probe,
99 .id_table = hts221_i2c_id_table,
101 module_i2c_driver(hts221_driver);
103 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
104 MODULE_DESCRIPTION("STMicroelectronics hts221 i2c driver");
105 MODULE_LICENSE("GPL v2");