1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * hmc6352.c - Honeywell Compass Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2009 Intel Corp
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/i2c.h>
15*4882a593Smuzhiyun #include <linux/err.h>
16*4882a593Smuzhiyun #include <linux/delay.h>
17*4882a593Smuzhiyun #include <linux/sysfs.h>
18*4882a593Smuzhiyun #include <linux/nospec.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun static DEFINE_MUTEX(compass_mutex);
21*4882a593Smuzhiyun
compass_command(struct i2c_client * c,u8 cmd)22*4882a593Smuzhiyun static int compass_command(struct i2c_client *c, u8 cmd)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun int ret = i2c_master_send(c, &cmd, 1);
25*4882a593Smuzhiyun if (ret < 0)
26*4882a593Smuzhiyun dev_warn(&c->dev, "command '%c' failed.\n", cmd);
27*4882a593Smuzhiyun return ret;
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun
compass_store(struct device * dev,const char * buf,size_t count,const char * map)30*4882a593Smuzhiyun static int compass_store(struct device *dev, const char *buf, size_t count,
31*4882a593Smuzhiyun const char *map)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun struct i2c_client *c = to_i2c_client(dev);
34*4882a593Smuzhiyun int ret;
35*4882a593Smuzhiyun unsigned long val;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun ret = kstrtoul(buf, 10, &val);
38*4882a593Smuzhiyun if (ret)
39*4882a593Smuzhiyun return ret;
40*4882a593Smuzhiyun if (val >= strlen(map))
41*4882a593Smuzhiyun return -EINVAL;
42*4882a593Smuzhiyun val = array_index_nospec(val, strlen(map));
43*4882a593Smuzhiyun mutex_lock(&compass_mutex);
44*4882a593Smuzhiyun ret = compass_command(c, map[val]);
45*4882a593Smuzhiyun mutex_unlock(&compass_mutex);
46*4882a593Smuzhiyun if (ret < 0)
47*4882a593Smuzhiyun return ret;
48*4882a593Smuzhiyun return count;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
compass_calibration_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)51*4882a593Smuzhiyun static ssize_t compass_calibration_store(struct device *dev,
52*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun return compass_store(dev, buf, count, "EC");
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
compass_power_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)57*4882a593Smuzhiyun static ssize_t compass_power_mode_store(struct device *dev,
58*4882a593Smuzhiyun struct device_attribute *attr, const char *buf, size_t count)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun return compass_store(dev, buf, count, "SW");
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
compass_heading_data_show(struct device * dev,struct device_attribute * attr,char * buf)63*4882a593Smuzhiyun static ssize_t compass_heading_data_show(struct device *dev,
64*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
67*4882a593Smuzhiyun unsigned char i2c_data[2];
68*4882a593Smuzhiyun int ret;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun mutex_lock(&compass_mutex);
71*4882a593Smuzhiyun ret = compass_command(client, 'A');
72*4882a593Smuzhiyun if (ret != 1) {
73*4882a593Smuzhiyun mutex_unlock(&compass_mutex);
74*4882a593Smuzhiyun return ret;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun msleep(10); /* sending 'A' cmd we need to wait for 7-10 millisecs */
77*4882a593Smuzhiyun ret = i2c_master_recv(client, i2c_data, 2);
78*4882a593Smuzhiyun mutex_unlock(&compass_mutex);
79*4882a593Smuzhiyun if (ret < 0) {
80*4882a593Smuzhiyun dev_warn(dev, "i2c read data cmd failed\n");
81*4882a593Smuzhiyun return ret;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun ret = (i2c_data[0] << 8) | i2c_data[1];
84*4882a593Smuzhiyun return sprintf(buf, "%d.%d\n", ret/10, ret%10);
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun static DEVICE_ATTR(heading0_input, S_IRUGO, compass_heading_data_show, NULL);
89*4882a593Smuzhiyun static DEVICE_ATTR(calibration, S_IWUSR, NULL, compass_calibration_store);
90*4882a593Smuzhiyun static DEVICE_ATTR(power_state, S_IWUSR, NULL, compass_power_mode_store);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun static struct attribute *mid_att_compass[] = {
93*4882a593Smuzhiyun &dev_attr_heading0_input.attr,
94*4882a593Smuzhiyun &dev_attr_calibration.attr,
95*4882a593Smuzhiyun &dev_attr_power_state.attr,
96*4882a593Smuzhiyun NULL
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun static const struct attribute_group m_compass_gr = {
100*4882a593Smuzhiyun .name = "hmc6352",
101*4882a593Smuzhiyun .attrs = mid_att_compass
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun
hmc6352_probe(struct i2c_client * client,const struct i2c_device_id * id)104*4882a593Smuzhiyun static int hmc6352_probe(struct i2c_client *client,
105*4882a593Smuzhiyun const struct i2c_device_id *id)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun int res;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun res = sysfs_create_group(&client->dev.kobj, &m_compass_gr);
110*4882a593Smuzhiyun if (res) {
111*4882a593Smuzhiyun dev_err(&client->dev, "device_create_file failed\n");
112*4882a593Smuzhiyun return res;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun dev_info(&client->dev, "%s HMC6352 compass chip found\n",
115*4882a593Smuzhiyun client->name);
116*4882a593Smuzhiyun return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
hmc6352_remove(struct i2c_client * client)119*4882a593Smuzhiyun static int hmc6352_remove(struct i2c_client *client)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun sysfs_remove_group(&client->dev.kobj, &m_compass_gr);
122*4882a593Smuzhiyun return 0;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun static const struct i2c_device_id hmc6352_id[] = {
126*4882a593Smuzhiyun { "hmc6352", 0 },
127*4882a593Smuzhiyun { }
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, hmc6352_id);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun static struct i2c_driver hmc6352_driver = {
133*4882a593Smuzhiyun .driver = {
134*4882a593Smuzhiyun .name = "hmc6352",
135*4882a593Smuzhiyun },
136*4882a593Smuzhiyun .probe = hmc6352_probe,
137*4882a593Smuzhiyun .remove = hmc6352_remove,
138*4882a593Smuzhiyun .id_table = hmc6352_id,
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun module_i2c_driver(hmc6352_driver);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
144*4882a593Smuzhiyun MODULE_DESCRIPTION("hmc6352 Compass Driver");
145*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
146