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