Blame view

kernel/linux-imx6_3.14.28/drivers/hwmon/htu21.c 5.06 KB
6b13f685e   김민수   BSP 최초 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  /*
   * Measurement Specialties HTU21D humidity and temperature sensor driver
   *
   * Copyright (C) 2013 William Markezana <william.markezana@meas-spec.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   */
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/i2c.h>
  #include <linux/hwmon.h>
  #include <linux/hwmon-sysfs.h>
  #include <linux/err.h>
  #include <linux/mutex.h>
  #include <linux/device.h>
  #include <linux/jiffies.h>
  
  /* HTU21 Commands */
  #define HTU21_T_MEASUREMENT_HM	0xE3
  #define HTU21_RH_MEASUREMENT_HM	0xE5
  
  struct htu21 {
  	struct device *hwmon_dev;
  	struct mutex lock;
  	bool valid;
  	unsigned long last_update;
  	int temperature;
  	int humidity;
  };
  
  static inline int htu21_temp_ticks_to_millicelsius(int ticks)
  {
  	ticks &= ~0x0003; /* clear status bits */
  	/*
  	 * Formula T = -46.85 + 175.72 * ST / 2^16 from datasheet p14,
  	 * optimized for integer fixed point (3 digits) arithmetic
  	 */
  	return ((21965 * ticks) >> 13) - 46850;
  }
  
  static inline int htu21_rh_ticks_to_per_cent_mille(int ticks)
  {
  	ticks &= ~0x0003; /* clear status bits */
  	/*
  	 * Formula RH = -6 + 125 * SRH / 2^16 from datasheet p14,
  	 * optimized for integer fixed point (3 digits) arithmetic
  	 */
  	return ((15625 * ticks) >> 13) - 6000;
  }
  
  static int htu21_update_measurements(struct i2c_client *client)
  {
  	int ret = 0;
  	struct htu21 *htu21 = i2c_get_clientdata(client);
  
  	mutex_lock(&htu21->lock);
  
  	if (time_after(jiffies, htu21->last_update + HZ / 2) ||
  	    !htu21->valid) {
  		ret = i2c_smbus_read_word_swapped(client,
  						  HTU21_T_MEASUREMENT_HM);
  		if (ret < 0)
  			goto out;
  		htu21->temperature = htu21_temp_ticks_to_millicelsius(ret);
  		ret = i2c_smbus_read_word_swapped(client,
  						  HTU21_RH_MEASUREMENT_HM);
  		if (ret < 0)
  			goto out;
  		htu21->humidity = htu21_rh_ticks_to_per_cent_mille(ret);
  		htu21->last_update = jiffies;
  		htu21->valid = true;
  	}
  out:
  	mutex_unlock(&htu21->lock);
  
  	return ret >= 0 ? 0 : ret;
  }
  
  static ssize_t htu21_show_temperature(struct device *dev,
  				      struct device_attribute *attr, char *buf)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct htu21 *htu21 = i2c_get_clientdata(client);
  	int ret = htu21_update_measurements(client);
  	if (ret < 0)
  		return ret;
  	return sprintf(buf, "%d
  ", htu21->temperature);
  }
  
  static ssize_t htu21_show_humidity(struct device *dev,
  				   struct device_attribute *attr, char *buf)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct htu21 *htu21 = i2c_get_clientdata(client);
  	int ret = htu21_update_measurements(client);
  	if (ret < 0)
  		return ret;
  	return sprintf(buf, "%d
  ", htu21->humidity);
  }
  
  static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  			  htu21_show_temperature, NULL, 0);
  static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
  			  htu21_show_humidity, NULL, 0);
  
  static struct attribute *htu21_attributes[] = {
  	&sensor_dev_attr_temp1_input.dev_attr.attr,
  	&sensor_dev_attr_humidity1_input.dev_attr.attr,
  	NULL
  };
  
  static const struct attribute_group htu21_group = {
  	.attrs = htu21_attributes,
  };
  
  static int htu21_probe(struct i2c_client *client,
  		       const struct i2c_device_id *id)
  {
  	struct htu21 *htu21;
  	int err;
  
  	if (!i2c_check_functionality(client->adapter,
  				     I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  		dev_err(&client->dev,
  			"adapter does not support SMBus word transactions
  ");
  		return -ENODEV;
  	}
  
  	htu21 = devm_kzalloc(&client->dev, sizeof(*htu21), GFP_KERNEL);
  	if (!htu21)
  		return -ENOMEM;
  
  	i2c_set_clientdata(client, htu21);
  
  	mutex_init(&htu21->lock);
  
  	err = sysfs_create_group(&client->dev.kobj, &htu21_group);
  	if (err) {
  		dev_dbg(&client->dev, "could not create sysfs files
  ");
  		return err;
  	}
  	htu21->hwmon_dev = hwmon_device_register(&client->dev);
  	if (IS_ERR(htu21->hwmon_dev)) {
  		dev_dbg(&client->dev, "unable to register hwmon device
  ");
  		err = PTR_ERR(htu21->hwmon_dev);
  		goto error;
  	}
  
  	dev_info(&client->dev, "initialized
  ");
  
  	return 0;
  
  error:
  	sysfs_remove_group(&client->dev.kobj, &htu21_group);
  	return err;
  }
  
  static int htu21_remove(struct i2c_client *client)
  {
  	struct htu21 *htu21 = i2c_get_clientdata(client);
  
  	hwmon_device_unregister(htu21->hwmon_dev);
  	sysfs_remove_group(&client->dev.kobj, &htu21_group);
  
  	return 0;
  }
  
  static const struct i2c_device_id htu21_id[] = {
  	{ "htu21", 0 },
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, htu21_id);
  
  static struct i2c_driver htu21_driver = {
  	.class = I2C_CLASS_HWMON,
  	.driver = {
  		.name	= "htu21",
  	},
  	.probe       = htu21_probe,
  	.remove      = htu21_remove,
  	.id_table    = htu21_id,
  };
  
  module_i2c_driver(htu21_driver);
  
  MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
  MODULE_DESCRIPTION("MEAS HTU21D humidity and temperature sensor driver");
  MODULE_LICENSE("GPL");