xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/hall/och165t_hall.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* drivers/input/sensors/access/kxtik.c
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Copyright (C) 2012-2015 ROCKCHIP.
4*4882a593Smuzhiyun  * Author: luowei <lw@rock-chips.com>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This software is licensed under the terms of the GNU General Public
7*4882a593Smuzhiyun  * License version 2, as published by the Free Software Foundation, and
8*4882a593Smuzhiyun  * may be copied, distributed, and modified under those terms.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful,
11*4882a593Smuzhiyun  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*4882a593Smuzhiyun  * GNU General Public License for more details.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun #include <linux/interrupt.h>
17*4882a593Smuzhiyun #include <linux/i2c.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/irq.h>
20*4882a593Smuzhiyun #include <linux/miscdevice.h>
21*4882a593Smuzhiyun #include <linux/gpio.h>
22*4882a593Smuzhiyun #include <linux/uaccess.h>
23*4882a593Smuzhiyun #include <asm/atomic.h>
24*4882a593Smuzhiyun #include <linux/delay.h>
25*4882a593Smuzhiyun #include <linux/input.h>
26*4882a593Smuzhiyun #include <linux/workqueue.h>
27*4882a593Smuzhiyun #include <linux/freezer.h>
28*4882a593Smuzhiyun #include <linux/of_gpio.h>
29*4882a593Smuzhiyun #ifdef CONFIG_HAS_EARLYSUSPEND
30*4882a593Smuzhiyun #include <linux/earlysuspend.h>
31*4882a593Smuzhiyun #endif
32*4882a593Smuzhiyun #include <linux/rk_keys.h>
33*4882a593Smuzhiyun #include <linux/sensor-dev.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define CM3232_CLOSE	0x01
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define CM3232_ADDR_COM 0
40*4882a593Smuzhiyun #define CM3232_ADDR_DATA 50
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define CM3232_DRV_NAME "cm3232"
43*4882a593Smuzhiyun //command code
44*4882a593Smuzhiyun #define COMMAND_CTRL 		0
45*4882a593Smuzhiyun #define COMMAND_ALS_DATA 	50 		//ALS: 15:8 MSB 8bits data
46*4882a593Smuzhiyun 						//7:0 LSB 8bits data
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /****************operate according to sensor chip:start************/
49*4882a593Smuzhiyun 
sensor_active(struct i2c_client * client,int enable,int rate)50*4882a593Smuzhiyun static int sensor_active(struct i2c_client *client, int enable, int rate)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct sensor_private_data *sensor =
53*4882a593Smuzhiyun 	    (struct sensor_private_data *) i2c_get_clientdata(client);
54*4882a593Smuzhiyun 	if(enable)
55*4882a593Smuzhiyun 	{
56*4882a593Smuzhiyun 		sensor->status_cur = SENSOR_ON;
57*4882a593Smuzhiyun 	}
58*4882a593Smuzhiyun 	else
59*4882a593Smuzhiyun 	{
60*4882a593Smuzhiyun 		sensor->status_cur = SENSOR_OFF;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 	return 0;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
sensor_init(struct i2c_client * client)65*4882a593Smuzhiyun static int sensor_init(struct i2c_client *client)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	struct sensor_private_data *sensor =
68*4882a593Smuzhiyun 	    (struct sensor_private_data *) i2c_get_clientdata(client);
69*4882a593Smuzhiyun 	int result = 0;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	result = sensor->ops->active(client,0,0);
72*4882a593Smuzhiyun 	if(result)
73*4882a593Smuzhiyun 	{
74*4882a593Smuzhiyun 		printk("%s:line=%d,error\n",__func__,__LINE__);
75*4882a593Smuzhiyun 		return result;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	sensor->status_cur = SENSOR_OFF;
79*4882a593Smuzhiyun 	return result;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 
sensor_report_value(struct i2c_client * client)83*4882a593Smuzhiyun static int sensor_report_value(struct i2c_client *client)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	struct sensor_private_data *sensor =
86*4882a593Smuzhiyun 	    (struct sensor_private_data *) i2c_get_clientdata(client);
87*4882a593Smuzhiyun 	struct sensor_platform_data *pdata = sensor->pdata;
88*4882a593Smuzhiyun 	int gpio_value = 0;
89*4882a593Smuzhiyun 	gpio_value = gpio_get_value(pdata->irq_pin);
90*4882a593Smuzhiyun 	if(gpio_value == 0)
91*4882a593Smuzhiyun 	{
92*4882a593Smuzhiyun 		//send power key to sleep
93*4882a593Smuzhiyun 		rk_send_power_key(1);
94*4882a593Smuzhiyun 		rk_send_power_key(0);
95*4882a593Smuzhiyun 	}
96*4882a593Smuzhiyun 	else
97*4882a593Smuzhiyun 	{
98*4882a593Smuzhiyun 		//rk_send_power_key(1);
99*4882a593Smuzhiyun 		//rk_send_power_key(0);
100*4882a593Smuzhiyun 		rk_send_wakeup_key(); // wake up the system
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 	return 0;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun static struct sensor_operate hall_och165t_ops = {
107*4882a593Smuzhiyun 	.name				= "och165t",
108*4882a593Smuzhiyun 	.type				= SENSOR_TYPE_HALL,	//sensor type and it should be correct
109*4882a593Smuzhiyun 	.id_i2c				= HALL_ID_OCH165T,	//i2c id number
110*4882a593Smuzhiyun 	.read_reg			= SENSOR_UNKNOW_DATA,	//read data
111*4882a593Smuzhiyun 	.read_len			= 2,			//data length
112*4882a593Smuzhiyun 	.id_reg				= SENSOR_UNKNOW_DATA,	//read device id from this register
113*4882a593Smuzhiyun 	.id_data 			= SENSOR_UNKNOW_DATA,	//device id
114*4882a593Smuzhiyun 	.precision			= 8,			//8 bits
115*4882a593Smuzhiyun 	.ctrl_reg 			= SENSOR_UNKNOW_DATA,	//enable or disable
116*4882a593Smuzhiyun 	.int_status_reg 		= SENSOR_UNKNOW_DATA,	//intterupt status register
117*4882a593Smuzhiyun 	.range				= {100,65535},		//range
118*4882a593Smuzhiyun 	.brightness         		= {10,255},             // brightness
119*4882a593Smuzhiyun 	.trig				= SENSOR_UNKNOW_DATA,
120*4882a593Smuzhiyun 	.active				= sensor_active,
121*4882a593Smuzhiyun 	.init				= sensor_init,
122*4882a593Smuzhiyun 	.report				= sensor_report_value,
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /****************operate according to sensor chip:end************/
hall_och165t_probe(struct i2c_client * client,const struct i2c_device_id * devid)126*4882a593Smuzhiyun static int hall_och165t_probe(struct i2c_client *client,
127*4882a593Smuzhiyun 			      const struct i2c_device_id *devid)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	return sensor_register_device(client, NULL, devid, &hall_och165t_ops);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
hall_och165t_remove(struct i2c_client * client)132*4882a593Smuzhiyun static int hall_och165t_remove(struct i2c_client *client)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	return sensor_unregister_device(client, NULL, &hall_och165t_ops);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun static const struct i2c_device_id hall_och165t_id[] = {
138*4882a593Smuzhiyun 	{"hall_och165t", HALL_ID_OCH165T},
139*4882a593Smuzhiyun 	{}
140*4882a593Smuzhiyun };
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun static struct i2c_driver hall_och165t_driver = {
143*4882a593Smuzhiyun 	.probe = hall_och165t_probe,
144*4882a593Smuzhiyun 	.remove = hall_och165t_remove,
145*4882a593Smuzhiyun 	.shutdown = sensor_shutdown,
146*4882a593Smuzhiyun 	.id_table = hall_och165t_id,
147*4882a593Smuzhiyun 	.driver = {
148*4882a593Smuzhiyun 		.name = "hall_och165t",
149*4882a593Smuzhiyun 	#ifdef CONFIG_PM
150*4882a593Smuzhiyun 		.pm = &sensor_pm_ops,
151*4882a593Smuzhiyun 	#endif
152*4882a593Smuzhiyun 	},
153*4882a593Smuzhiyun };
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun module_i2c_driver(hall_och165t_driver);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun MODULE_AUTHOR("luowei <lw@rock-chips.com>");
158*4882a593Smuzhiyun MODULE_DESCRIPTION("och165t hall driver");
159*4882a593Smuzhiyun MODULE_LICENSE("GPL");
160