xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/pressure/pr_ms5607.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* drivers/input/sensors/pressure/ms5607.c
2  *
3  * Copyright (C) 2012-2015 ROCKCHIP.
4  * Author: luowei <lw@rock-chips.com>
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <linux/irq.h>
20 #include <linux/miscdevice.h>
21 #include <linux/gpio.h>
22 #include <linux/uaccess.h>
23 #include <asm/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/workqueue.h>
27 #include <linux/freezer.h>
28 #include <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
31 #endif
32 #include <linux/sensor-dev.h>
33 
34 
35 #define CMD_RESET   0x1E  // ADC reset command
36 #define CMD_ADC_READ 0x00  // ADC read command
37 #define CMD_ADC_CONV 0x40  // ADC conversion command
38 #define CMD_ADC_D1   0x00    // ADC D1 conversion
39 #define CMD_ADC_D2   0x10    // ADC D2 conversion
40 #define CMD_ADC_256  0x00    // ADC OSR=256
41 #define CMD_ADC_512  0x02    // ADC OSR=512
42 #define CMD_ADC_1024 0x04    // ADC OSR=1024
43 #define CMD_ADC_2048 0x06    // ADC OSR=2048
44 #define CMD_ADC_4096 0x08    // ADC OSR=4096
45 #define CMD_PROM_RD  0xA0  // Prom read command
46 
47 
48 /****************operate according to sensor chip:start************/
49 
50 static int C[8] = {0};
51 int g_ms5607_temp;
52 int g_ms5607_pr_status;
53 
54 #if defined(CONFIG_TMP_MS5607)
55 extern int g_ms5607_temp_status;
56 #else
57 static int g_ms5607_temp_status = SENSOR_OFF;
58 #endif
59 
60 
sensor_active(struct i2c_client * client,int enable,int rate)61 static int sensor_active(struct i2c_client *client, int enable, int rate)
62 {
63 	int result = 0;
64 	int i = 0;
65 	char prom[16];
66 
67 	if((enable) && (g_ms5607_temp_status == SENSOR_OFF))
68 	{
69 		result = sensor_write_reg_normal(client, CMD_RESET);
70 		if(result)
71 		printk("%s:line=%d,error\n",__func__,__LINE__);
72 
73 		//Read PROM (128 bit of calibration words)
74 		memset(prom, 0, 16);
75 		prom[0]= CMD_PROM_RD;//CMD_PROM_RD;
76 		for(i=0; i<8; i++)
77 		{
78 			prom[i*2]= CMD_PROM_RD + i*2;
79 			result = sensor_rx_data(client, &prom[i*2], 2);
80 			if(result)
81 			{
82 				printk("%s:line=%d,error\n",__func__,__LINE__);
83 				return result;
84 			}
85 		}
86 
87 		for (i=0;i<8;i++)
88 		{
89 			C[i] = prom[2*i] << 8 | prom[2*i + 1];
90 			//printk("prom[%d]=0x%x,prom[%d]=0x%x",2*i,prom[2*i],(2*i + 1),prom[2*i + 1]);
91 			//printk("\nC[%d]=%d,",i+1,C[i]);
92 		}
93 
94 	}
95 
96 	g_ms5607_pr_status = enable;
97 
98 	return result;
99 }
100 
sensor_init(struct i2c_client * client)101 static int sensor_init(struct i2c_client *client)
102 {
103 	struct sensor_private_data *sensor =
104 	    (struct sensor_private_data *) i2c_get_clientdata(client);
105 	int result = 0;
106 
107 	result = sensor->ops->active(client,0,0);
108 	if(result)
109 	{
110 		printk("%s:line=%d,error\n",__func__,__LINE__);
111 		return result;
112 	}
113 
114 	sensor->status_cur = SENSOR_OFF;
115 	g_ms5607_pr_status = sensor->status_cur;
116 
117 	//Reset
118 	//result = sensor_write_reg_normal(client, CMD_RESET);
119 	//if(result)
120         //printk("%s:line=%d,error\n",__func__,__LINE__);
121 
122 	return result;
123 }
124 
125 
pressure_report_value(struct input_dev * input,int data)126 static int pressure_report_value(struct input_dev *input, int data)
127 {
128 	//get pressure, high and temperature from register data
129 
130 	input_report_abs(input, ABS_PRESSURE, data);
131 	input_sync(input);
132 
133 	return 0;
134 }
135 
136 
sensor_report_value(struct i2c_client * client)137 static int sensor_report_value(struct i2c_client *client)
138 {
139 	struct sensor_private_data *sensor =
140 	    (struct sensor_private_data *) i2c_get_clientdata(client);
141 
142 	int result = 0;
143 	char buffer[3];
144 	char index = 0;
145 	unsigned int  D1=0, D2=0;
146 
147 	int T2 = 0;
148 	long long OFF = 0;	// offset at actual temperature
149 	long long SENS = 0;	// sensitivity at actual temperature
150 	int dT = 0;		// difference between actual and measured temperature
151 	long long OFF2 = 0;
152 	long long SENS2 = 0;
153 	int P = 0;		// compensated pressure value
154 
155 
156 	memset(buffer, 0, 3);
157 	if(sensor->ops->read_len < 3)	//sensor->ops->read_len = 3
158 	{
159 		printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
160 		return -1;
161 	}
162 
163 	//D1 conversion
164 	sensor_write_reg_normal(client,  CMD_ADC_CONV + CMD_ADC_D1 + CMD_ADC_4096);
165 	msleep(10);
166 
167 	memset(buffer, 0, 3);
168 	buffer[0] = CMD_ADC_READ;
169 	result = sensor_rx_data(client, &buffer[0], 3);
170 	if(result)
171 	{
172 		printk("%s:line=%d,error\n",__func__,__LINE__);
173 		return result;
174 	}
175 
176 	D1 = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
177 	DBG("\nD1=%d :buffer[0]=0x%x,buffer[1]=0x%x,buffer2]=0x%x\n",D1,buffer[0],buffer[1],buffer[2]);
178 
179 	//D2 conversion
180 	sensor_write_reg_normal(client,  CMD_ADC_CONV + CMD_ADC_D2 + CMD_ADC_4096);
181 	msleep(10);
182 
183 	memset(buffer, 0, 3);
184 	buffer[0] = CMD_ADC_READ;
185 	result = sensor_rx_data(client, &buffer[0], 3);
186 	if(result)
187 	{
188 		printk("%s:line=%d,error\n",__func__,__LINE__);
189 		return result;
190 	}
191 
192 	D2 = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
193 	DBG("D2=%d:buffer[0]=0x%x,buffer[1]=0x%x,buffer2]=0x%x\n",D2,buffer[0],buffer[1],buffer[2]);
194 
195 	dT = D2 - ((unsigned int)C[5] << 8);
196 
197 	g_ms5607_temp = (int)(2000 + ((long long)dT * C[6] >> 23));
198 
199 	OFF = ((unsigned long long)C[2] << 17) + (C[4] * (long long)dT >> 6);
200 
201 	SENS = ((long long)C[1] << 16) + (C[3] * (long long)dT >> 7);
202 
203 	/*calcualte 2nd order pressure and temperature (BP5607 2nd order algorithm)*/
204 	if (g_ms5607_temp < -4000 || g_ms5607_temp > 8500)
205 	{
206 		printk("%s:temperature is error\n",__func__);
207 		return -1;
208 	}
209 
210 	if (g_ms5607_temp < 2000)
211 	{
212 		int tmp;
213 		tmp = (g_ms5607_temp - 2000) * (g_ms5607_temp - 2000);
214 
215 		T2 = (int)((long long)(dT * dT) >> 31);
216 		OFF2 = (((long long)tmp * 61)*((long long)tmp * 61)) >> 4;
217 		SENS2 = (long long)((tmp*tmp) << 1);
218 
219 		if (g_ms5607_temp < -1500)
220 		{
221 			tmp = (g_ms5607_temp + 1500) * (g_ms5607_temp + 1500);
222 			OFF2 += 15 * tmp;
223 			SENS2 += 8 * tmp;
224 		}
225 	}
226 	else
227 	{
228 		T2=0;
229 		OFF2 = 0;
230 		SENS2 = 0;
231 	}
232 
233 	g_ms5607_temp -= T2;
234 	OFF -= OFF2;
235 	SENS -= SENS2;
236 	P = (int)((((D1 * SENS) >> 21) - OFF) >> 15);
237 
238 	index = pressure_report_value(sensor->input_dev, P);
239 
240 	DBG("%s:pressure=%d,temperature=%d\n",__func__,P,g_ms5607_temp);
241 
242 	return result;
243 }
244 
245 static struct sensor_operate pressure_ms5607_ops = {
246 	.name				= "pr_ms5607",
247 	.type				= SENSOR_TYPE_PRESSURE,	//sensor type and it should be correct
248 	.id_i2c				= PRESSURE_ID_MS5607,	//i2c id number
249 	.read_reg			= SENSOR_UNKNOW_DATA,	//read data
250 	.read_len			= 3,			//data length
251 	.id_reg				= SENSOR_UNKNOW_DATA,	//read device id from this register
252 	.id_data 			= SENSOR_UNKNOW_DATA,	//device id
253 	.precision			= 24,			//8 bits
254 	.ctrl_reg 			= SENSOR_UNKNOW_DATA,	//enable or disable
255 	.int_status_reg 		= SENSOR_UNKNOW_DATA,	//intterupt status register
256 	.range				= {100,65535},		//range
257 	.brightness			= {10,255},     	//brightness
258 	.trig				= IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
259 	.active				= sensor_active,
260 	.init				= sensor_init,
261 	.report				= sensor_report_value,
262 };
263 
264 /****************operate according to sensor chip:end************/
pressure_ms5607_probe(struct i2c_client * client,const struct i2c_device_id * devid)265 static int pressure_ms5607_probe(struct i2c_client *client, const struct i2c_device_id *devid)
266 {
267 	return sensor_register_device(client, NULL, devid, &pressure_ms5607_ops);
268 }
269 
pressure_ms5607_remove(struct i2c_client * client)270 static int pressure_ms5607_remove(struct i2c_client *client)
271 {
272 	return sensor_unregister_device(client, NULL, &pressure_ms5607_ops);
273 }
274 
275 static const struct i2c_device_id pressure_ms5607_id[] = {
276 	{"pr_ms5607", PRESSURE_ID_MS5607},
277 	{}
278 };
279 
280 static struct i2c_driver pressure_ms5607_driver = {
281 	.probe = pressure_ms5607_probe,
282 	.remove = pressure_ms5607_remove,
283 	.shutdown = sensor_shutdown,
284 	.id_table = pressure_ms5607_id,
285 	.driver = {
286 		.name = "pressure_ms5607",
287 	#ifdef CONFIG_PM
288 		.pm = &sensor_pm_ops,
289 	#endif
290 	},
291 };
292 
293 module_i2c_driver(pressure_ms5607_driver);
294 
295 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
296 MODULE_DESCRIPTION("ms5607 pressure driver");
297 MODULE_LICENSE("GPL");
298