zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * stk3x1x.c - Linux kernel modules for sensortek stk301x, stk321x and stk331x |
| 3 | * proximity/ambient light sensor |
| 4 | * |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 5 | * Copyright (c) 2013, The Linux Foundation. All Rights Reserved. |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 6 | * Copyright (C) 2012 Lex Hsieh / sensortek <lex_hsieh@sitronix.com.tw> or |
| 7 | * <lex_hsieh@sensortek.com.tw> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 14 | * Linux Foundation chooses to take subject only to the GPLv2 license |
| 15 | * terms, and distributes only under these terms. |
| 16 | * |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/i2c.h> |
| 31 | #include <linux/mutex.h> |
| 32 | #include <linux/kdev_t.h> |
| 33 | #include <linux/fs.h> |
| 34 | #include <linux/input.h> |
Jie Cheng | 246a936 | 2013-12-06 13:06:31 +0800 | [diff] [blame] | 35 | #include <linux/sensors.h> |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 36 | #include <linux/workqueue.h> |
| 37 | #include <linux/irq.h> |
| 38 | #include <linux/delay.h> |
| 39 | #include <linux/sched.h> |
| 40 | #include <linux/kthread.h> |
| 41 | #include <linux/errno.h> |
| 42 | #include <linux/wakelock.h> |
| 43 | #include <linux/interrupt.h> |
| 44 | #include <linux/gpio.h> |
| 45 | #include <linux/fs.h> |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 46 | #include <linux/uaccess.h> |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 47 | #include <linux/regulator/consumer.h> |
Bingzhe Cai | 1ccffb7 | 2013-07-26 01:31:19 +0800 | [diff] [blame] | 48 | #ifdef CONFIG_OF |
| 49 | #include <linux/of_gpio.h> |
| 50 | #endif |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 51 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 52 | #include <linux/earlysuspend.h> |
| 53 | #endif |
| 54 | #include "linux/stk3x1x.h" |
| 55 | |
| 56 | #define DRIVER_VERSION "3.4.4ts" |
| 57 | |
| 58 | /* Driver Settings */ |
| 59 | #define CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD |
| 60 | #ifdef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD |
| 61 | #define STK_ALS_CHANGE_THD 20 /* The threshold to trigger ALS interrupt, unit: lux */ |
| 62 | #endif /* #ifdef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD */ |
| 63 | #define STK_INT_PS_MODE 1 /* 1, 2, or 3 */ |
| 64 | #define STK_POLL_PS |
| 65 | #define STK_POLL_ALS /* ALS interrupt is valid only when STK_PS_INT_MODE = 1 or 4*/ |
| 66 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 67 | /* Define Register Map */ |
| 68 | #define STK_STATE_REG 0x00 |
| 69 | #define STK_PSCTRL_REG 0x01 |
| 70 | #define STK_ALSCTRL_REG 0x02 |
| 71 | #define STK_LEDCTRL_REG 0x03 |
| 72 | #define STK_INT_REG 0x04 |
| 73 | #define STK_WAIT_REG 0x05 |
| 74 | #define STK_THDH1_PS_REG 0x06 |
| 75 | #define STK_THDH2_PS_REG 0x07 |
| 76 | #define STK_THDL1_PS_REG 0x08 |
| 77 | #define STK_THDL2_PS_REG 0x09 |
| 78 | #define STK_THDH1_ALS_REG 0x0A |
| 79 | #define STK_THDH2_ALS_REG 0x0B |
| 80 | #define STK_THDL1_ALS_REG 0x0C |
| 81 | #define STK_THDL2_ALS_REG 0x0D |
| 82 | #define STK_FLAG_REG 0x10 |
| 83 | #define STK_DATA1_PS_REG 0x11 |
| 84 | #define STK_DATA2_PS_REG 0x12 |
| 85 | #define STK_DATA1_ALS_REG 0x13 |
| 86 | #define STK_DATA2_ALS_REG 0x14 |
| 87 | #define STK_DATA1_OFFSET_REG 0x15 |
| 88 | #define STK_DATA2_OFFSET_REG 0x16 |
| 89 | #define STK_DATA1_IR_REG 0x17 |
| 90 | #define STK_DATA2_IR_REG 0x18 |
| 91 | #define STK_PDT_ID_REG 0x3E |
| 92 | #define STK_RSRVD_REG 0x3F |
| 93 | #define STK_SW_RESET_REG 0x80 |
| 94 | |
| 95 | |
| 96 | /* Define state reg */ |
| 97 | #define STK_STATE_EN_IRS_SHIFT 7 |
| 98 | #define STK_STATE_EN_AK_SHIFT 6 |
| 99 | #define STK_STATE_EN_ASO_SHIFT 5 |
| 100 | #define STK_STATE_EN_IRO_SHIFT 4 |
| 101 | #define STK_STATE_EN_WAIT_SHIFT 2 |
| 102 | #define STK_STATE_EN_ALS_SHIFT 1 |
| 103 | #define STK_STATE_EN_PS_SHIFT 0 |
| 104 | |
| 105 | #define STK_STATE_EN_IRS_MASK 0x80 |
| 106 | #define STK_STATE_EN_AK_MASK 0x40 |
| 107 | #define STK_STATE_EN_ASO_MASK 0x20 |
| 108 | #define STK_STATE_EN_IRO_MASK 0x10 |
| 109 | #define STK_STATE_EN_WAIT_MASK 0x04 |
| 110 | #define STK_STATE_EN_ALS_MASK 0x02 |
| 111 | #define STK_STATE_EN_PS_MASK 0x01 |
| 112 | |
| 113 | /* Define PS ctrl reg */ |
| 114 | #define STK_PS_PRS_SHIFT 6 |
| 115 | #define STK_PS_GAIN_SHIFT 4 |
| 116 | #define STK_PS_IT_SHIFT 0 |
| 117 | |
| 118 | #define STK_PS_PRS_MASK 0xC0 |
| 119 | #define STK_PS_GAIN_MASK 0x30 |
| 120 | #define STK_PS_IT_MASK 0x0F |
| 121 | |
| 122 | /* Define ALS ctrl reg */ |
| 123 | #define STK_ALS_PRS_SHIFT 6 |
| 124 | #define STK_ALS_GAIN_SHIFT 4 |
| 125 | #define STK_ALS_IT_SHIFT 0 |
| 126 | |
| 127 | #define STK_ALS_PRS_MASK 0xC0 |
| 128 | #define STK_ALS_GAIN_MASK 0x30 |
| 129 | #define STK_ALS_IT_MASK 0x0F |
| 130 | |
| 131 | /* Define LED ctrl reg */ |
| 132 | #define STK_LED_IRDR_SHIFT 6 |
| 133 | #define STK_LED_DT_SHIFT 0 |
| 134 | |
| 135 | #define STK_LED_IRDR_MASK 0xC0 |
| 136 | #define STK_LED_DT_MASK 0x3F |
| 137 | |
| 138 | /* Define interrupt reg */ |
| 139 | #define STK_INT_CTRL_SHIFT 7 |
| 140 | #define STK_INT_OUI_SHIFT 4 |
| 141 | #define STK_INT_ALS_SHIFT 3 |
| 142 | #define STK_INT_PS_SHIFT 0 |
| 143 | |
| 144 | #define STK_INT_CTRL_MASK 0x80 |
| 145 | #define STK_INT_OUI_MASK 0x10 |
| 146 | #define STK_INT_ALS_MASK 0x08 |
| 147 | #define STK_INT_PS_MASK 0x07 |
| 148 | |
| 149 | #define STK_INT_ALS 0x08 |
| 150 | |
| 151 | /* Define flag reg */ |
| 152 | #define STK_FLG_ALSDR_SHIFT 7 |
| 153 | #define STK_FLG_PSDR_SHIFT 6 |
| 154 | #define STK_FLG_ALSINT_SHIFT 5 |
| 155 | #define STK_FLG_PSINT_SHIFT 4 |
| 156 | #define STK_FLG_OUI_SHIFT 2 |
| 157 | #define STK_FLG_IR_RDY_SHIFT 1 |
| 158 | #define STK_FLG_NF_SHIFT 0 |
| 159 | |
| 160 | #define STK_FLG_ALSDR_MASK 0x80 |
| 161 | #define STK_FLG_PSDR_MASK 0x40 |
| 162 | #define STK_FLG_ALSINT_MASK 0x20 |
| 163 | #define STK_FLG_PSINT_MASK 0x10 |
| 164 | #define STK_FLG_OUI_MASK 0x04 |
| 165 | #define STK_FLG_IR_RDY_MASK 0x02 |
| 166 | #define STK_FLG_NF_MASK 0x01 |
| 167 | |
| 168 | /* misc define */ |
| 169 | #define MIN_ALS_POLL_DELAY_NS 110000000 |
| 170 | |
| 171 | #define DEVICE_NAME "stk_ps" |
Bingzhe Cai | 8e6792f | 2013-08-26 13:40:56 +0800 | [diff] [blame] | 172 | #define ALS_NAME "stk3x1x-ls" |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 173 | #define PS_NAME "proximity" |
| 174 | |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 175 | /* POWER SUPPLY VOLTAGE RANGE */ |
| 176 | #define STK3X1X_VDD_MIN_UV 2000000 |
| 177 | #define STK3X1X_VDD_MAX_UV 3300000 |
| 178 | #define STK3X1X_VIO_MIN_UV 1750000 |
| 179 | #define STK3X1X_VIO_MAX_UV 1950000 |
| 180 | |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 181 | #define STK_FIR_LEN 16 |
| 182 | #define MAX_FIR_LEN 32 |
Jie Cheng | 246a936 | 2013-12-06 13:06:31 +0800 | [diff] [blame] | 183 | |
| 184 | static struct sensors_classdev sensors_light_cdev = { |
| 185 | .name = "stk3x1x-light", |
| 186 | .vendor = "Sensortek", |
| 187 | .version = 1, |
| 188 | .handle = SENSORS_LIGHT_HANDLE, |
| 189 | .type = SENSOR_TYPE_LIGHT, |
| 190 | .max_range = "6500", |
| 191 | .resolution = "0.0625", |
| 192 | .sensor_power = "0.09", |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 193 | .min_delay = (MIN_ALS_POLL_DELAY_NS / 1000), /* us */ |
Jie Cheng | 246a936 | 2013-12-06 13:06:31 +0800 | [diff] [blame] | 194 | .fifo_reserved_event_count = 0, |
| 195 | .fifo_max_event_count = 0, |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 196 | .enabled = 0, |
| 197 | .delay_msec = 200, |
| 198 | .sensors_enable = NULL, |
| 199 | .sensors_poll_delay = NULL, |
Jie Cheng | 246a936 | 2013-12-06 13:06:31 +0800 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | static struct sensors_classdev sensors_proximity_cdev = { |
| 203 | .name = "stk3x1x-proximity", |
| 204 | .vendor = "Sensortek", |
| 205 | .version = 1, |
| 206 | .handle = SENSORS_PROXIMITY_HANDLE, |
| 207 | .type = SENSOR_TYPE_PROXIMITY, |
| 208 | .max_range = "5.0", |
| 209 | .resolution = "5.0", |
| 210 | .sensor_power = "0.1", |
| 211 | .min_delay = 0, |
| 212 | .fifo_reserved_event_count = 0, |
| 213 | .fifo_max_event_count = 0, |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 214 | .enabled = 0, |
| 215 | .delay_msec = 200, |
| 216 | .sensors_enable = NULL, |
| 217 | .sensors_poll_delay = NULL, |
Jie Cheng | 246a936 | 2013-12-06 13:06:31 +0800 | [diff] [blame] | 218 | }; |
| 219 | |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 220 | struct data_filter { |
| 221 | u16 raw[MAX_FIR_LEN]; |
| 222 | int sum; |
| 223 | int number; |
| 224 | int idx; |
| 225 | }; |
| 226 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 227 | struct stk3x1x_data { |
| 228 | struct i2c_client *client; |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 229 | struct stk3x1x_platform_data *pdata; |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 230 | struct sensors_classdev als_cdev; |
| 231 | struct sensors_classdev ps_cdev; |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 232 | #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS)) |
| 233 | int32_t irq; |
| 234 | struct work_struct stk_work; |
| 235 | struct workqueue_struct *stk_wq; |
| 236 | #endif |
| 237 | int int_pin; |
| 238 | uint8_t wait_reg; |
| 239 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 240 | struct early_suspend stk_early_suspend; |
| 241 | #endif |
| 242 | uint16_t ps_thd_h; |
| 243 | uint16_t ps_thd_l; |
| 244 | struct mutex io_lock; |
| 245 | struct input_dev *ps_input_dev; |
| 246 | int32_t ps_distance_last; |
| 247 | bool ps_enabled; |
| 248 | struct wake_lock ps_wakelock; |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 249 | struct work_struct stk_ps_work; |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 250 | struct workqueue_struct *stk_ps_wq; |
| 251 | #ifdef STK_POLL_PS |
| 252 | struct wake_lock ps_nosuspend_wl; |
| 253 | #endif |
| 254 | struct input_dev *als_input_dev; |
| 255 | int32_t als_lux_last; |
| 256 | uint32_t als_transmittance; |
| 257 | bool als_enabled; |
| 258 | struct hrtimer als_timer; |
| 259 | struct hrtimer ps_timer; |
| 260 | ktime_t als_poll_delay; |
| 261 | ktime_t ps_poll_delay; |
| 262 | #ifdef STK_POLL_ALS |
| 263 | struct work_struct stk_als_work; |
| 264 | struct workqueue_struct *stk_als_wq; |
| 265 | #endif |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 266 | struct regulator *vdd; |
| 267 | struct regulator *vio; |
| 268 | bool power_enabled; |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 269 | bool use_fir; |
| 270 | struct data_filter fir; |
| 271 | atomic_t firlength; |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | #if( !defined(CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD)) |
| 275 | static uint32_t lux_threshold_table[] = |
| 276 | { |
| 277 | 3, |
| 278 | 10, |
| 279 | 40, |
| 280 | 65, |
| 281 | 145, |
| 282 | 300, |
| 283 | 550, |
| 284 | 930, |
| 285 | 1250, |
| 286 | 1700, |
| 287 | }; |
| 288 | |
| 289 | #define LUX_THD_TABLE_SIZE (sizeof(lux_threshold_table)/sizeof(uint32_t)+1) |
| 290 | static uint16_t code_threshold_table[LUX_THD_TABLE_SIZE+1]; |
| 291 | #endif |
| 292 | |
| 293 | static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable); |
| 294 | static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable); |
| 295 | static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l); |
| 296 | static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h); |
| 297 | static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l); |
| 298 | static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h); |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 299 | static int stk3x1x_device_ctl(struct stk3x1x_data *ps_data, bool enable); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 300 | //static int32_t stk3x1x_set_ps_aoffset(struct stk3x1x_data *ps_data, uint16_t offset); |
| 301 | |
| 302 | inline uint32_t stk_alscode2lux(struct stk3x1x_data *ps_data, uint32_t alscode) |
| 303 | { |
| 304 | alscode += ((alscode<<7)+(alscode<<3)+(alscode>>1)); |
| 305 | alscode<<=3; |
| 306 | alscode/=ps_data->als_transmittance; |
| 307 | return alscode; |
| 308 | } |
| 309 | |
| 310 | inline uint32_t stk_lux2alscode(struct stk3x1x_data *ps_data, uint32_t lux) |
| 311 | { |
| 312 | lux*=ps_data->als_transmittance; |
| 313 | lux/=1100; |
| 314 | if (unlikely(lux>=(1<<16))) |
| 315 | lux = (1<<16) -1; |
| 316 | return lux; |
| 317 | } |
| 318 | |
| 319 | #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD |
| 320 | static void stk_init_code_threshold_table(struct stk3x1x_data *ps_data) |
| 321 | { |
| 322 | uint32_t i,j; |
| 323 | uint32_t alscode; |
| 324 | |
| 325 | code_threshold_table[0] = 0; |
| 326 | #ifdef STK_DEBUG_PRINTF |
| 327 | printk(KERN_INFO "alscode[0]=%d\n",0); |
| 328 | #endif |
| 329 | for (i=1,j=0;i<LUX_THD_TABLE_SIZE;i++,j++) |
| 330 | { |
| 331 | alscode = stk_lux2alscode(ps_data, lux_threshold_table[j]); |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 332 | dev_dbg(&ps_data->client->dev, "alscode[%d]=%d\n", i, alscode); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 333 | code_threshold_table[i] = (uint16_t)(alscode); |
| 334 | } |
| 335 | code_threshold_table[i] = 0xffff; |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 336 | dev_dbg(&ps_data->client->dev, "alscode[%d]=%d\n", i, alscode); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static uint32_t stk_get_lux_interval_index(uint16_t alscode) |
| 340 | { |
| 341 | uint32_t i; |
| 342 | for (i=1;i<=LUX_THD_TABLE_SIZE;i++) |
| 343 | { |
| 344 | if ((alscode>=code_threshold_table[i-1])&&(alscode<code_threshold_table[i])) |
| 345 | { |
| 346 | return i; |
| 347 | } |
| 348 | } |
| 349 | return LUX_THD_TABLE_SIZE; |
| 350 | } |
| 351 | #else |
| 352 | inline void stk_als_set_new_thd(struct stk3x1x_data *ps_data, uint16_t alscode) |
| 353 | { |
| 354 | int32_t high_thd,low_thd; |
| 355 | high_thd = alscode + stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD); |
| 356 | low_thd = alscode - stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD); |
| 357 | if (high_thd >= (1<<16)) |
| 358 | high_thd = (1<<16) -1; |
| 359 | if (low_thd <0) |
| 360 | low_thd = 0; |
| 361 | stk3x1x_set_als_thd_h(ps_data, (uint16_t)high_thd); |
| 362 | stk3x1x_set_als_thd_l(ps_data, (uint16_t)low_thd); |
| 363 | } |
| 364 | #endif // CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD |
| 365 | |
| 366 | |
| 367 | static int32_t stk3x1x_init_all_reg(struct stk3x1x_data *ps_data, struct stk3x1x_platform_data *plat_data) |
| 368 | { |
| 369 | int32_t ret; |
| 370 | uint8_t w_reg; |
| 371 | |
| 372 | w_reg = plat_data->state_reg; |
| 373 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_reg); |
| 374 | if (ret < 0) |
| 375 | { |
| 376 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | ps_data->ps_thd_h = plat_data->ps_thd_h; |
| 381 | ps_data->ps_thd_l = plat_data->ps_thd_l; |
| 382 | |
| 383 | w_reg = plat_data->psctrl_reg; |
| 384 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_PSCTRL_REG, w_reg); |
| 385 | if (ret < 0) |
| 386 | { |
| 387 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 388 | return ret; |
| 389 | } |
| 390 | w_reg = plat_data->alsctrl_reg; |
| 391 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_ALSCTRL_REG, w_reg); |
| 392 | if (ret < 0) |
| 393 | { |
| 394 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 395 | return ret; |
| 396 | } |
| 397 | w_reg = plat_data->ledctrl_reg; |
| 398 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_LEDCTRL_REG, w_reg); |
| 399 | if (ret < 0) |
| 400 | { |
| 401 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 402 | return ret; |
| 403 | } |
| 404 | ps_data->wait_reg = plat_data->wait_reg; |
| 405 | |
| 406 | if(ps_data->wait_reg < 2) |
| 407 | { |
| 408 | printk(KERN_WARNING "%s: wait_reg should be larger than 2, force to write 2\n", __func__); |
| 409 | ps_data->wait_reg = 2; |
| 410 | } |
| 411 | else if (ps_data->wait_reg > 0xFF) |
| 412 | { |
| 413 | printk(KERN_WARNING "%s: wait_reg should be less than 0xFF, force to write 0xFF\n", __func__); |
| 414 | ps_data->wait_reg = 0xFF; |
| 415 | } |
| 416 | w_reg = plat_data->wait_reg; |
| 417 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_WAIT_REG, w_reg); |
| 418 | if (ret < 0) |
| 419 | { |
| 420 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 421 | return ret; |
| 422 | } |
| 423 | stk3x1x_set_ps_thd_h(ps_data, ps_data->ps_thd_h); |
| 424 | stk3x1x_set_ps_thd_l(ps_data, ps_data->ps_thd_l); |
| 425 | |
| 426 | w_reg = 0; |
| 427 | #ifndef STK_POLL_PS |
| 428 | w_reg |= STK_INT_PS_MODE; |
| 429 | #else |
| 430 | w_reg |= 0x01; |
| 431 | #endif |
| 432 | |
| 433 | #if (!defined(STK_POLL_ALS) && (STK_INT_PS_MODE != 0x02) && (STK_INT_PS_MODE != 0x03)) |
| 434 | w_reg |= STK_INT_ALS; |
| 435 | #endif |
| 436 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_INT_REG, w_reg); |
| 437 | if (ret < 0) |
| 438 | { |
| 439 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 440 | return ret; |
| 441 | } |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 442 | |
| 443 | ret = i2c_smbus_write_byte_data(ps_data->client, 0x87, 0x60); |
| 444 | if (ret < 0) { |
| 445 | dev_err(&ps_data->client->dev, |
| 446 | "%s: write i2c error\n", __func__); |
| 447 | return ret; |
| 448 | } |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static int32_t stk3x1x_check_pid(struct stk3x1x_data *ps_data) |
| 453 | { |
| 454 | int32_t err1, err2; |
| 455 | |
| 456 | err1 = i2c_smbus_read_byte_data(ps_data->client,STK_PDT_ID_REG); |
| 457 | if (err1 < 0) |
| 458 | { |
| 459 | printk(KERN_ERR "%s: read i2c error, err=%d\n", __func__, err1); |
| 460 | return err1; |
| 461 | } |
| 462 | |
| 463 | err2 = i2c_smbus_read_byte_data(ps_data->client,STK_RSRVD_REG); |
| 464 | if (err2 < 0) |
| 465 | { |
| 466 | printk(KERN_ERR "%s: read i2c error, err=%d\n", __func__, err2); |
| 467 | return -1; |
| 468 | } |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 469 | if(err2 == 0xC0) |
| 470 | printk(KERN_INFO "%s: RID=0xC0!!!!!!!!!!!!!\n", __func__); |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | static int32_t stk3x1x_software_reset(struct stk3x1x_data *ps_data) |
| 477 | { |
| 478 | int32_t r; |
| 479 | uint8_t w_reg; |
| 480 | |
| 481 | w_reg = 0x7F; |
| 482 | r = i2c_smbus_write_byte_data(ps_data->client,STK_WAIT_REG,w_reg); |
| 483 | if (r<0) |
| 484 | { |
| 485 | printk(KERN_ERR "%s: software reset: write i2c error, ret=%d\n", __func__, r); |
| 486 | return r; |
| 487 | } |
| 488 | r = i2c_smbus_read_byte_data(ps_data->client,STK_WAIT_REG); |
| 489 | if (w_reg != r) |
| 490 | { |
| 491 | printk(KERN_ERR "%s: software reset: read-back value is not the same\n", __func__); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | r = i2c_smbus_write_byte_data(ps_data->client,STK_SW_RESET_REG,0); |
| 496 | if (r<0) |
| 497 | { |
| 498 | printk(KERN_ERR "%s: software reset: read error after reset\n", __func__); |
| 499 | return r; |
| 500 | } |
| 501 | msleep(1); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l) |
| 507 | { |
| 508 | uint8_t temp; |
| 509 | uint8_t* pSrc = (uint8_t*)&thd_l; |
| 510 | temp = *pSrc; |
| 511 | *pSrc = *(pSrc+1); |
| 512 | *(pSrc+1) = temp; |
| 513 | return i2c_smbus_write_word_data(ps_data->client,STK_THDL1_ALS_REG,thd_l); |
| 514 | } |
| 515 | static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h) |
| 516 | { |
| 517 | uint8_t temp; |
| 518 | uint8_t* pSrc = (uint8_t*)&thd_h; |
| 519 | temp = *pSrc; |
| 520 | *pSrc = *(pSrc+1); |
| 521 | *(pSrc+1) = temp; |
| 522 | return i2c_smbus_write_word_data(ps_data->client,STK_THDH1_ALS_REG,thd_h); |
| 523 | } |
| 524 | |
| 525 | static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l) |
| 526 | { |
| 527 | uint8_t temp; |
| 528 | uint8_t* pSrc = (uint8_t*)&thd_l; |
| 529 | |
| 530 | temp = *pSrc; |
| 531 | *pSrc = *(pSrc+1); |
| 532 | *(pSrc+1) = temp; |
| 533 | ps_data->ps_thd_l = thd_l; |
| 534 | return i2c_smbus_write_word_data(ps_data->client,STK_THDL1_PS_REG,thd_l); |
| 535 | } |
| 536 | |
| 537 | static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h) |
| 538 | { |
| 539 | uint8_t temp; |
| 540 | uint8_t* pSrc = (uint8_t*)&thd_h; |
| 541 | |
| 542 | temp = *pSrc; |
| 543 | *pSrc = *(pSrc+1); |
| 544 | *(pSrc+1) = temp; |
| 545 | ps_data->ps_thd_h = thd_h; |
| 546 | return i2c_smbus_write_word_data(ps_data->client,STK_THDH1_PS_REG,thd_h); |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | static int32_t stk3x1x_set_ps_foffset(struct stk3x1x_data *ps_data, uint16_t offset) |
| 551 | { |
| 552 | uint8_t temp; |
| 553 | uint8_t* pSrc = (uint8_t*)&offset; |
| 554 | temp = *pSrc; |
| 555 | *pSrc = *(pSrc+1); |
| 556 | *(pSrc+1) = temp; |
| 557 | return i2c_smbus_write_word_data(ps_data->client,STK_DATA1_OFFSET_REG,offset); |
| 558 | } |
| 559 | |
| 560 | static int32_t stk3x1x_set_ps_aoffset(struct stk3x1x_data *ps_data, uint16_t offset) |
| 561 | { |
| 562 | uint8_t temp; |
| 563 | uint8_t* pSrc = (uint8_t*)&offset; |
| 564 | int ret; |
| 565 | uint8_t w_state_reg; |
| 566 | uint8_t re_en; |
| 567 | |
| 568 | ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG); |
| 569 | if (ret < 0) |
| 570 | { |
| 571 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 572 | return ret; |
| 573 | } |
| 574 | re_en = (ret & STK_STATE_EN_AK_MASK) ? 1: 0; |
| 575 | if(re_en) |
| 576 | { |
| 577 | w_state_reg = (uint8_t)(ret & (~STK_STATE_EN_AK_MASK)); |
| 578 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg); |
| 579 | if (ret < 0) |
| 580 | { |
| 581 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 582 | return ret; |
| 583 | } |
| 584 | msleep(1); |
| 585 | } |
| 586 | temp = *pSrc; |
| 587 | *pSrc = *(pSrc+1); |
| 588 | *(pSrc+1) = temp; |
| 589 | ret = i2c_smbus_write_word_data(ps_data->client,0x0E,offset); |
| 590 | if(!re_en) |
| 591 | return ret; |
| 592 | |
| 593 | w_state_reg |= STK_STATE_EN_AK_MASK; |
| 594 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg); |
| 595 | if (ret < 0) |
| 596 | { |
| 597 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | */ |
| 604 | |
| 605 | static inline uint32_t stk3x1x_get_ps_reading(struct stk3x1x_data *ps_data) |
| 606 | { |
| 607 | int32_t word_data, tmp_word_data; |
| 608 | |
| 609 | tmp_word_data = i2c_smbus_read_word_data(ps_data->client,STK_DATA1_PS_REG); |
| 610 | if(tmp_word_data < 0) |
| 611 | { |
| 612 | printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data); |
| 613 | return tmp_word_data; |
| 614 | } |
| 615 | word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ; |
| 616 | return word_data; |
| 617 | } |
| 618 | |
| 619 | static int32_t stk3x1x_set_flag(struct stk3x1x_data *ps_data, uint8_t org_flag_reg, uint8_t clr) |
| 620 | { |
| 621 | uint8_t w_flag; |
| 622 | w_flag = org_flag_reg | (STK_FLG_ALSINT_MASK | STK_FLG_PSINT_MASK | STK_FLG_OUI_MASK | STK_FLG_IR_RDY_MASK); |
| 623 | w_flag &= (~clr); |
| 624 | //printk(KERN_INFO "%s: org_flag_reg=0x%x, w_flag = 0x%x\n", __func__, org_flag_reg, w_flag); |
| 625 | return i2c_smbus_write_byte_data(ps_data->client,STK_FLAG_REG, w_flag); |
| 626 | } |
| 627 | |
| 628 | static int32_t stk3x1x_get_flag(struct stk3x1x_data *ps_data) |
| 629 | { |
| 630 | return i2c_smbus_read_byte_data(ps_data->client,STK_FLAG_REG); |
| 631 | } |
| 632 | |
| 633 | static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable) |
| 634 | { |
| 635 | int32_t ret; |
| 636 | uint8_t w_state_reg; |
| 637 | uint8_t curr_ps_enable; |
| 638 | curr_ps_enable = ps_data->ps_enabled?1:0; |
| 639 | if(curr_ps_enable == enable) |
| 640 | return 0; |
| 641 | |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 642 | if (enable) { |
| 643 | ret = stk3x1x_device_ctl(ps_data, enable); |
| 644 | if (ret) |
| 645 | return ret; |
| 646 | } |
| 647 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 648 | ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG); |
| 649 | if (ret < 0) |
| 650 | { |
| 651 | printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret); |
| 652 | return ret; |
| 653 | } |
| 654 | w_state_reg = ret; |
| 655 | w_state_reg &= ~(STK_STATE_EN_PS_MASK | STK_STATE_EN_WAIT_MASK | 0x60); |
| 656 | if(enable) |
| 657 | { |
| 658 | w_state_reg |= STK_STATE_EN_PS_MASK; |
| 659 | if(!(ps_data->als_enabled)) |
| 660 | w_state_reg |= STK_STATE_EN_WAIT_MASK; |
| 661 | } |
| 662 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg); |
| 663 | if (ret < 0) |
| 664 | { |
| 665 | printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret); |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | if(enable) |
| 670 | { |
| 671 | #ifdef STK_POLL_PS |
| 672 | hrtimer_start(&ps_data->ps_timer, ps_data->ps_poll_delay, HRTIMER_MODE_REL); |
| 673 | ps_data->ps_distance_last = -1; |
| 674 | #endif |
| 675 | ps_data->ps_enabled = true; |
| 676 | #ifndef STK_POLL_PS |
| 677 | #ifndef STK_POLL_ALS |
| 678 | if(!(ps_data->als_enabled)) |
| 679 | #endif /* #ifndef STK_POLL_ALS */ |
| 680 | enable_irq(ps_data->irq); |
| 681 | msleep(1); |
| 682 | ret = stk3x1x_get_flag(ps_data); |
| 683 | if (ret < 0) |
| 684 | { |
| 685 | printk(KERN_ERR "%s: read i2c error, ret=%d\n", __func__, ret); |
| 686 | return ret; |
| 687 | } |
| 688 | |
| 689 | near_far_state = ret & STK_FLG_NF_MASK; |
| 690 | ps_data->ps_distance_last = near_far_state; |
| 691 | input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state); |
| 692 | input_sync(ps_data->ps_input_dev); |
| 693 | wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ); |
| 694 | reading = stk3x1x_get_ps_reading(ps_data); |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 695 | dev_dbg(&ps_data->client->dev, |
| 696 | "%s: ps input event=%d, ps code = %d\n", |
| 697 | __func__, near_far_state, reading); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 698 | #endif /* #ifndef STK_POLL_PS */ |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | #ifdef STK_POLL_PS |
| 703 | hrtimer_cancel(&ps_data->ps_timer); |
| 704 | #else |
| 705 | #ifndef STK_POLL_ALS |
| 706 | if(!(ps_data->als_enabled)) |
| 707 | #endif |
| 708 | disable_irq(ps_data->irq); |
| 709 | #endif |
| 710 | ps_data->ps_enabled = false; |
| 711 | } |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 712 | if (!enable) { |
| 713 | ret = stk3x1x_device_ctl(ps_data, enable); |
| 714 | if (ret) |
| 715 | return ret; |
| 716 | } |
| 717 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable) |
| 722 | { |
| 723 | int32_t ret; |
| 724 | uint8_t w_state_reg; |
| 725 | uint8_t curr_als_enable = (ps_data->als_enabled)?1:0; |
| 726 | |
| 727 | if(curr_als_enable == enable) |
| 728 | return 0; |
| 729 | |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 730 | if (enable) { |
| 731 | ret = stk3x1x_device_ctl(ps_data, enable); |
| 732 | if (ret) |
| 733 | return ret; |
| 734 | } |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 735 | #ifndef STK_POLL_ALS |
| 736 | if (enable) |
| 737 | { |
| 738 | stk3x1x_set_als_thd_h(ps_data, 0x0000); |
| 739 | stk3x1x_set_als_thd_l(ps_data, 0xFFFF); |
| 740 | } |
| 741 | #endif |
| 742 | ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG); |
| 743 | if (ret < 0) |
| 744 | { |
| 745 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 746 | return ret; |
| 747 | } |
| 748 | w_state_reg = (uint8_t)(ret & (~(STK_STATE_EN_ALS_MASK | STK_STATE_EN_WAIT_MASK))); |
| 749 | if(enable) |
| 750 | w_state_reg |= STK_STATE_EN_ALS_MASK; |
| 751 | else if (ps_data->ps_enabled) |
| 752 | w_state_reg |= STK_STATE_EN_WAIT_MASK; |
| 753 | |
| 754 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg); |
| 755 | if (ret < 0) |
| 756 | { |
| 757 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | if (enable) |
| 762 | { |
| 763 | ps_data->als_enabled = true; |
| 764 | #ifdef STK_POLL_ALS |
| 765 | hrtimer_start(&ps_data->als_timer, ps_data->als_poll_delay, HRTIMER_MODE_REL); |
| 766 | #else |
| 767 | #ifndef STK_POLL_PS |
| 768 | if(!(ps_data->ps_enabled)) |
| 769 | #endif |
| 770 | enable_irq(ps_data->irq); |
| 771 | #endif |
| 772 | } |
| 773 | else |
| 774 | { |
| 775 | ps_data->als_enabled = false; |
| 776 | #ifdef STK_POLL_ALS |
| 777 | hrtimer_cancel(&ps_data->als_timer); |
| 778 | #else |
| 779 | #ifndef STK_POLL_PS |
| 780 | if(!(ps_data->ps_enabled)) |
| 781 | #endif |
| 782 | disable_irq(ps_data->irq); |
| 783 | #endif |
| 784 | } |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 785 | if (!enable) { |
| 786 | ret = stk3x1x_device_ctl(ps_data, enable); |
| 787 | if (ret) |
| 788 | return ret; |
| 789 | } |
| 790 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 791 | return ret; |
| 792 | } |
| 793 | |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 794 | static inline int32_t stk3x1x_filter_reading(struct stk3x1x_data *ps_data, |
| 795 | int32_t word_data) |
| 796 | { |
| 797 | int index; |
| 798 | int firlen = atomic_read(&ps_data->firlength); |
| 799 | |
| 800 | if (ps_data->fir.number < firlen) { |
| 801 | ps_data->fir.raw[ps_data->fir.number] = word_data; |
| 802 | ps_data->fir.sum += word_data; |
| 803 | ps_data->fir.number++; |
| 804 | ps_data->fir.idx++; |
| 805 | } else { |
| 806 | index = ps_data->fir.idx % firlen; |
| 807 | ps_data->fir.sum -= ps_data->fir.raw[index]; |
| 808 | ps_data->fir.raw[index] = word_data; |
| 809 | ps_data->fir.sum += word_data; |
| 810 | ps_data->fir.idx++; |
| 811 | word_data = ps_data->fir.sum/firlen; |
| 812 | } |
| 813 | return word_data; |
| 814 | } |
| 815 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 816 | static inline int32_t stk3x1x_get_als_reading(struct stk3x1x_data *ps_data) |
| 817 | { |
| 818 | int32_t word_data, tmp_word_data; |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 819 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 820 | tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_ALS_REG); |
| 821 | if(tmp_word_data < 0) |
| 822 | { |
| 823 | printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data); |
| 824 | return tmp_word_data; |
| 825 | } |
| 826 | word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ; |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 827 | if (ps_data->use_fir) |
| 828 | word_data = stk3x1x_filter_reading(ps_data, word_data); |
| 829 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 830 | return word_data; |
| 831 | } |
| 832 | |
| 833 | static int32_t stk3x1x_get_ir_reading(struct stk3x1x_data *ps_data) |
| 834 | { |
| 835 | int32_t word_data, tmp_word_data; |
| 836 | int32_t ret; |
| 837 | uint8_t w_reg, retry = 0; |
| 838 | |
| 839 | if(ps_data->ps_enabled) |
| 840 | { |
| 841 | stk3x1x_enable_ps(ps_data, 0); |
| 842 | ps_data->ps_enabled = true; |
| 843 | } |
| 844 | ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG); |
| 845 | if (ret < 0) |
| 846 | { |
| 847 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 848 | return ret; |
| 849 | } |
| 850 | w_reg = (uint8_t)(ret & (~STK_STATE_EN_IRS_MASK)); |
| 851 | w_reg |= STK_STATE_EN_IRS_MASK; |
| 852 | |
| 853 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_reg); |
| 854 | if (ret < 0) |
| 855 | { |
| 856 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 857 | return ret; |
| 858 | } |
| 859 | msleep(100); |
| 860 | |
| 861 | do |
| 862 | { |
| 863 | msleep(50); |
| 864 | ret = stk3x1x_get_flag(ps_data); |
| 865 | if (ret < 0) |
| 866 | { |
| 867 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 868 | return ret; |
| 869 | } |
| 870 | retry++; |
| 871 | }while(retry < 5 && ((ret&STK_FLG_IR_RDY_MASK) == 0)); |
| 872 | |
| 873 | if(retry == 5) |
| 874 | { |
| 875 | printk(KERN_ERR "%s: ir data is not ready for 300ms\n", __func__); |
| 876 | return -EINVAL; |
| 877 | } |
| 878 | |
| 879 | ret = stk3x1x_get_flag(ps_data); |
| 880 | if (ret < 0) |
| 881 | { |
| 882 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 883 | return ret; |
| 884 | } |
| 885 | |
| 886 | ret = stk3x1x_set_flag(ps_data, ret, STK_FLG_IR_RDY_MASK); |
| 887 | if (ret < 0) |
| 888 | { |
| 889 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 890 | return ret; |
| 891 | } |
| 892 | |
| 893 | tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_IR_REG); |
| 894 | if(tmp_word_data < 0) |
| 895 | { |
| 896 | printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data); |
| 897 | return tmp_word_data; |
| 898 | } |
| 899 | word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ; |
| 900 | |
| 901 | if(ps_data->ps_enabled) |
| 902 | stk3x1x_enable_ps(ps_data, 1); |
| 903 | return word_data; |
| 904 | } |
| 905 | |
| 906 | |
| 907 | static ssize_t stk_als_code_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 908 | { |
| 909 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 910 | int32_t reading; |
| 911 | |
| 912 | reading = stk3x1x_get_als_reading(ps_data); |
| 913 | return scnprintf(buf, PAGE_SIZE, "%d\n", reading); |
| 914 | } |
| 915 | |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 916 | static ssize_t stk_als_enable_set(struct sensors_classdev *sensors_cdev, |
| 917 | unsigned int enabled) |
| 918 | { |
| 919 | struct stk3x1x_data *als_data = container_of(sensors_cdev, |
| 920 | struct stk3x1x_data, als_cdev); |
| 921 | int err; |
| 922 | |
| 923 | mutex_lock(&als_data->io_lock); |
| 924 | err = stk3x1x_enable_als(als_data, enabled); |
| 925 | mutex_unlock(&als_data->io_lock); |
| 926 | |
| 927 | if (err < 0) |
| 928 | return err; |
| 929 | return 0; |
| 930 | } |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 931 | |
| 932 | static ssize_t stk_als_enable_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 933 | { |
| 934 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 935 | int32_t enable, ret; |
| 936 | |
| 937 | mutex_lock(&ps_data->io_lock); |
| 938 | enable = (ps_data->als_enabled)?1:0; |
| 939 | mutex_unlock(&ps_data->io_lock); |
| 940 | ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG); |
| 941 | ret = (ret & STK_STATE_EN_ALS_MASK)?1:0; |
| 942 | |
| 943 | if(enable != ret) |
| 944 | printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret); |
| 945 | |
| 946 | return scnprintf(buf, PAGE_SIZE, "%d\n", ret); |
| 947 | } |
| 948 | |
| 949 | static ssize_t stk_als_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 950 | { |
| 951 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 952 | uint8_t en; |
| 953 | if (sysfs_streq(buf, "1")) |
| 954 | en = 1; |
| 955 | else if (sysfs_streq(buf, "0")) |
| 956 | en = 0; |
| 957 | else |
| 958 | { |
| 959 | printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf); |
| 960 | return -EINVAL; |
| 961 | } |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 962 | dev_dbg(dev, "%s: Enable ALS : %d\n", __func__, en); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 963 | mutex_lock(&ps_data->io_lock); |
| 964 | stk3x1x_enable_als(ps_data, en); |
| 965 | mutex_unlock(&ps_data->io_lock); |
| 966 | return size; |
| 967 | } |
| 968 | |
| 969 | static ssize_t stk_als_lux_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 970 | { |
| 971 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 972 | int32_t als_reading; |
| 973 | uint32_t als_lux; |
| 974 | als_reading = stk3x1x_get_als_reading(ps_data); |
| 975 | mutex_lock(&ps_data->io_lock); |
| 976 | als_lux = stk_alscode2lux(ps_data, als_reading); |
| 977 | mutex_unlock(&ps_data->io_lock); |
| 978 | return scnprintf(buf, PAGE_SIZE, "%d lux\n", als_lux); |
| 979 | } |
| 980 | |
| 981 | static ssize_t stk_als_lux_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 982 | { |
| 983 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 984 | unsigned long value = 0; |
| 985 | int ret; |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 986 | ret = kstrtoul(buf, 16, &value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 987 | if(ret < 0) |
| 988 | { |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 989 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 990 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 991 | return ret; |
| 992 | } |
| 993 | mutex_lock(&ps_data->io_lock); |
| 994 | ps_data->als_lux_last = value; |
| 995 | input_report_abs(ps_data->als_input_dev, ABS_MISC, value); |
| 996 | input_sync(ps_data->als_input_dev); |
| 997 | mutex_unlock(&ps_data->io_lock); |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 998 | dev_dbg(dev, "%s: als input event %ld lux\n", __func__, value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 999 | |
| 1000 | return size; |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | static ssize_t stk_als_transmittance_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1005 | { |
| 1006 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1007 | int32_t transmittance; |
| 1008 | mutex_lock(&ps_data->io_lock); |
| 1009 | transmittance = ps_data->als_transmittance; |
| 1010 | mutex_unlock(&ps_data->io_lock); |
| 1011 | return scnprintf(buf, PAGE_SIZE, "%d\n", transmittance); |
| 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | static ssize_t stk_als_transmittance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1016 | { |
| 1017 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1018 | unsigned long value = 0; |
| 1019 | int ret; |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1020 | ret = kstrtoul(buf, 10, &value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1021 | if(ret < 0) |
| 1022 | { |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1023 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 1024 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1025 | return ret; |
| 1026 | } |
| 1027 | mutex_lock(&ps_data->io_lock); |
| 1028 | ps_data->als_transmittance = value; |
| 1029 | mutex_unlock(&ps_data->io_lock); |
| 1030 | return size; |
| 1031 | } |
| 1032 | |
| 1033 | static ssize_t stk_als_delay_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1034 | { |
| 1035 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 1036 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
| 1037 | (u32)ktime_to_ms(ps_data->als_poll_delay)); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 1040 | static inline void stk_als_delay_store_fir(struct stk3x1x_data *ps_data) |
| 1041 | { |
| 1042 | ps_data->fir.number = 0; |
| 1043 | ps_data->fir.idx = 0; |
| 1044 | ps_data->fir.sum = 0; |
| 1045 | } |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 1046 | |
| 1047 | static ssize_t stk_als_poll_delay_set(struct sensors_classdev *sensors_cdev, |
| 1048 | unsigned int delay_msec) |
| 1049 | { |
| 1050 | struct stk3x1x_data *als_data = container_of(sensors_cdev, |
| 1051 | struct stk3x1x_data, als_cdev); |
| 1052 | uint64_t value = 0; |
| 1053 | |
| 1054 | value = delay_msec * 1000000; |
| 1055 | |
| 1056 | if (value < MIN_ALS_POLL_DELAY_NS) |
| 1057 | value = MIN_ALS_POLL_DELAY_NS; |
| 1058 | |
| 1059 | mutex_lock(&als_data->io_lock); |
| 1060 | if (value != ktime_to_ns(als_data->als_poll_delay)) |
| 1061 | als_data->als_poll_delay = ns_to_ktime(value); |
| 1062 | |
| 1063 | if (als_data->use_fir) |
| 1064 | stk_als_delay_store_fir(als_data); |
| 1065 | |
| 1066 | mutex_unlock(&als_data->io_lock); |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1071 | static ssize_t stk_als_delay_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1072 | { |
| 1073 | uint64_t value = 0; |
| 1074 | int ret; |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 1075 | struct stk3x1x_data *als_data = dev_get_drvdata(dev); |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1076 | ret = kstrtoull(buf, 10, &value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1077 | if(ret < 0) |
| 1078 | { |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 1079 | dev_err(dev, "%s:kstrtoull failed, ret=0x%x\n", __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1080 | return ret; |
| 1081 | } |
| 1082 | #ifdef STK_DEBUG_PRINTF |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 1083 | dev_dbg(dev, "%s: set als poll delay=%lld\n", __func__, value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1084 | #endif |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 1085 | ret = stk_als_poll_delay_set(&als_data->als_cdev, value); |
| 1086 | if (ret < 0) |
| 1087 | return ret; |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1088 | return size; |
| 1089 | } |
| 1090 | |
| 1091 | static ssize_t stk_als_ir_code_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1092 | { |
| 1093 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1094 | int32_t reading; |
| 1095 | reading = stk3x1x_get_ir_reading(ps_data); |
| 1096 | return scnprintf(buf, PAGE_SIZE, "%d\n", reading); |
| 1097 | } |
| 1098 | |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 1099 | static ssize_t stk_als_firlen_show(struct device *dev, |
| 1100 | struct device_attribute *attr, char *buf) |
| 1101 | { |
| 1102 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1103 | int len = atomic_read(&ps_data->firlength); |
| 1104 | |
| 1105 | dev_dbg(dev, "%s: len = %2d, idx = %2d\n", |
| 1106 | __func__, len, ps_data->fir.idx); |
| 1107 | dev_dbg(dev, "%s: sum = %5d, ave = %5d\n", |
| 1108 | __func__, ps_data->fir.sum, ps_data->fir.sum/len); |
| 1109 | |
| 1110 | return scnprintf(buf, PAGE_SIZE, "%d\n", len); |
| 1111 | } |
| 1112 | |
| 1113 | static ssize_t stk_als_firlen_store(struct device *dev, |
| 1114 | struct device_attribute *attr, |
| 1115 | const char *buf, size_t size) |
| 1116 | { |
| 1117 | uint64_t value = 0; |
| 1118 | int ret; |
| 1119 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1120 | ret = kstrtoull(buf, 10, &value); |
| 1121 | if (ret < 0) { |
| 1122 | dev_err(dev, "%s:strict_strtoull failed, ret=0x%x\n", |
| 1123 | __func__, ret); |
| 1124 | return ret; |
| 1125 | } |
| 1126 | |
| 1127 | if (value > MAX_FIR_LEN) { |
| 1128 | dev_err(dev, "%s: firlen exceed maximum filter length\n", |
| 1129 | __func__); |
| 1130 | } else if (value < 1) { |
| 1131 | atomic_set(&ps_data->firlength, 1); |
| 1132 | memset(&ps_data->fir, 0x00, sizeof(ps_data->fir)); |
| 1133 | } else { |
| 1134 | atomic_set(&ps_data->firlength, value); |
| 1135 | memset(&ps_data->fir, 0x00, sizeof(ps_data->fir)); |
| 1136 | } |
| 1137 | return size; |
| 1138 | } |
| 1139 | |
| 1140 | static ssize_t stk_als_fir_enable_show(struct device *dev, |
| 1141 | struct device_attribute *attr, char *buf) |
| 1142 | { |
| 1143 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1144 | |
| 1145 | return scnprintf(buf, PAGE_SIZE, "%d\n", ps_data->use_fir); |
| 1146 | } |
| 1147 | |
| 1148 | static ssize_t stk_als_fir_enable_store(struct device *dev, |
| 1149 | struct device_attribute *attr, |
| 1150 | const char *buf, size_t size) |
| 1151 | { |
| 1152 | uint64_t value = 0; |
| 1153 | int ret; |
| 1154 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1155 | ret = kstrtoull(buf, 10, &value); |
| 1156 | if (ret < 0) { |
| 1157 | dev_err(dev, "%s:strict_strtoull failed, ret=0x%x\n", |
| 1158 | __func__, ret); |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
| 1162 | if (value) { |
| 1163 | ps_data->use_fir = true; |
| 1164 | memset(&ps_data->fir, 0x00, sizeof(ps_data->fir)); |
| 1165 | } else { |
| 1166 | ps_data->use_fir = false; |
| 1167 | } |
| 1168 | return size; |
| 1169 | } |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1170 | static ssize_t stk_ps_code_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1171 | { |
| 1172 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1173 | uint32_t reading; |
| 1174 | reading = stk3x1x_get_ps_reading(ps_data); |
| 1175 | return scnprintf(buf, PAGE_SIZE, "%d\n", reading); |
| 1176 | } |
| 1177 | |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 1178 | static ssize_t stk_ps_enable_set(struct sensors_classdev *sensors_cdev, |
| 1179 | unsigned int enabled) |
| 1180 | { |
| 1181 | struct stk3x1x_data *ps_data = container_of(sensors_cdev, |
| 1182 | struct stk3x1x_data, ps_cdev); |
| 1183 | int err; |
| 1184 | |
| 1185 | mutex_lock(&ps_data->io_lock); |
| 1186 | err = stk3x1x_enable_ps(ps_data, enabled); |
| 1187 | mutex_unlock(&ps_data->io_lock); |
| 1188 | |
| 1189 | if (err < 0) |
| 1190 | return err; |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1194 | static ssize_t stk_ps_enable_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1195 | { |
| 1196 | int32_t enable, ret; |
| 1197 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1198 | |
| 1199 | mutex_lock(&ps_data->io_lock); |
| 1200 | enable = (ps_data->ps_enabled)?1:0; |
| 1201 | mutex_unlock(&ps_data->io_lock); |
| 1202 | ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG); |
| 1203 | ret = (ret & STK_STATE_EN_PS_MASK)?1:0; |
| 1204 | |
| 1205 | if(enable != ret) |
| 1206 | printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret); |
| 1207 | |
| 1208 | return scnprintf(buf, PAGE_SIZE, "%d\n", ret); |
| 1209 | } |
| 1210 | |
| 1211 | static ssize_t stk_ps_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1212 | { |
| 1213 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1214 | uint8_t en; |
| 1215 | if (sysfs_streq(buf, "1")) |
| 1216 | en = 1; |
| 1217 | else if (sysfs_streq(buf, "0")) |
| 1218 | en = 0; |
| 1219 | else |
| 1220 | { |
| 1221 | printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf); |
| 1222 | return -EINVAL; |
| 1223 | } |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 1224 | dev_dbg(dev, "%s: Enable PS : %d\n", __func__, en); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1225 | mutex_lock(&ps_data->io_lock); |
| 1226 | stk3x1x_enable_ps(ps_data, en); |
| 1227 | mutex_unlock(&ps_data->io_lock); |
| 1228 | return size; |
| 1229 | } |
| 1230 | |
| 1231 | static ssize_t stk_ps_enable_aso_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1232 | { |
| 1233 | int32_t ret; |
| 1234 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1235 | |
| 1236 | ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG); |
| 1237 | ret = (ret & STK_STATE_EN_ASO_MASK)?1:0; |
| 1238 | |
| 1239 | return scnprintf(buf, PAGE_SIZE, "%d\n", ret); |
| 1240 | } |
| 1241 | |
| 1242 | static ssize_t stk_ps_enable_aso_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1243 | { |
| 1244 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1245 | uint8_t en; |
| 1246 | int32_t ret; |
| 1247 | uint8_t w_state_reg; |
| 1248 | |
| 1249 | if (sysfs_streq(buf, "1")) |
| 1250 | en = 1; |
| 1251 | else if (sysfs_streq(buf, "0")) |
| 1252 | en = 0; |
| 1253 | else |
| 1254 | { |
| 1255 | printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf); |
| 1256 | return -EINVAL; |
| 1257 | } |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 1258 | dev_dbg(dev, "%s: Enable PS ASO : %d\n", __func__, en); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1259 | |
| 1260 | ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG); |
| 1261 | if (ret < 0) |
| 1262 | { |
| 1263 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 1264 | return ret; |
| 1265 | } |
| 1266 | w_state_reg = (uint8_t)(ret & (~STK_STATE_EN_ASO_MASK)); |
| 1267 | if(en) |
| 1268 | w_state_reg |= STK_STATE_EN_ASO_MASK; |
| 1269 | |
| 1270 | ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg); |
| 1271 | if (ret < 0) |
| 1272 | { |
| 1273 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 1274 | return ret; |
| 1275 | } |
| 1276 | |
| 1277 | return size; |
| 1278 | } |
| 1279 | |
| 1280 | |
| 1281 | static ssize_t stk_ps_offset_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1282 | { |
| 1283 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1284 | int32_t word_data, tmp_word_data; |
| 1285 | |
| 1286 | tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_OFFSET_REG); |
| 1287 | if(tmp_word_data < 0) |
| 1288 | { |
| 1289 | printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data); |
| 1290 | return tmp_word_data; |
| 1291 | } |
| 1292 | word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ; |
| 1293 | return scnprintf(buf, PAGE_SIZE, "%d\n", word_data); |
| 1294 | } |
| 1295 | |
| 1296 | static ssize_t stk_ps_offset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1297 | { |
| 1298 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1299 | unsigned long value = 0; |
| 1300 | int ret; |
| 1301 | uint16_t offset; |
| 1302 | |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1303 | ret = kstrtoul(buf, 10, &value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1304 | if(ret < 0) |
| 1305 | { |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1306 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 1307 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1308 | return ret; |
| 1309 | } |
| 1310 | if(value > 65535) |
| 1311 | { |
| 1312 | printk(KERN_ERR "%s: invalid value, offset=%ld\n", __func__, value); |
| 1313 | return -EINVAL; |
| 1314 | } |
| 1315 | |
| 1316 | offset = (uint16_t) ((value&0x00FF) << 8) | ((value&0xFF00) >>8); |
| 1317 | ret = i2c_smbus_write_word_data(ps_data->client,STK_DATA1_OFFSET_REG,offset); |
| 1318 | if(ret < 0) |
| 1319 | { |
| 1320 | printk(KERN_ERR "%s: write i2c error\n", __func__); |
| 1321 | return ret; |
| 1322 | } |
| 1323 | return size; |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | static ssize_t stk_ps_distance_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1328 | { |
| 1329 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1330 | int32_t dist=1, ret; |
| 1331 | |
| 1332 | mutex_lock(&ps_data->io_lock); |
| 1333 | ret = stk3x1x_get_flag(ps_data); |
| 1334 | if(ret < 0) |
| 1335 | { |
| 1336 | printk(KERN_ERR "%s: stk3x1x_get_flag failed, ret=0x%x\n", __func__, ret); |
| 1337 | return ret; |
| 1338 | } |
| 1339 | dist = (ret & STK_FLG_NF_MASK)?1:0; |
| 1340 | |
| 1341 | ps_data->ps_distance_last = dist; |
| 1342 | input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, dist); |
| 1343 | input_sync(ps_data->ps_input_dev); |
| 1344 | mutex_unlock(&ps_data->io_lock); |
| 1345 | wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ); |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 1346 | dev_dbg(dev, "%s: ps input event %d cm\n", __func__, dist); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1347 | return scnprintf(buf, PAGE_SIZE, "%d\n", dist); |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | static ssize_t stk_ps_distance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1352 | { |
| 1353 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1354 | unsigned long value = 0; |
| 1355 | int ret; |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1356 | ret = kstrtoul(buf, 10, &value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1357 | if(ret < 0) |
| 1358 | { |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1359 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 1360 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1361 | return ret; |
| 1362 | } |
| 1363 | mutex_lock(&ps_data->io_lock); |
| 1364 | ps_data->ps_distance_last = value; |
| 1365 | input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, value); |
| 1366 | input_sync(ps_data->ps_input_dev); |
| 1367 | mutex_unlock(&ps_data->io_lock); |
| 1368 | wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ); |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 1369 | dev_dbg(dev, "%s: ps input event %ld cm\n", __func__, value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1370 | return size; |
| 1371 | } |
| 1372 | |
| 1373 | |
| 1374 | static ssize_t stk_ps_code_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1375 | { |
| 1376 | int32_t ps_thd_l1_reg, ps_thd_l2_reg; |
| 1377 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1378 | mutex_lock(&ps_data->io_lock); |
| 1379 | ps_thd_l1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL1_PS_REG); |
| 1380 | if(ps_thd_l1_reg < 0) |
| 1381 | { |
| 1382 | printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l1_reg); |
| 1383 | return -EINVAL; |
| 1384 | } |
| 1385 | ps_thd_l2_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL2_PS_REG); |
| 1386 | if(ps_thd_l2_reg < 0) |
| 1387 | { |
| 1388 | printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l2_reg); |
| 1389 | return -EINVAL; |
| 1390 | } |
| 1391 | mutex_unlock(&ps_data->io_lock); |
| 1392 | ps_thd_l1_reg = ps_thd_l1_reg<<8 | ps_thd_l2_reg; |
| 1393 | return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_l1_reg); |
| 1394 | } |
| 1395 | |
| 1396 | |
| 1397 | static ssize_t stk_ps_code_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1398 | { |
| 1399 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1400 | unsigned long value = 0; |
| 1401 | int ret; |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1402 | ret = kstrtoul(buf, 10, &value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1403 | if(ret < 0) |
| 1404 | { |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1405 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 1406 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1407 | return ret; |
| 1408 | } |
| 1409 | mutex_lock(&ps_data->io_lock); |
| 1410 | stk3x1x_set_ps_thd_l(ps_data, value); |
| 1411 | mutex_unlock(&ps_data->io_lock); |
| 1412 | return size; |
| 1413 | } |
| 1414 | |
| 1415 | static ssize_t stk_ps_code_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1416 | { |
| 1417 | int32_t ps_thd_h1_reg, ps_thd_h2_reg; |
| 1418 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1419 | mutex_lock(&ps_data->io_lock); |
| 1420 | ps_thd_h1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH1_PS_REG); |
| 1421 | if(ps_thd_h1_reg < 0) |
| 1422 | { |
| 1423 | printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h1_reg); |
| 1424 | return -EINVAL; |
| 1425 | } |
| 1426 | ps_thd_h2_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH2_PS_REG); |
| 1427 | if(ps_thd_h2_reg < 0) |
| 1428 | { |
| 1429 | printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h2_reg); |
| 1430 | return -EINVAL; |
| 1431 | } |
| 1432 | mutex_unlock(&ps_data->io_lock); |
| 1433 | ps_thd_h1_reg = ps_thd_h1_reg<<8 | ps_thd_h2_reg; |
| 1434 | return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_h1_reg); |
| 1435 | } |
| 1436 | |
| 1437 | |
| 1438 | static ssize_t stk_ps_code_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1439 | { |
| 1440 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1441 | unsigned long value = 0; |
| 1442 | int ret; |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1443 | ret = kstrtoul(buf, 10, &value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1444 | if(ret < 0) |
| 1445 | { |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1446 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 1447 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1448 | return ret; |
| 1449 | } |
| 1450 | mutex_lock(&ps_data->io_lock); |
| 1451 | stk3x1x_set_ps_thd_h(ps_data, value); |
| 1452 | mutex_unlock(&ps_data->io_lock); |
| 1453 | return size; |
| 1454 | } |
| 1455 | |
| 1456 | #if 0 |
| 1457 | static ssize_t stk_als_lux_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1458 | { |
| 1459 | int32_t als_thd_l0_reg,als_thd_l1_reg; |
| 1460 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1461 | uint32_t als_lux; |
| 1462 | |
| 1463 | mutex_lock(&ps_data->io_lock); |
| 1464 | als_thd_l0_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL1_ALS_REG); |
| 1465 | als_thd_l1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL2_ALS_REG); |
| 1466 | if(als_thd_l0_reg < 0) |
| 1467 | { |
| 1468 | printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l0_reg); |
| 1469 | return -EINVAL; |
| 1470 | } |
| 1471 | if(als_thd_l1_reg < 0) |
| 1472 | { |
| 1473 | printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l1_reg); |
| 1474 | return -EINVAL; |
| 1475 | } |
| 1476 | als_thd_l0_reg|=(als_thd_l1_reg<<8); |
| 1477 | als_lux = stk_alscode2lux(ps_data, als_thd_l0_reg); |
| 1478 | mutex_unlock(&ps_data->io_lock); |
| 1479 | return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux); |
| 1480 | } |
| 1481 | |
| 1482 | |
| 1483 | static ssize_t stk_als_lux_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1484 | { |
| 1485 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1486 | unsigned long value = 0; |
| 1487 | int ret; |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1488 | ret = kstrtoul(buf, 10, &value); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1489 | if(ret < 0) |
| 1490 | { |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1491 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 1492 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1493 | return ret; |
| 1494 | } |
| 1495 | mutex_lock(&ps_data->io_lock); |
| 1496 | value = stk_lux2alscode(ps_data, value); |
| 1497 | stk3x1x_set_als_thd_l(ps_data, value); |
| 1498 | mutex_unlock(&ps_data->io_lock); |
| 1499 | return size; |
| 1500 | } |
| 1501 | |
| 1502 | static ssize_t stk_als_lux_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1503 | { |
| 1504 | int32_t als_thd_h0_reg,als_thd_h1_reg; |
| 1505 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1506 | uint32_t als_lux; |
| 1507 | |
| 1508 | mutex_lock(&ps_data->io_lock); |
| 1509 | als_thd_h0_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH1_ALS_REG); |
| 1510 | als_thd_h1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH2_ALS_REG); |
| 1511 | if(als_thd_h0_reg < 0) |
| 1512 | { |
| 1513 | printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h0_reg); |
| 1514 | return -EINVAL; |
| 1515 | } |
| 1516 | if(als_thd_h1_reg < 0) |
| 1517 | { |
| 1518 | printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h1_reg); |
| 1519 | return -EINVAL; |
| 1520 | } |
| 1521 | als_thd_h0_reg|=(als_thd_h1_reg<<8); |
| 1522 | als_lux = stk_alscode2lux(ps_data, als_thd_h0_reg); |
| 1523 | mutex_unlock(&ps_data->io_lock); |
| 1524 | return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux); |
| 1525 | } |
| 1526 | |
| 1527 | |
| 1528 | static ssize_t stk_als_lux_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1529 | { |
| 1530 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1531 | unsigned long value = 0; |
| 1532 | int ret; |
| 1533 | ret = strict_strtoul(buf, 10, &value); |
| 1534 | if(ret < 0) |
| 1535 | { |
| 1536 | printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret); |
| 1537 | return ret; |
| 1538 | } |
| 1539 | mutex_lock(&ps_data->io_lock); |
| 1540 | value = stk_lux2alscode(ps_data, value); |
| 1541 | stk3x1x_set_als_thd_h(ps_data, value); |
| 1542 | mutex_unlock(&ps_data->io_lock); |
| 1543 | return size; |
| 1544 | } |
| 1545 | #endif |
| 1546 | |
| 1547 | |
| 1548 | static ssize_t stk_all_reg_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1549 | { |
| 1550 | int32_t ps_reg[27]; |
| 1551 | uint8_t cnt; |
| 1552 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1553 | mutex_lock(&ps_data->io_lock); |
| 1554 | for(cnt=0;cnt<25;cnt++) |
| 1555 | { |
| 1556 | ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, (cnt)); |
| 1557 | if(ps_reg[cnt] < 0) |
| 1558 | { |
| 1559 | mutex_unlock(&ps_data->io_lock); |
| 1560 | printk(KERN_ERR "stk_all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]); |
| 1561 | return -EINVAL; |
| 1562 | } |
| 1563 | else |
| 1564 | { |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 1565 | dev_dbg(dev, "reg[0x%2X]=0x%2X\n", cnt, ps_reg[cnt]); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, STK_PDT_ID_REG); |
| 1569 | if(ps_reg[cnt] < 0) |
| 1570 | { |
| 1571 | mutex_unlock(&ps_data->io_lock); |
| 1572 | printk( KERN_ERR "all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]); |
| 1573 | return -EINVAL; |
| 1574 | } |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 1575 | dev_dbg(dev, "reg[0x%x]=0x%2X\n", STK_PDT_ID_REG, ps_reg[cnt]); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1576 | cnt++; |
| 1577 | ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, STK_RSRVD_REG); |
| 1578 | if(ps_reg[cnt] < 0) |
| 1579 | { |
| 1580 | mutex_unlock(&ps_data->io_lock); |
| 1581 | printk( KERN_ERR "all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]); |
| 1582 | return -EINVAL; |
| 1583 | } |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 1584 | dev_dbg(dev, "reg[0x%x]=0x%2X\n", STK_RSRVD_REG, ps_reg[cnt]); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1585 | mutex_unlock(&ps_data->io_lock); |
| 1586 | |
| 1587 | return scnprintf(buf, PAGE_SIZE, "%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X\n", |
| 1588 | ps_reg[0], ps_reg[1], ps_reg[2], ps_reg[3], ps_reg[4], ps_reg[5], ps_reg[6], ps_reg[7], ps_reg[8], |
| 1589 | ps_reg[9], ps_reg[10], ps_reg[11], ps_reg[12], ps_reg[13], ps_reg[14], ps_reg[15], ps_reg[16], ps_reg[17], |
| 1590 | ps_reg[18], ps_reg[19], ps_reg[20], ps_reg[21], ps_reg[22], ps_reg[23], ps_reg[24], ps_reg[25], ps_reg[26]); |
| 1591 | } |
| 1592 | |
| 1593 | static ssize_t stk_recv_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1594 | { |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | |
| 1599 | static ssize_t stk_recv_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1600 | { |
| 1601 | unsigned long value = 0; |
| 1602 | int ret; |
| 1603 | int32_t recv_data; |
| 1604 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1605 | |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1606 | ret = kstrtoul(buf, 16, &value); |
| 1607 | if (ret < 0) { |
| 1608 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 1609 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1610 | return ret; |
| 1611 | } |
| 1612 | recv_data = i2c_smbus_read_byte_data(ps_data->client,value); |
| 1613 | printk("%s: reg 0x%x=0x%x\n", __func__, (int)value, recv_data); |
| 1614 | return size; |
| 1615 | } |
| 1616 | |
| 1617 | |
| 1618 | static ssize_t stk_send_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1619 | { |
| 1620 | return 0; |
| 1621 | } |
| 1622 | |
| 1623 | |
| 1624 | static ssize_t stk_send_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) |
| 1625 | { |
| 1626 | int addr, cmd; |
| 1627 | u8 addr_u8, cmd_u8; |
| 1628 | int32_t ret, i; |
| 1629 | char *token[10]; |
| 1630 | struct stk3x1x_data *ps_data = dev_get_drvdata(dev); |
| 1631 | |
| 1632 | for (i = 0; i < 2; i++) |
| 1633 | token[i] = strsep((char **)&buf, " "); |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1634 | ret = kstrtoul(token[0], 16, (unsigned long *)&(addr)); |
| 1635 | if (ret < 0) { |
| 1636 | |
| 1637 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 1638 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1639 | return ret; |
| 1640 | } |
Bingzhe Cai | 440b835 | 2013-07-26 01:02:29 +0800 | [diff] [blame] | 1641 | ret = kstrtoul(token[1], 16, (unsigned long *)&(cmd)); |
| 1642 | if (ret < 0) { |
| 1643 | printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", |
| 1644 | __func__, ret); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1645 | return ret; |
| 1646 | } |
Bingzhe Cai | 0c4de11 | 2013-10-21 13:57:42 +0800 | [diff] [blame] | 1647 | dev_dbg(dev, "%s: write reg 0x%x=0x%x\n", __func__, addr, cmd); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1648 | addr_u8 = (u8) addr; |
| 1649 | cmd_u8 = (u8) cmd; |
| 1650 | //mutex_lock(&ps_data->io_lock); |
| 1651 | ret = i2c_smbus_write_byte_data(ps_data->client,addr_u8,cmd_u8); |
| 1652 | //mutex_unlock(&ps_data->io_lock); |
| 1653 | if (0 != ret) |
| 1654 | { |
| 1655 | printk(KERN_ERR "%s: i2c_smbus_write_byte_data fail\n", __func__); |
| 1656 | return ret; |
| 1657 | } |
| 1658 | |
| 1659 | return size; |
| 1660 | } |
| 1661 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1662 | static struct device_attribute als_enable_attribute = __ATTR(enable,0664,stk_als_enable_show,stk_als_enable_store); |
| 1663 | static struct device_attribute als_lux_attribute = __ATTR(lux,0664,stk_als_lux_show,stk_als_lux_store); |
| 1664 | static struct device_attribute als_code_attribute = __ATTR(code, 0444, stk_als_code_show, NULL); |
| 1665 | static struct device_attribute als_transmittance_attribute = __ATTR(transmittance,0664,stk_als_transmittance_show,stk_als_transmittance_store); |
Bingzhe Cai | 9a15793 | 2013-07-11 01:09:41 +0800 | [diff] [blame] | 1666 | static struct device_attribute als_poll_delay_attribute = |
| 1667 | __ATTR(poll_delay, 0664, stk_als_delay_show, stk_als_delay_store); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1668 | static struct device_attribute als_ir_code_attribute = __ATTR(ircode,0444,stk_als_ir_code_show,NULL); |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 1669 | static struct device_attribute als_firlen_attribute = |
| 1670 | __ATTR(firlen, 0664, stk_als_firlen_show, stk_als_firlen_store); |
| 1671 | static struct device_attribute als_fir_enable_attribute = |
| 1672 | __ATTR(fir_enable, 0664, stk_als_fir_enable_show, |
| 1673 | stk_als_fir_enable_store); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1674 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1675 | static struct attribute *stk_als_attrs [] = |
| 1676 | { |
| 1677 | &als_enable_attribute.attr, |
| 1678 | &als_lux_attribute.attr, |
| 1679 | &als_code_attribute.attr, |
| 1680 | &als_transmittance_attribute.attr, |
| 1681 | &als_poll_delay_attribute.attr, |
| 1682 | &als_ir_code_attribute.attr, |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 1683 | &als_firlen_attribute.attr, |
| 1684 | &als_fir_enable_attribute.attr, |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1685 | NULL |
| 1686 | }; |
| 1687 | |
| 1688 | static struct attribute_group stk_als_attribute_group = { |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1689 | .attrs = stk_als_attrs, |
| 1690 | }; |
| 1691 | |
| 1692 | |
| 1693 | static struct device_attribute ps_enable_attribute = __ATTR(enable,0664,stk_ps_enable_show,stk_ps_enable_store); |
| 1694 | static struct device_attribute ps_enable_aso_attribute = __ATTR(enableaso,0664,stk_ps_enable_aso_show,stk_ps_enable_aso_store); |
| 1695 | static struct device_attribute ps_distance_attribute = __ATTR(distance,0664,stk_ps_distance_show, stk_ps_distance_store); |
| 1696 | static struct device_attribute ps_offset_attribute = __ATTR(offset,0664,stk_ps_offset_show, stk_ps_offset_store); |
| 1697 | static struct device_attribute ps_code_attribute = __ATTR(code, 0444, stk_ps_code_show, NULL); |
| 1698 | static struct device_attribute ps_code_thd_l_attribute = __ATTR(codethdl,0664,stk_ps_code_thd_l_show,stk_ps_code_thd_l_store); |
| 1699 | static struct device_attribute ps_code_thd_h_attribute = __ATTR(codethdh,0664,stk_ps_code_thd_h_show,stk_ps_code_thd_h_store); |
| 1700 | static struct device_attribute recv_attribute = __ATTR(recv,0664,stk_recv_show,stk_recv_store); |
| 1701 | static struct device_attribute send_attribute = __ATTR(send,0664,stk_send_show, stk_send_store); |
| 1702 | static struct device_attribute all_reg_attribute = __ATTR(allreg, 0444, stk_all_reg_show, NULL); |
| 1703 | |
| 1704 | static struct attribute *stk_ps_attrs [] = |
| 1705 | { |
| 1706 | &ps_enable_attribute.attr, |
| 1707 | &ps_enable_aso_attribute.attr, |
| 1708 | &ps_distance_attribute.attr, |
| 1709 | &ps_offset_attribute.attr, |
| 1710 | &ps_code_attribute.attr, |
| 1711 | &ps_code_thd_l_attribute.attr, |
| 1712 | &ps_code_thd_h_attribute.attr, |
| 1713 | &recv_attribute.attr, |
| 1714 | &send_attribute.attr, |
| 1715 | &all_reg_attribute.attr, |
| 1716 | NULL |
| 1717 | }; |
| 1718 | |
| 1719 | static struct attribute_group stk_ps_attribute_group = { |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1720 | .attrs = stk_ps_attrs, |
| 1721 | }; |
| 1722 | |
| 1723 | #ifdef STK_POLL_ALS |
| 1724 | static enum hrtimer_restart stk_als_timer_func(struct hrtimer *timer) |
| 1725 | { |
| 1726 | struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, als_timer); |
| 1727 | queue_work(ps_data->stk_als_wq, &ps_data->stk_als_work); |
| 1728 | hrtimer_forward_now(&ps_data->als_timer, ps_data->als_poll_delay); |
| 1729 | return HRTIMER_RESTART; |
| 1730 | } |
| 1731 | |
| 1732 | static void stk_als_work_func(struct work_struct *work) |
| 1733 | { |
| 1734 | struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_als_work); |
| 1735 | int32_t reading; |
| 1736 | |
| 1737 | mutex_lock(&ps_data->io_lock); |
| 1738 | reading = stk3x1x_get_als_reading(ps_data); |
| 1739 | if(reading < 0) |
| 1740 | return; |
| 1741 | ps_data->als_lux_last = stk_alscode2lux(ps_data, reading); |
| 1742 | input_report_abs(ps_data->als_input_dev, ABS_MISC, ps_data->als_lux_last); |
| 1743 | input_sync(ps_data->als_input_dev); |
| 1744 | mutex_unlock(&ps_data->io_lock); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1745 | } |
| 1746 | #endif |
| 1747 | |
| 1748 | static enum hrtimer_restart stk_ps_timer_func(struct hrtimer *timer) |
| 1749 | { |
| 1750 | struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, ps_timer); |
| 1751 | queue_work(ps_data->stk_ps_wq, &ps_data->stk_ps_work); |
| 1752 | #ifdef STK_POLL_PS |
| 1753 | hrtimer_forward_now(&ps_data->ps_timer, ps_data->ps_poll_delay); |
| 1754 | return HRTIMER_RESTART; |
| 1755 | #else |
| 1756 | hrtimer_cancel(&ps_data->ps_timer); |
| 1757 | return HRTIMER_NORESTART; |
| 1758 | #endif |
| 1759 | } |
| 1760 | |
| 1761 | static void stk_ps_work_func(struct work_struct *work) |
| 1762 | { |
| 1763 | struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_ps_work); |
| 1764 | uint32_t reading; |
| 1765 | int32_t near_far_state; |
| 1766 | uint8_t org_flag_reg; |
| 1767 | int32_t ret; |
| 1768 | uint8_t disable_flag = 0; |
| 1769 | mutex_lock(&ps_data->io_lock); |
| 1770 | |
| 1771 | org_flag_reg = stk3x1x_get_flag(ps_data); |
| 1772 | if(org_flag_reg < 0) |
| 1773 | { |
| 1774 | printk(KERN_ERR "%s: get_status_reg fail, ret=%d", __func__, org_flag_reg); |
| 1775 | goto err_i2c_rw; |
| 1776 | } |
| 1777 | near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0; |
| 1778 | reading = stk3x1x_get_ps_reading(ps_data); |
| 1779 | if(ps_data->ps_distance_last != near_far_state) |
| 1780 | { |
| 1781 | ps_data->ps_distance_last = near_far_state; |
| 1782 | input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state); |
| 1783 | input_sync(ps_data->ps_input_dev); |
| 1784 | wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ); |
| 1785 | #ifdef STK_DEBUG_PRINTF |
| 1786 | printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n",__func__, near_far_state, reading); |
| 1787 | #endif |
| 1788 | } |
| 1789 | ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag); |
| 1790 | if(ret < 0) |
| 1791 | { |
| 1792 | printk(KERN_ERR "%s:stk3x1x_set_flag fail, ret=%d\n", __func__, ret); |
| 1793 | goto err_i2c_rw; |
| 1794 | } |
| 1795 | |
| 1796 | mutex_unlock(&ps_data->io_lock); |
| 1797 | return; |
| 1798 | |
| 1799 | err_i2c_rw: |
| 1800 | mutex_unlock(&ps_data->io_lock); |
| 1801 | msleep(30); |
| 1802 | return; |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS)) |
| 1807 | static void stk_work_func(struct work_struct *work) |
| 1808 | { |
| 1809 | uint32_t reading; |
| 1810 | #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02)) |
| 1811 | int32_t ret; |
| 1812 | uint8_t disable_flag = 0; |
| 1813 | uint8_t org_flag_reg; |
| 1814 | #endif /* #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02)) */ |
| 1815 | |
| 1816 | #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD |
| 1817 | uint32_t nLuxIndex; |
| 1818 | #endif |
| 1819 | struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_work); |
| 1820 | int32_t near_far_state; |
| 1821 | |
| 1822 | mutex_lock(&ps_data->io_lock); |
| 1823 | |
| 1824 | #if (STK_INT_PS_MODE == 0x03) |
| 1825 | near_far_state = gpio_get_value(ps_data->int_pin); |
| 1826 | #elif (STK_INT_PS_MODE == 0x02) |
| 1827 | near_far_state = !(gpio_get_value(ps_data->int_pin)); |
| 1828 | #endif |
| 1829 | |
| 1830 | #if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02)) |
| 1831 | ps_data->ps_distance_last = near_far_state; |
| 1832 | input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state); |
| 1833 | input_sync(ps_data->ps_input_dev); |
| 1834 | wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ); |
| 1835 | reading = stk3x1x_get_ps_reading(ps_data); |
| 1836 | #ifdef STK_DEBUG_PRINTF |
| 1837 | printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n",__func__, near_far_state, reading); |
| 1838 | #endif |
| 1839 | #else |
| 1840 | /* mode 0x01 or 0x04 */ |
| 1841 | org_flag_reg = stk3x1x_get_flag(ps_data); |
| 1842 | if(org_flag_reg < 0) |
| 1843 | { |
| 1844 | printk(KERN_ERR "%s: get_status_reg fail, org_flag_reg=%d", __func__, org_flag_reg); |
| 1845 | goto err_i2c_rw; |
| 1846 | } |
| 1847 | |
| 1848 | if (org_flag_reg & STK_FLG_ALSINT_MASK) |
| 1849 | { |
| 1850 | disable_flag |= STK_FLG_ALSINT_MASK; |
| 1851 | reading = stk3x1x_get_als_reading(ps_data); |
| 1852 | if(reading < 0) |
| 1853 | { |
| 1854 | printk(KERN_ERR "%s: stk3x1x_get_als_reading fail, ret=%d", __func__, reading); |
| 1855 | goto err_i2c_rw; |
| 1856 | } |
| 1857 | #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD |
| 1858 | nLuxIndex = stk_get_lux_interval_index(reading); |
| 1859 | stk3x1x_set_als_thd_h(ps_data, code_threshold_table[nLuxIndex]); |
| 1860 | stk3x1x_set_als_thd_l(ps_data, code_threshold_table[nLuxIndex-1]); |
| 1861 | #else |
| 1862 | stk_als_set_new_thd(ps_data, reading); |
| 1863 | #endif //CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD |
| 1864 | ps_data->als_lux_last = stk_alscode2lux(ps_data, reading); |
| 1865 | input_report_abs(ps_data->als_input_dev, ABS_MISC, ps_data->als_lux_last); |
| 1866 | input_sync(ps_data->als_input_dev); |
| 1867 | #ifdef STK_DEBUG_PRINTF |
| 1868 | printk(KERN_INFO "%s: als input event %d lux\n",__func__, ps_data->als_lux_last); |
| 1869 | #endif |
| 1870 | } |
| 1871 | if (org_flag_reg & STK_FLG_PSINT_MASK) |
| 1872 | { |
| 1873 | disable_flag |= STK_FLG_PSINT_MASK; |
| 1874 | near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0; |
| 1875 | |
| 1876 | ps_data->ps_distance_last = near_far_state; |
| 1877 | input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state); |
| 1878 | input_sync(ps_data->ps_input_dev); |
| 1879 | wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ); |
| 1880 | reading = stk3x1x_get_ps_reading(ps_data); |
| 1881 | #ifdef STK_DEBUG_PRINTF |
| 1882 | printk(KERN_INFO "%s: ps input event=%d, ps code = %d\n",__func__, near_far_state, reading); |
| 1883 | #endif |
| 1884 | } |
| 1885 | |
| 1886 | ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag); |
| 1887 | if(ret < 0) |
| 1888 | { |
| 1889 | printk(KERN_ERR "%s:reset_int_flag fail, ret=%d\n", __func__, ret); |
| 1890 | goto err_i2c_rw; |
| 1891 | } |
| 1892 | #endif |
| 1893 | |
| 1894 | msleep(1); |
| 1895 | enable_irq(ps_data->irq); |
| 1896 | mutex_unlock(&ps_data->io_lock); |
| 1897 | return; |
| 1898 | |
| 1899 | err_i2c_rw: |
| 1900 | mutex_unlock(&ps_data->io_lock); |
| 1901 | msleep(30); |
| 1902 | enable_irq(ps_data->irq); |
| 1903 | return; |
| 1904 | } |
| 1905 | #endif |
| 1906 | |
| 1907 | #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS)) |
| 1908 | static irqreturn_t stk_oss_irq_handler(int irq, void *data) |
| 1909 | { |
| 1910 | struct stk3x1x_data *pData = data; |
| 1911 | disable_irq_nosync(irq); |
| 1912 | queue_work(pData->stk_wq,&pData->stk_work); |
| 1913 | return IRQ_HANDLED; |
| 1914 | } |
| 1915 | #endif /* #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS)) */ |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 1916 | |
| 1917 | static inline void stk3x1x_init_fir(struct stk3x1x_data *ps_data) |
| 1918 | { |
| 1919 | memset(&ps_data->fir, 0x00, sizeof(ps_data->fir)); |
| 1920 | atomic_set(&ps_data->firlength, STK_FIR_LEN); |
| 1921 | } |
| 1922 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1923 | static int32_t stk3x1x_init_all_setting(struct i2c_client *client, struct stk3x1x_platform_data *plat_data) |
| 1924 | { |
| 1925 | int32_t ret; |
| 1926 | struct stk3x1x_data *ps_data = i2c_get_clientdata(client); |
| 1927 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1928 | ret = stk3x1x_software_reset(ps_data); |
| 1929 | if(ret < 0) |
| 1930 | return ret; |
| 1931 | |
| 1932 | stk3x1x_check_pid(ps_data); |
| 1933 | if(ret < 0) |
| 1934 | return ret; |
| 1935 | |
| 1936 | ret = stk3x1x_init_all_reg(ps_data, plat_data); |
| 1937 | if(ret < 0) |
| 1938 | return ret; |
| 1939 | #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD |
| 1940 | stk_init_code_threshold_table(ps_data); |
| 1941 | #endif |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 1942 | |
| 1943 | if (plat_data->use_fir) |
| 1944 | stk3x1x_init_fir(ps_data); |
| 1945 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 1946 | return 0; |
| 1947 | } |
| 1948 | |
| 1949 | #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS)) |
| 1950 | static int stk3x1x_setup_irq(struct i2c_client *client) |
| 1951 | { |
| 1952 | int irq, err = -EIO; |
| 1953 | struct stk3x1x_data *ps_data = i2c_get_clientdata(client); |
| 1954 | |
| 1955 | irq = gpio_to_irq(ps_data->int_pin); |
| 1956 | #ifdef STK_DEBUG_PRINTF |
| 1957 | printk(KERN_INFO "%s: int pin #=%d, irq=%d\n",__func__, ps_data->int_pin, irq); |
| 1958 | #endif |
| 1959 | if (irq <= 0) |
| 1960 | { |
| 1961 | printk(KERN_ERR "irq number is not specified, irq # = %d, int pin=%d\n",irq, ps_data->int_pin); |
| 1962 | return irq; |
| 1963 | } |
| 1964 | ps_data->irq = irq; |
| 1965 | err = gpio_request(ps_data->int_pin,"stk-int"); |
| 1966 | if(err < 0) |
| 1967 | { |
| 1968 | printk(KERN_ERR "%s: gpio_request, err=%d", __func__, err); |
| 1969 | return err; |
| 1970 | } |
| 1971 | err = gpio_direction_input(ps_data->int_pin); |
| 1972 | if(err < 0) |
| 1973 | { |
| 1974 | printk(KERN_ERR "%s: gpio_direction_input, err=%d", __func__, err); |
| 1975 | return err; |
| 1976 | } |
| 1977 | #if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02)) |
| 1978 | err = request_any_context_irq(irq, stk_oss_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, DEVICE_NAME, ps_data); |
| 1979 | #else |
| 1980 | err = request_any_context_irq(irq, stk_oss_irq_handler, IRQF_TRIGGER_LOW, DEVICE_NAME, ps_data); |
| 1981 | #endif |
| 1982 | if (err < 0) |
| 1983 | { |
| 1984 | printk(KERN_WARNING "%s: request_any_context_irq(%d) failed for (%d)\n", __func__, irq, err); |
| 1985 | goto err_request_any_context_irq; |
| 1986 | } |
| 1987 | disable_irq(irq); |
| 1988 | |
| 1989 | return 0; |
| 1990 | err_request_any_context_irq: |
| 1991 | gpio_free(ps_data->int_pin); |
| 1992 | return err; |
| 1993 | } |
| 1994 | #endif |
| 1995 | |
| 1996 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 1997 | static void stk3x1x_early_suspend(struct early_suspend *h) |
| 1998 | { |
| 1999 | struct stk3x1x_data *ps_data = container_of(h, struct stk3x1x_data, stk_early_suspend); |
| 2000 | #ifndef STK_POLL_PS |
| 2001 | int err; |
| 2002 | #endif |
| 2003 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2004 | mutex_lock(&ps_data->io_lock); |
| 2005 | if(ps_data->als_enabled) |
| 2006 | { |
| 2007 | stk3x1x_enable_als(ps_data, 0); |
| 2008 | ps_data->als_enabled = true; |
| 2009 | } |
| 2010 | if(ps_data->ps_enabled) |
| 2011 | { |
| 2012 | #ifdef STK_POLL_PS |
| 2013 | wake_lock(&ps_data->ps_nosuspend_wl); |
| 2014 | #else |
| 2015 | err = enable_irq_wake(ps_data->irq); |
| 2016 | if (err) |
| 2017 | printk(KERN_WARNING "%s: set_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err); |
| 2018 | #endif |
| 2019 | } |
| 2020 | mutex_unlock(&ps_data->io_lock); |
| 2021 | return; |
| 2022 | } |
| 2023 | |
| 2024 | static void stk3x1x_late_resume(struct early_suspend *h) |
| 2025 | { |
| 2026 | struct stk3x1x_data *ps_data = container_of(h, struct stk3x1x_data, stk_early_suspend); |
| 2027 | #ifndef STK_POLL_PS |
| 2028 | int err; |
| 2029 | #endif |
| 2030 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2031 | mutex_lock(&ps_data->io_lock); |
| 2032 | if(ps_data->als_enabled) |
| 2033 | stk3x1x_enable_als(ps_data, 1); |
| 2034 | |
| 2035 | if(ps_data->ps_enabled) |
| 2036 | { |
| 2037 | #ifdef STK_POLL_PS |
| 2038 | wake_lock(&ps_data->ps_nosuspend_wl); |
| 2039 | #else |
| 2040 | err = disable_irq_wake(ps_data->irq); |
| 2041 | if (err) |
| 2042 | printk(KERN_WARNING "%s: disable_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err); |
| 2043 | #endif |
| 2044 | } |
| 2045 | mutex_unlock(&ps_data->io_lock); |
| 2046 | return; |
| 2047 | } |
| 2048 | #endif //#ifdef CONFIG_HAS_EARLYSUSPEND |
| 2049 | |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2050 | static int stk3x1x_power_ctl(struct stk3x1x_data *data, bool on) |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 2051 | { |
| 2052 | int ret = 0; |
| 2053 | |
| 2054 | if (!on && data->power_enabled) { |
| 2055 | ret = regulator_disable(data->vdd); |
| 2056 | if (ret) { |
| 2057 | dev_err(&data->client->dev, |
| 2058 | "Regulator vdd disable failed ret=%d\n", ret); |
| 2059 | return ret; |
| 2060 | } |
| 2061 | |
| 2062 | ret = regulator_disable(data->vio); |
| 2063 | if (ret) { |
| 2064 | dev_err(&data->client->dev, |
| 2065 | "Regulator vio disable failed ret=%d\n", ret); |
| 2066 | regulator_enable(data->vdd); |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2067 | return ret; |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 2068 | } |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2069 | data->power_enabled = on; |
| 2070 | dev_dbg(&data->client->dev, "stk3x1x_power_ctl on=%d\n", |
| 2071 | on); |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 2072 | } else if (on && !data->power_enabled) { |
| 2073 | |
| 2074 | ret = regulator_enable(data->vdd); |
| 2075 | if (ret) { |
| 2076 | dev_err(&data->client->dev, |
| 2077 | "Regulator vdd enable failed ret=%d\n", ret); |
| 2078 | return ret; |
| 2079 | } |
| 2080 | |
| 2081 | ret = regulator_enable(data->vio); |
| 2082 | if (ret) { |
| 2083 | dev_err(&data->client->dev, |
| 2084 | "Regulator vio enable failed ret=%d\n", ret); |
| 2085 | regulator_disable(data->vdd); |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2086 | return ret; |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 2087 | } |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2088 | data->power_enabled = on; |
| 2089 | dev_dbg(&data->client->dev, "stk3x1x_power_ctl on=%d\n", |
| 2090 | on); |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 2091 | } else { |
| 2092 | dev_warn(&data->client->dev, |
| 2093 | "Power on=%d. enabled=%d\n", |
| 2094 | on, data->power_enabled); |
| 2095 | } |
| 2096 | |
| 2097 | return ret; |
| 2098 | } |
| 2099 | |
| 2100 | static int stk3x1x_power_init(struct stk3x1x_data *data, bool on) |
| 2101 | { |
| 2102 | int ret; |
| 2103 | |
| 2104 | if (!on) { |
| 2105 | if (regulator_count_voltages(data->vdd) > 0) |
| 2106 | regulator_set_voltage(data->vdd, |
| 2107 | 0, STK3X1X_VDD_MAX_UV); |
| 2108 | |
| 2109 | regulator_put(data->vdd); |
| 2110 | |
| 2111 | if (regulator_count_voltages(data->vio) > 0) |
| 2112 | regulator_set_voltage(data->vio, |
| 2113 | 0, STK3X1X_VIO_MAX_UV); |
| 2114 | |
| 2115 | regulator_put(data->vio); |
| 2116 | } else { |
| 2117 | data->vdd = regulator_get(&data->client->dev, "vdd"); |
| 2118 | if (IS_ERR(data->vdd)) { |
| 2119 | ret = PTR_ERR(data->vdd); |
| 2120 | dev_err(&data->client->dev, |
| 2121 | "Regulator get failed vdd ret=%d\n", ret); |
| 2122 | return ret; |
| 2123 | } |
| 2124 | |
| 2125 | if (regulator_count_voltages(data->vdd) > 0) { |
| 2126 | ret = regulator_set_voltage(data->vdd, |
| 2127 | STK3X1X_VDD_MIN_UV, |
| 2128 | STK3X1X_VDD_MAX_UV); |
| 2129 | if (ret) { |
| 2130 | dev_err(&data->client->dev, |
| 2131 | "Regulator set failed vdd ret=%d\n", |
| 2132 | ret); |
| 2133 | goto reg_vdd_put; |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | data->vio = regulator_get(&data->client->dev, "vio"); |
| 2138 | if (IS_ERR(data->vio)) { |
| 2139 | ret = PTR_ERR(data->vio); |
| 2140 | dev_err(&data->client->dev, |
| 2141 | "Regulator get failed vio ret=%d\n", ret); |
| 2142 | goto reg_vdd_set; |
| 2143 | } |
| 2144 | |
| 2145 | if (regulator_count_voltages(data->vio) > 0) { |
| 2146 | ret = regulator_set_voltage(data->vio, |
| 2147 | STK3X1X_VIO_MIN_UV, |
| 2148 | STK3X1X_VIO_MAX_UV); |
| 2149 | if (ret) { |
| 2150 | dev_err(&data->client->dev, |
| 2151 | "Regulator set failed vio ret=%d\n", ret); |
| 2152 | goto reg_vio_put; |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | return 0; |
| 2158 | |
| 2159 | reg_vio_put: |
| 2160 | regulator_put(data->vio); |
| 2161 | reg_vdd_set: |
| 2162 | if (regulator_count_voltages(data->vdd) > 0) |
| 2163 | regulator_set_voltage(data->vdd, 0, STK3X1X_VDD_MAX_UV); |
| 2164 | reg_vdd_put: |
| 2165 | regulator_put(data->vdd); |
| 2166 | return ret; |
| 2167 | } |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2168 | |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2169 | static int stk3x1x_device_ctl(struct stk3x1x_data *ps_data, bool enable) |
| 2170 | { |
| 2171 | int ret; |
| 2172 | struct device *dev = &ps_data->client->dev; |
| 2173 | |
| 2174 | if (enable && !ps_data->power_enabled) { |
| 2175 | ret = stk3x1x_power_ctl(ps_data, true); |
| 2176 | if (ret) { |
| 2177 | dev_err(dev, "Failed to enable device power\n"); |
| 2178 | goto err_exit; |
| 2179 | } |
| 2180 | ret = stk3x1x_init_all_setting(ps_data->client, ps_data->pdata); |
| 2181 | if (ret < 0) { |
| 2182 | stk3x1x_power_ctl(ps_data, false); |
| 2183 | dev_err(dev, "Failed to re-init device setting\n"); |
| 2184 | goto err_exit; |
| 2185 | } |
| 2186 | } else if (!enable && ps_data->power_enabled) { |
| 2187 | if (!ps_data->als_enabled && !ps_data->ps_enabled) { |
| 2188 | ret = stk3x1x_power_ctl(ps_data, false); |
| 2189 | if (ret) { |
| 2190 | dev_err(dev, "Failed to disable device power\n"); |
| 2191 | goto err_exit; |
| 2192 | } |
| 2193 | } else { |
| 2194 | dev_dbg(dev, "device control: als_enabled=%d, ps_enabled=%d\n", |
| 2195 | ps_data->als_enabled, ps_data->ps_enabled); |
| 2196 | } |
| 2197 | } else { |
| 2198 | dev_dbg(dev, "device control: enable=%d, power_enabled=%d\n", |
| 2199 | enable, ps_data->power_enabled); |
| 2200 | } |
| 2201 | return 0; |
| 2202 | |
| 2203 | err_exit: |
| 2204 | return ret; |
| 2205 | } |
Bingzhe Cai | 1ccffb7 | 2013-07-26 01:31:19 +0800 | [diff] [blame] | 2206 | #ifdef CONFIG_OF |
| 2207 | static int stk3x1x_parse_dt(struct device *dev, |
| 2208 | struct stk3x1x_platform_data *pdata) |
| 2209 | { |
| 2210 | int rc; |
| 2211 | struct device_node *np = dev->of_node; |
| 2212 | u32 temp_val; |
| 2213 | |
| 2214 | pdata->int_pin = of_get_named_gpio_flags(np, "stk,irq-gpio", |
| 2215 | 0, &pdata->int_flags); |
| 2216 | if (pdata->int_pin < 0) { |
| 2217 | dev_err(dev, "Unable to read irq-gpio\n"); |
| 2218 | return pdata->int_pin; |
| 2219 | } |
| 2220 | |
| 2221 | rc = of_property_read_u32(np, "stk,transmittance", &temp_val); |
| 2222 | if (!rc) |
| 2223 | pdata->transmittance = temp_val; |
| 2224 | else { |
| 2225 | dev_err(dev, "Unable to read transmittance\n"); |
| 2226 | return rc; |
| 2227 | } |
| 2228 | |
| 2229 | rc = of_property_read_u32(np, "stk,state-reg", &temp_val); |
| 2230 | if (!rc) |
| 2231 | pdata->state_reg = temp_val; |
| 2232 | else { |
| 2233 | dev_err(dev, "Unable to read state-reg\n"); |
| 2234 | return rc; |
| 2235 | } |
| 2236 | |
| 2237 | rc = of_property_read_u32(np, "stk,psctrl-reg", &temp_val); |
| 2238 | if (!rc) |
| 2239 | pdata->psctrl_reg = (u8)temp_val; |
| 2240 | else { |
| 2241 | dev_err(dev, "Unable to read psctrl-reg\n"); |
| 2242 | return rc; |
| 2243 | } |
| 2244 | |
| 2245 | rc = of_property_read_u32(np, "stk,alsctrl-reg", &temp_val); |
| 2246 | if (!rc) |
| 2247 | pdata->alsctrl_reg = (u8)temp_val; |
| 2248 | else { |
| 2249 | dev_err(dev, "Unable to read alsctrl-reg\n"); |
| 2250 | return rc; |
| 2251 | } |
| 2252 | |
| 2253 | rc = of_property_read_u32(np, "stk,ledctrl-reg", &temp_val); |
| 2254 | if (!rc) |
| 2255 | pdata->ledctrl_reg = (u8)temp_val; |
| 2256 | else { |
| 2257 | dev_err(dev, "Unable to read ledctrl-reg\n"); |
| 2258 | return rc; |
| 2259 | } |
| 2260 | |
| 2261 | rc = of_property_read_u32(np, "stk,wait-reg", &temp_val); |
| 2262 | if (!rc) |
| 2263 | pdata->wait_reg = (u8)temp_val; |
| 2264 | else { |
| 2265 | dev_err(dev, "Unable to read wait-reg\n"); |
| 2266 | return rc; |
| 2267 | } |
| 2268 | |
| 2269 | rc = of_property_read_u32(np, "stk,ps-thdh", &temp_val); |
| 2270 | if (!rc) |
| 2271 | pdata->ps_thd_h = (u16)temp_val; |
| 2272 | else { |
| 2273 | dev_err(dev, "Unable to read ps-thdh\n"); |
| 2274 | return rc; |
| 2275 | } |
| 2276 | |
| 2277 | rc = of_property_read_u32(np, "stk,ps-thdl", &temp_val); |
| 2278 | if (!rc) |
| 2279 | pdata->ps_thd_l = (u16)temp_val; |
| 2280 | else { |
| 2281 | dev_err(dev, "Unable to read ps-thdl\n"); |
| 2282 | return rc; |
| 2283 | } |
| 2284 | |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 2285 | pdata->use_fir = of_property_read_bool(np, "stk,use-fir"); |
| 2286 | |
Bingzhe Cai | 1ccffb7 | 2013-07-26 01:31:19 +0800 | [diff] [blame] | 2287 | return 0; |
| 2288 | } |
| 2289 | #else |
| 2290 | static int stk3x1x_parse_dt(struct device *dev, |
| 2291 | struct stk3x1x_platform_data *pdata) |
| 2292 | { |
| 2293 | return -ENODEV; |
| 2294 | } |
| 2295 | #endif /* !CONFIG_OF */ |
| 2296 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2297 | static int stk3x1x_probe(struct i2c_client *client, |
| 2298 | const struct i2c_device_id *id) |
| 2299 | { |
| 2300 | int err = -ENODEV; |
| 2301 | struct stk3x1x_data *ps_data; |
| 2302 | struct stk3x1x_platform_data *plat_data; |
| 2303 | printk(KERN_INFO "%s: driver version = %s\n", __func__, DRIVER_VERSION); |
| 2304 | |
| 2305 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 2306 | { |
| 2307 | printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_BYTE_DATA\n", __func__); |
| 2308 | return -ENODEV; |
| 2309 | } |
| 2310 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) |
| 2311 | { |
| 2312 | printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_WORD_DATA\n", __func__); |
| 2313 | return -ENODEV; |
| 2314 | } |
| 2315 | |
| 2316 | ps_data = kzalloc(sizeof(struct stk3x1x_data),GFP_KERNEL); |
| 2317 | if(!ps_data) |
| 2318 | { |
| 2319 | printk(KERN_ERR "%s: failed to allocate stk3x1x_data\n", __func__); |
| 2320 | return -ENOMEM; |
| 2321 | } |
| 2322 | ps_data->client = client; |
| 2323 | i2c_set_clientdata(client,ps_data); |
| 2324 | mutex_init(&ps_data->io_lock); |
| 2325 | wake_lock_init(&ps_data->ps_wakelock,WAKE_LOCK_SUSPEND, "stk_input_wakelock"); |
| 2326 | |
| 2327 | #ifdef STK_POLL_PS |
| 2328 | wake_lock_init(&ps_data->ps_nosuspend_wl,WAKE_LOCK_SUSPEND, "stk_nosuspend_wakelock"); |
| 2329 | #endif |
Bingzhe Cai | 1ccffb7 | 2013-07-26 01:31:19 +0800 | [diff] [blame] | 2330 | if (client->dev.of_node) { |
| 2331 | plat_data = devm_kzalloc(&client->dev, |
| 2332 | sizeof(struct stk3x1x_platform_data), GFP_KERNEL); |
| 2333 | if (!plat_data) { |
| 2334 | dev_err(&client->dev, "Failed to allocate memory\n"); |
| 2335 | return -ENOMEM; |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2336 | } |
Bingzhe Cai | 1ccffb7 | 2013-07-26 01:31:19 +0800 | [diff] [blame] | 2337 | |
| 2338 | err = stk3x1x_parse_dt(&client->dev, plat_data); |
| 2339 | dev_err(&client->dev, |
| 2340 | "%s: stk3x1x_parse_dt ret=%d\n", __func__, err); |
| 2341 | if (err) |
| 2342 | return err; |
| 2343 | } else |
| 2344 | plat_data = client->dev.platform_data; |
| 2345 | |
| 2346 | if (!plat_data) { |
| 2347 | dev_err(&client->dev, |
| 2348 | "%s: no stk3x1x platform data!\n", __func__); |
| 2349 | goto err_als_input_allocate; |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2350 | } |
Bingzhe Cai | 1ccffb7 | 2013-07-26 01:31:19 +0800 | [diff] [blame] | 2351 | ps_data->als_transmittance = plat_data->transmittance; |
| 2352 | ps_data->int_pin = plat_data->int_pin; |
Bingzhe Cai | ab00b7d | 2013-08-23 13:51:56 +0800 | [diff] [blame] | 2353 | ps_data->use_fir = plat_data->use_fir; |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2354 | ps_data->pdata = plat_data; |
Bingzhe Cai | 1ccffb7 | 2013-07-26 01:31:19 +0800 | [diff] [blame] | 2355 | |
| 2356 | if (ps_data->als_transmittance == 0) { |
| 2357 | dev_err(&client->dev, |
| 2358 | "%s: Please set als_transmittance\n", __func__); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2359 | goto err_als_input_allocate; |
| 2360 | } |
| 2361 | |
| 2362 | ps_data->als_input_dev = input_allocate_device(); |
| 2363 | if (ps_data->als_input_dev==NULL) |
| 2364 | { |
| 2365 | printk(KERN_ERR "%s: could not allocate als device\n", __func__); |
| 2366 | err = -ENOMEM; |
| 2367 | goto err_als_input_allocate; |
| 2368 | } |
| 2369 | ps_data->ps_input_dev = input_allocate_device(); |
| 2370 | if (ps_data->ps_input_dev==NULL) |
| 2371 | { |
| 2372 | printk(KERN_ERR "%s: could not allocate ps device\n", __func__); |
| 2373 | err = -ENOMEM; |
| 2374 | goto err_ps_input_allocate; |
| 2375 | } |
| 2376 | ps_data->als_input_dev->name = ALS_NAME; |
| 2377 | ps_data->ps_input_dev->name = PS_NAME; |
| 2378 | set_bit(EV_ABS, ps_data->als_input_dev->evbit); |
| 2379 | set_bit(EV_ABS, ps_data->ps_input_dev->evbit); |
| 2380 | input_set_abs_params(ps_data->als_input_dev, ABS_MISC, 0, stk_alscode2lux(ps_data, (1<<16)-1), 0, 0); |
| 2381 | input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0,1, 0, 0); |
| 2382 | err = input_register_device(ps_data->als_input_dev); |
| 2383 | if (err<0) |
| 2384 | { |
| 2385 | printk(KERN_ERR "%s: can not register als input device\n", __func__); |
| 2386 | goto err_als_input_register; |
| 2387 | } |
| 2388 | err = input_register_device(ps_data->ps_input_dev); |
| 2389 | if (err<0) |
| 2390 | { |
| 2391 | printk(KERN_ERR "%s: can not register ps input device\n", __func__); |
| 2392 | goto err_ps_input_register; |
| 2393 | } |
| 2394 | |
| 2395 | err = sysfs_create_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group); |
| 2396 | if (err < 0) |
| 2397 | { |
| 2398 | printk(KERN_ERR "%s:could not create sysfs group for als\n", __func__); |
| 2399 | goto err_als_sysfs_create_group; |
| 2400 | } |
| 2401 | err = sysfs_create_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group); |
| 2402 | if (err < 0) |
| 2403 | { |
| 2404 | printk(KERN_ERR "%s:could not create sysfs group for ps\n", __func__); |
| 2405 | goto err_ps_sysfs_create_group; |
| 2406 | } |
| 2407 | input_set_drvdata(ps_data->als_input_dev, ps_data); |
| 2408 | input_set_drvdata(ps_data->ps_input_dev, ps_data); |
| 2409 | |
| 2410 | #ifdef STK_POLL_ALS |
| 2411 | ps_data->stk_als_wq = create_singlethread_workqueue("stk_als_wq"); |
| 2412 | INIT_WORK(&ps_data->stk_als_work, stk_als_work_func); |
| 2413 | hrtimer_init(&ps_data->als_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2414 | ps_data->als_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC); |
| 2415 | ps_data->als_timer.function = stk_als_timer_func; |
| 2416 | #endif |
| 2417 | |
| 2418 | ps_data->stk_ps_wq = create_singlethread_workqueue("stk_ps_wq"); |
| 2419 | INIT_WORK(&ps_data->stk_ps_work, stk_ps_work_func); |
| 2420 | hrtimer_init(&ps_data->ps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2421 | ps_data->ps_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC); |
| 2422 | ps_data->ps_timer.function = stk_ps_timer_func; |
| 2423 | #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS)) |
| 2424 | ps_data->stk_wq = create_singlethread_workqueue("stk_wq"); |
| 2425 | INIT_WORK(&ps_data->stk_work, stk_work_func); |
| 2426 | err = stk3x1x_setup_irq(client); |
| 2427 | if(err < 0) |
| 2428 | goto err_stk3x1x_setup_irq; |
| 2429 | #endif |
| 2430 | |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 2431 | err = stk3x1x_power_init(ps_data, true); |
| 2432 | if (err) |
| 2433 | goto err_power_init; |
| 2434 | |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2435 | err = stk3x1x_power_ctl(ps_data, true); |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 2436 | if (err) |
| 2437 | goto err_power_on; |
| 2438 | |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2439 | ps_data->als_enabled = false; |
| 2440 | ps_data->ps_enabled = false; |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2441 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 2442 | ps_data->stk_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; |
| 2443 | ps_data->stk_early_suspend.suspend = stk3x1x_early_suspend; |
| 2444 | ps_data->stk_early_suspend.resume = stk3x1x_late_resume; |
| 2445 | register_early_suspend(&ps_data->stk_early_suspend); |
| 2446 | #endif |
Jie Cheng | 246a936 | 2013-12-06 13:06:31 +0800 | [diff] [blame] | 2447 | /* make sure everything is ok before registering the class device */ |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 2448 | ps_data->als_cdev = sensors_light_cdev; |
| 2449 | ps_data->als_cdev.sensors_enable = stk_als_enable_set; |
| 2450 | ps_data->als_cdev.sensors_poll_delay = stk_als_poll_delay_set; |
| 2451 | err = sensors_classdev_register(&client->dev, &ps_data->als_cdev); |
Jie Cheng | 246a936 | 2013-12-06 13:06:31 +0800 | [diff] [blame] | 2452 | if (err) |
| 2453 | goto err_power_on; |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 2454 | |
| 2455 | ps_data->ps_cdev = sensors_proximity_cdev; |
| 2456 | ps_data->ps_cdev.sensors_enable = stk_ps_enable_set; |
| 2457 | err = sensors_classdev_register(&client->dev, &ps_data->ps_cdev); |
Jie Cheng | 246a936 | 2013-12-06 13:06:31 +0800 | [diff] [blame] | 2458 | if (err) |
| 2459 | goto err_class_sysfs; |
| 2460 | |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2461 | /* enable device power only when it is enabled */ |
| 2462 | err = stk3x1x_power_ctl(ps_data, false); |
| 2463 | if (err) |
| 2464 | goto err_init_all_setting; |
| 2465 | |
| 2466 | dev_dbg(&client->dev, "%s: probe successfully", __func__); |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2467 | return 0; |
| 2468 | |
| 2469 | err_init_all_setting: |
Bingzhe Cai | 271114e | 2013-10-18 18:35:09 +0800 | [diff] [blame] | 2470 | stk3x1x_power_ctl(ps_data, false); |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 2471 | sensors_classdev_unregister(&ps_data->ps_cdev); |
Jie Cheng | 246a936 | 2013-12-06 13:06:31 +0800 | [diff] [blame] | 2472 | err_class_sysfs: |
Jie Cheng | 45524a1 | 2013-12-27 14:24:08 +0800 | [diff] [blame] | 2473 | sensors_classdev_unregister(&ps_data->als_cdev); |
Bingzhe Cai | 2b32b50 | 2013-07-09 01:55:39 +0800 | [diff] [blame] | 2474 | err_power_on: |
| 2475 | stk3x1x_power_init(ps_data, false); |
| 2476 | err_power_init: |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2477 | #ifndef STK_POLL_PS |
| 2478 | free_irq(ps_data->irq, ps_data); |
| 2479 | gpio_free(plat_data->int_pin); |
| 2480 | #endif |
| 2481 | #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS)) |
| 2482 | err_stk3x1x_setup_irq: |
| 2483 | #endif |
| 2484 | #ifdef STK_POLL_ALS |
| 2485 | hrtimer_try_to_cancel(&ps_data->als_timer); |
| 2486 | destroy_workqueue(ps_data->stk_als_wq); |
| 2487 | #endif |
| 2488 | destroy_workqueue(ps_data->stk_ps_wq); |
| 2489 | #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS)) |
| 2490 | destroy_workqueue(ps_data->stk_wq); |
| 2491 | #endif |
| 2492 | sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group); |
| 2493 | err_ps_sysfs_create_group: |
| 2494 | sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group); |
| 2495 | err_als_sysfs_create_group: |
| 2496 | input_unregister_device(ps_data->ps_input_dev); |
| 2497 | err_ps_input_register: |
| 2498 | input_unregister_device(ps_data->als_input_dev); |
| 2499 | err_als_input_register: |
| 2500 | input_free_device(ps_data->ps_input_dev); |
| 2501 | err_ps_input_allocate: |
| 2502 | input_free_device(ps_data->als_input_dev); |
| 2503 | err_als_input_allocate: |
| 2504 | #ifdef STK_POLL_PS |
| 2505 | wake_lock_destroy(&ps_data->ps_nosuspend_wl); |
| 2506 | #endif |
| 2507 | wake_lock_destroy(&ps_data->ps_wakelock); |
| 2508 | mutex_destroy(&ps_data->io_lock); |
| 2509 | kfree(ps_data); |
| 2510 | return err; |
| 2511 | } |
| 2512 | |
| 2513 | |
| 2514 | static int stk3x1x_remove(struct i2c_client *client) |
| 2515 | { |
| 2516 | struct stk3x1x_data *ps_data = i2c_get_clientdata(client); |
| 2517 | #ifndef STK_POLL_PS |
| 2518 | free_irq(ps_data->irq, ps_data); |
| 2519 | gpio_free(ps_data->int_pin); |
| 2520 | #endif |
| 2521 | #ifdef STK_POLL_ALS |
| 2522 | hrtimer_try_to_cancel(&ps_data->als_timer); |
| 2523 | destroy_workqueue(ps_data->stk_als_wq); |
| 2524 | #endif |
| 2525 | destroy_workqueue(ps_data->stk_ps_wq); |
| 2526 | #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS)) |
| 2527 | destroy_workqueue(ps_data->stk_wq); |
| 2528 | #endif |
| 2529 | sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group); |
| 2530 | sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group); |
| 2531 | input_unregister_device(ps_data->ps_input_dev); |
| 2532 | input_unregister_device(ps_data->als_input_dev); |
| 2533 | input_free_device(ps_data->ps_input_dev); |
| 2534 | input_free_device(ps_data->als_input_dev); |
| 2535 | #ifdef STK_POLL_PS |
| 2536 | wake_lock_destroy(&ps_data->ps_nosuspend_wl); |
| 2537 | #endif |
| 2538 | wake_lock_destroy(&ps_data->ps_wakelock); |
| 2539 | mutex_destroy(&ps_data->io_lock); |
| 2540 | kfree(ps_data); |
| 2541 | |
| 2542 | return 0; |
| 2543 | } |
| 2544 | |
| 2545 | static const struct i2c_device_id stk_ps_id[] = |
| 2546 | { |
| 2547 | { "stk_ps", 0}, |
| 2548 | {} |
| 2549 | }; |
| 2550 | MODULE_DEVICE_TABLE(i2c, stk_ps_id); |
| 2551 | |
Bingzhe Cai | 1ccffb7 | 2013-07-26 01:31:19 +0800 | [diff] [blame] | 2552 | static struct of_device_id stk_match_table[] = { |
| 2553 | { .compatible = "stk,stk3x1x", }, |
| 2554 | { }, |
| 2555 | }; |
| 2556 | |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2557 | static struct i2c_driver stk_ps_driver = |
| 2558 | { |
| 2559 | .driver = { |
| 2560 | .name = DEVICE_NAME, |
| 2561 | .owner = THIS_MODULE, |
Bingzhe Cai | 1ccffb7 | 2013-07-26 01:31:19 +0800 | [diff] [blame] | 2562 | .of_match_table = stk_match_table, |
zzz | b7673aa | 2013-07-24 02:55:43 +0800 | [diff] [blame] | 2563 | }, |
| 2564 | .probe = stk3x1x_probe, |
| 2565 | .remove = stk3x1x_remove, |
| 2566 | .id_table = stk_ps_id, |
| 2567 | }; |
| 2568 | |
| 2569 | |
| 2570 | static int __init stk3x1x_init(void) |
| 2571 | { |
| 2572 | int ret; |
| 2573 | ret = i2c_add_driver(&stk_ps_driver); |
| 2574 | if (ret) |
| 2575 | return ret; |
| 2576 | |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
| 2580 | static void __exit stk3x1x_exit(void) |
| 2581 | { |
| 2582 | i2c_del_driver(&stk_ps_driver); |
| 2583 | } |
| 2584 | |
| 2585 | module_init(stk3x1x_init); |
| 2586 | module_exit(stk3x1x_exit); |
| 2587 | MODULE_AUTHOR("Lex Hsieh <lex_hsieh@sitronix.com.tw>"); |
| 2588 | MODULE_DESCRIPTION("Sensortek stk3x1x Proximity Sensor driver"); |
| 2589 | MODULE_LICENSE("GPL"); |
| 2590 | MODULE_VERSION(DRIVER_VERSION); |