blob: da30672d26a160ae7bdb22ddc75530828a454901 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* board-mahimahi-microp.c
2 * Copyright (C) 2009 Google.
3 * Copyright (C) 2009 HTC Corporation.
4 *
5 * The Microp on mahimahi is an i2c device that supports
6 * the following functions
7 * - LEDs (Green, Amber, Jogball backlight)
8 * - Lightsensor
9 * - Headset remotekeys
10 * - G-sensor
11 * - Interrupts
12 *
13 * This software is licensed under the terms of the GNU General Public
14 * License version 2, as published by the Free Software Foundation, and
15 * may be copied, distributed, and modified under those terms.
16 *
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#include <linux/kernel.h>
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/leds.h>
27#include <linux/workqueue.h>
28#include <linux/i2c.h>
29#include <linux/delay.h>
30#include <linux/gpio.h>
31#include <linux/miscdevice.h>
32#include <linux/input.h>
33#include <asm/uaccess.h>
34#include <linux/wakelock.h>
35#include <asm/mach-types.h>
36#include <mach/htc_pwrsink.h>
37#include <linux/earlysuspend.h>
38#include <linux/bma150.h>
39#include <linux/lightsensor.h>
40#include <asm/mach/mmc.h>
41#include <mach/htc_35mm_jack.h>
42#include <asm/setup.h>
43#include <linux/debugfs.h>
44#include <linux/seq_file.h>
45#include <linux/mutex.h>
46#include <linux/jiffies.h>
47
48#include "board-mahimahi.h"
49
50
51#define MICROP_I2C_NAME "mahimahi-microp"
52
53#define MICROP_LSENSOR_ADC_CHAN 6
54#define MICROP_REMOTE_KEY_ADC_CHAN 7
55
56#define MICROP_I2C_WCMD_MISC 0x20
57#define MICROP_I2C_WCMD_SPI_EN 0x21
58#define MICROP_I2C_WCMD_AUTO_BL_CTL 0x23
59#define MICROP_I2C_RCMD_SPI_BL_STATUS 0x24
60#define MICROP_I2C_WCMD_BUTTONS_LED_CTRL 0x25
61#define MICROP_I2C_RCMD_VERSION 0x30
62#define MICROP_I2C_WCMD_ADC_TABLE 0x42
63#define MICROP_I2C_WCMD_LED_MODE 0x53
64#define MICROP_I2C_RCMD_GREEN_LED_REMAIN_TIME 0x54
65#define MICROP_I2C_RCMD_AMBER_RED_LED_REMAIN_TIME 0x55
66#define MICROP_I2C_RCMD_BLUE_LED_REMAIN_TIME 0x57
67#define MICROP_I2C_WCMD_JOGBALL_LED_MODE 0x5A
68#define MICROP_I2C_RCMD_JOGBALL_LED_REMAIN_TIME 0x5B
69#define MICROP_I2C_WCMD_JOGBALL_LED_PWM_SET 0x5C
70#define MICROP_I2C_WCMD_JOGBALL_LED_PERIOD_SET 0x5D
71#define MICROP_I2C_WCMD_READ_ADC_VALUE_REQ 0x60
72#define MICROP_I2C_RCMD_ADC_VALUE 0x62
73#define MICROP_I2C_WCMD_REMOTEKEY_TABLE 0x63
74#define MICROP_I2C_WCMD_LCM_REGISTER 0x70
75#define MICROP_I2C_WCMD_GSENSOR_REG 0x73
76#define MICROP_I2C_WCMD_GSENSOR_REG_DATA_REQ 0x74
77#define MICROP_I2C_RCMD_GSENSOR_REG_DATA 0x75
78#define MICROP_I2C_WCMD_GSENSOR_DATA_REQ 0x76
79#define MICROP_I2C_RCMD_GSENSOR_X_DATA 0x77
80#define MICROP_I2C_RCMD_GSENSOR_Y_DATA 0x78
81#define MICROP_I2C_RCMD_GSENSOR_Z_DATA 0x79
82#define MICROP_I2C_RCMD_GSENSOR_DATA 0x7A
83#define MICROP_I2C_WCMD_OJ_REG 0x7B
84#define MICROP_I2C_WCMD_OJ_REG_DATA_REQ 0x7C
85#define MICROP_I2C_RCMD_OJ_REG_DATA 0x7D
86#define MICROP_I2C_WCMD_OJ_POS_DATA_REQ 0x7E
87#define MICROP_I2C_RCMD_OJ_POS_DATA 0x7F
88#define MICROP_I2C_WCMD_GPI_INT_CTL_EN 0x80
89#define MICROP_I2C_WCMD_GPI_INT_CTL_DIS 0x81
90#define MICROP_I2C_RCMD_GPI_INT_STATUS 0x82
91#define MICROP_I2C_RCMD_GPI_STATUS 0x83
92#define MICROP_I2C_WCMD_GPI_INT_STATUS_CLR 0x84
93#define MICROP_I2C_RCMD_GPI_INT_SETTING 0x85
94#define MICROP_I2C_RCMD_REMOTE_KEYCODE 0x87
95#define MICROP_I2C_WCMD_REMOTE_KEY_DEBN_TIME 0x88
96#define MICROP_I2C_WCMD_REMOTE_PLUG_DEBN_TIME 0x89
97#define MICROP_I2C_WCMD_SIMCARD_DEBN_TIME 0x8A
98#define MICROP_I2C_WCMD_GPO_LED_STATUS_EN 0x90
99#define MICROP_I2C_WCMD_GPO_LED_STATUS_DIS 0x91
100
101#define IRQ_GSENSOR (1<<10)
102#define IRQ_LSENSOR (1<<9)
103#define IRQ_REMOTEKEY (1<<7)
104#define IRQ_HEADSETIN (1<<2)
105#define IRQ_SDCARD (1<<0)
106
107#define READ_GPI_STATE_HPIN (1<<2)
108#define READ_GPI_STATE_SDCARD (1<<0)
109
110#define ALS_CALIBRATE_MODE 147
111
112/* Check pattern, to check if ALS has been calibrated */
113#define ALS_CALIBRATED 0x6DA5
114
115/* delay for deferred light sensor read */
116#define LS_READ_DELAY (HZ/2)
117
118/*#define DEBUG_BMA150 */
119#ifdef DEBUG_BMA150
120/* Debug logging of accelleration data */
121#define GSENSOR_LOG_MAX 2048 /* needs to be power of 2 */
122#define GSENSOR_LOG_MASK (GSENSOR_LOG_MAX - 1)
123
124struct gsensor_log {
125 ktime_t timestamp;
126 short x;
127 short y;
128 short z;
129};
130
131static DEFINE_MUTEX(gsensor_log_lock);
132static struct gsensor_log gsensor_log[GSENSOR_LOG_MAX];
133static unsigned gsensor_log_head;
134static unsigned gsensor_log_tail;
135
136void gsensor_log_status(ktime_t time, short x, short y, short z)
137{
138 unsigned n;
139 mutex_lock(&gsensor_log_lock);
140 n = gsensor_log_head;
141 gsensor_log[n].timestamp = time;
142 gsensor_log[n].x = x;
143 gsensor_log[n].y = y;
144 gsensor_log[n].z = z;
145 n = (n + 1) & GSENSOR_LOG_MASK;
146 if (n == gsensor_log_tail)
147 gsensor_log_tail = (gsensor_log_tail + 1) & GSENSOR_LOG_MASK;
148 gsensor_log_head = n;
149 mutex_unlock(&gsensor_log_lock);
150}
151
152static int gsensor_log_print(struct seq_file *sf, void *private)
153{
154 unsigned n;
155
156 mutex_lock(&gsensor_log_lock);
157 seq_printf(sf, "timestamp X Y Z\n");
158 for (n = gsensor_log_tail;
159 n != gsensor_log_head;
160 n = (n + 1) & GSENSOR_LOG_MASK) {
161 seq_printf(sf, "%10d.%010d %6d %6d %6d\n",
162 gsensor_log[n].timestamp.tv.sec,
163 gsensor_log[n].timestamp.tv.nsec,
164 gsensor_log[n].x, gsensor_log[n].y,
165 gsensor_log[n].z);
166 }
167 mutex_unlock(&gsensor_log_lock);
168 return 0;
169}
170
171static int gsensor_log_open(struct inode *inode, struct file *file)
172{
173 return single_open(file, gsensor_log_print, NULL);
174}
175
176static struct file_operations gsensor_log_fops = {
177 .open = gsensor_log_open,
178 .read = seq_read,
179 .llseek = seq_lseek,
180 .release = single_release,
181};
182#endif /* def DEBUG_BMA150 */
183
184static int microp_headset_has_mic(void);
185static int microp_enable_headset_plug_event(void);
186static int microp_enable_key_event(void);
187static int microp_disable_key_event(void);
188
189static struct h35mm_platform_data mahimahi_h35mm_data = {
190 .plug_event_enable = microp_enable_headset_plug_event,
191 .headset_has_mic = microp_headset_has_mic,
192 .key_event_enable = microp_enable_key_event,
193 .key_event_disable = microp_disable_key_event,
194};
195
196static struct platform_device mahimahi_h35mm = {
197 .name = "htc_headset",
198 .id = -1,
199 .dev = {
200 .platform_data = &mahimahi_h35mm_data,
201 },
202};
203
204enum led_type {
205 GREEN_LED,
206 AMBER_LED,
207 RED_LED,
208 BLUE_LED,
209 JOGBALL_LED,
210 BUTTONS_LED,
211 NUM_LEDS,
212};
213
214static uint16_t lsensor_adc_table[10] = {
215 0x000, 0x001, 0x00F, 0x01E, 0x03C, 0x121, 0x190, 0x2BA, 0x26E, 0x3FF
216};
217
218static uint16_t remote_key_adc_table[6] = {
219 0, 33, 43, 110, 129, 220
220};
221
222static uint32_t golden_adc = 0xC0;
223static uint32_t als_kadc;
224
225static struct wake_lock microp_i2c_wakelock;
226
227static struct i2c_client *private_microp_client;
228
229struct microp_int_pin {
230 uint16_t int_gsensor;
231 uint16_t int_lsensor;
232 uint16_t int_reset;
233 uint16_t int_simcard;
234 uint16_t int_hpin;
235 uint16_t int_remotekey;
236};
237
238struct microp_led_data {
239 int type;
240 struct led_classdev ldev;
241 struct mutex led_data_mutex;
242 struct work_struct brightness_work;
243 spinlock_t brightness_lock;
244 enum led_brightness brightness;
245 uint8_t mode;
246 uint8_t blink;
247};
248
249struct microp_i2c_work {
250 struct work_struct work;
251 struct i2c_client *client;
252 int (*intr_debounce)(uint8_t *pin_status);
253 void (*intr_function)(uint8_t *pin_status);
254};
255
256struct microp_i2c_client_data {
257 struct microp_led_data leds[NUM_LEDS];
258 uint16_t version;
259 struct microp_i2c_work work;
260 struct delayed_work hpin_debounce_work;
261 struct delayed_work ls_read_work;
262 struct early_suspend early_suspend;
263 uint8_t enable_early_suspend;
264 uint8_t enable_reset_button;
265 int microp_is_suspend;
266 int auto_backlight_enabled;
267 uint8_t light_sensor_enabled;
268 uint8_t force_light_sensor_read;
269 uint8_t button_led_value;
270 int headset_is_in;
271 int is_hpin_pin_stable;
272 struct input_dev *ls_input_dev;
273 uint32_t als_kadc;
274 uint32_t als_gadc;
275 uint8_t als_calibrating;
276};
277
278static char *hex2string(uint8_t *data, int len)
279{
280 static char buf[101];
281 int i;
282
283 i = (sizeof(buf) - 1) / 4;
284 if (len > i)
285 len = i;
286
287 for (i = 0; i < len; i++)
288 sprintf(buf + i * 4, "[%02X]", data[i]);
289
290 return buf;
291}
292
293#define I2C_READ_RETRY_TIMES 10
294#define I2C_WRITE_RETRY_TIMES 10
295
296static int i2c_read_block(struct i2c_client *client, uint8_t addr,
297 uint8_t *data, int length)
298{
299 int retry;
300 int ret;
301 struct i2c_msg msgs[] = {
302 {
303 .addr = client->addr,
304 .flags = 0,
305 .len = 1,
306 .buf = &addr,
307 },
308 {
309 .addr = client->addr,
310 .flags = I2C_M_RD,
311 .len = length,
312 .buf = data,
313 }
314 };
315
316 mdelay(1);
317 for (retry = 0; retry <= I2C_READ_RETRY_TIMES; retry++) {
318 ret = i2c_transfer(client->adapter, msgs, 2);
319 if (ret == 2) {
320 dev_dbg(&client->dev, "R [%02X] = %s\n", addr,
321 hex2string(data, length));
322 return 0;
323 }
324 msleep(10);
325 }
326
327 dev_err(&client->dev, "i2c_read_block retry over %d\n",
328 I2C_READ_RETRY_TIMES);
329 return -EIO;
330}
331
332#define MICROP_I2C_WRITE_BLOCK_SIZE 21
333static int i2c_write_block(struct i2c_client *client, uint8_t addr,
334 uint8_t *data, int length)
335{
336 int retry;
337 uint8_t buf[MICROP_I2C_WRITE_BLOCK_SIZE];
338 int ret;
339
340 struct i2c_msg msg[] = {
341 {
342 .addr = client->addr,
343 .flags = 0,
344 .len = length + 1,
345 .buf = buf,
346 }
347 };
348
349 dev_dbg(&client->dev, "W [%02X] = %s\n", addr,
350 hex2string(data, length));
351
352 if (length + 1 > MICROP_I2C_WRITE_BLOCK_SIZE) {
353 dev_err(&client->dev, "i2c_write_block length too long\n");
354 return -E2BIG;
355 }
356
357 buf[0] = addr;
358 memcpy((void *)&buf[1], (void *)data, length);
359
360 mdelay(1);
361 for (retry = 0; retry <= I2C_WRITE_RETRY_TIMES; retry++) {
362 ret = i2c_transfer(client->adapter, msg, 1);
363 if (ret == 1)
364 return 0;
365 msleep(10);
366 }
367 dev_err(&client->dev, "i2c_write_block retry over %d\n",
368 I2C_WRITE_RETRY_TIMES);
369 return -EIO;
370}
371
372static int microp_read_adc(uint8_t channel, uint16_t *value)
373{
374 struct i2c_client *client;
375 int ret;
376 uint8_t cmd[2], data[2];
377
378 client = private_microp_client;
379 cmd[0] = 0;
380 cmd[1] = channel;
381 ret = i2c_write_block(client, MICROP_I2C_WCMD_READ_ADC_VALUE_REQ,
382 cmd, 2);
383 if (ret < 0) {
384 dev_err(&client->dev, "%s: request adc fail\n", __func__);
385 return -EIO;
386 }
387
388 ret = i2c_read_block(client, MICROP_I2C_RCMD_ADC_VALUE, data, 2);
389 if (ret < 0) {
390 dev_err(&client->dev, "%s: read adc fail\n", __func__);
391 return -EIO;
392 }
393 *value = data[0] << 8 | data[1];
394 return 0;
395}
396
397static int microp_read_gpi_status(struct i2c_client *client, uint16_t *status)
398{
399 uint8_t data[2];
400 int ret;
401
402 ret = i2c_read_block(client, MICROP_I2C_RCMD_GPI_STATUS, data, 2);
403 if (ret < 0) {
404 dev_err(&client->dev, "%s: read failed\n", __func__);
405 return -EIO;
406 }
407 *status = (data[0] << 8) | data[1];
408 return 0;
409}
410
411static int microp_interrupt_enable(struct i2c_client *client,
412 uint16_t interrupt_mask)
413{
414 uint8_t data[2];
415 int ret = -1;
416
417 data[0] = interrupt_mask >> 8;
418 data[1] = interrupt_mask & 0xFF;
419 ret = i2c_write_block(client, MICROP_I2C_WCMD_GPI_INT_CTL_EN, data, 2);
420
421 if (ret < 0)
422 dev_err(&client->dev, "%s: enable 0x%x interrupt failed\n",
423 __func__, interrupt_mask);
424 return ret;
425}
426
427static int microp_interrupt_disable(struct i2c_client *client,
428 uint16_t interrupt_mask)
429{
430 uint8_t data[2];
431 int ret = -1;
432
433 data[0] = interrupt_mask >> 8;
434 data[1] = interrupt_mask & 0xFF;
435 ret = i2c_write_block(client, MICROP_I2C_WCMD_GPI_INT_CTL_DIS, data, 2);
436
437 if (ret < 0)
438 dev_err(&client->dev, "%s: disable 0x%x interrupt failed\n",
439 __func__, interrupt_mask);
440 return ret;
441}
442
443
444/*
445 * SD slot card-detect support
446 */
447static unsigned int sdslot_cd = 0;
448static void (*sdslot_status_cb)(int card_present, void *dev_id);
449static void *sdslot_mmc_dev;
450
451int mahimahi_microp_sdslot_status_register(
452 void (*cb)(int card_present, void *dev_id),
453 void *dev_id)
454{
455 if (sdslot_status_cb)
456 return -EBUSY;
457 sdslot_status_cb = cb;
458 sdslot_mmc_dev = dev_id;
459 return 0;
460}
461
462unsigned int mahimahi_microp_sdslot_status(struct device *dev)
463{
464 return sdslot_cd;
465}
466
467static void mahimahi_microp_sdslot_update_status(int status)
468{
469 sdslot_cd = !(status & READ_GPI_STATE_SDCARD);
470 if (sdslot_status_cb)
471 sdslot_status_cb(sdslot_cd, sdslot_mmc_dev);
472}
473
474/*
475 *Headset Support
476*/
477static void hpin_debounce_do_work(struct work_struct *work)
478{
479 uint16_t gpi_status = 0;
480 struct microp_i2c_client_data *cdata;
481 int insert = 0;
482 struct i2c_client *client;
483
484 client = private_microp_client;
485 cdata = i2c_get_clientdata(client);
486
487 microp_read_gpi_status(client, &gpi_status);
488 insert = (gpi_status & READ_GPI_STATE_HPIN) ? 0 : 1;
489 if (insert != cdata->headset_is_in) {
490 cdata->headset_is_in = insert;
491 pr_debug("headset %s\n", insert ? "inserted" : "removed");
492 htc_35mm_jack_plug_event(cdata->headset_is_in,
493 &cdata->is_hpin_pin_stable);
494 }
495}
496
497static int microp_enable_headset_plug_event(void)
498{
499 int ret;
500 struct i2c_client *client;
501 struct microp_i2c_client_data *cdata;
502 uint16_t stat;
503
504 client = private_microp_client;
505 cdata = i2c_get_clientdata(client);
506
507 /* enable microp interrupt to detect changes */
508 ret = microp_interrupt_enable(client, IRQ_HEADSETIN);
509 if (ret < 0) {
510 dev_err(&client->dev, "%s: failed to enable irqs\n",
511 __func__);
512 return 0;
513 }
514 /* see if headset state has changed */
515 microp_read_gpi_status(client, &stat);
516 stat = !(stat & READ_GPI_STATE_HPIN);
517 if(cdata->headset_is_in != stat) {
518 cdata->headset_is_in = stat;
519 pr_debug("Headset state changed\n");
520 htc_35mm_jack_plug_event(stat, &cdata->is_hpin_pin_stable);
521 }
522
523 return 1;
524}
525
526static int microp_headset_detect_mic(void)
527{
528 uint16_t data;
529
530 microp_read_adc(MICROP_REMOTE_KEY_ADC_CHAN, &data);
531 if (data >= 200)
532 return 1;
533 else
534 return 0;
535}
536
537static int microp_headset_has_mic(void)
538{
539 int mic1 = -1;
540 int mic2 = -1;
541 int count = 0;
542
543 mic2 = microp_headset_detect_mic();
544
545 /* debounce the detection wait until 2 consecutive read are equal */
546 while ((mic1 != mic2) && (count < 10)) {
547 mic1 = mic2;
548 msleep(600);
549 mic2 = microp_headset_detect_mic();
550 count++;
551 }
552
553 pr_info("%s: microphone (%d) %s\n", __func__, count,
554 mic1 ? "present" : "not present");
555
556 return mic1;
557}
558
559static int microp_enable_key_event(void)
560{
561 int ret;
562 struct i2c_client *client;
563
564 client = private_microp_client;
565
566 if (!is_cdma_version(system_rev))
567 gpio_set_value(MAHIMAHI_GPIO_35MM_KEY_INT_SHUTDOWN, 1);
568
569 /* turn on key interrupt */
570 /* enable microp interrupt to detect changes */
571 ret = microp_interrupt_enable(client, IRQ_REMOTEKEY);
572 if (ret < 0) {
573 dev_err(&client->dev, "%s: failed to enable irqs\n",
574 __func__);
575 return ret;
576 }
577 return 0;
578}
579
580static int microp_disable_key_event(void)
581{
582 int ret;
583 struct i2c_client *client;
584
585 client = private_microp_client;
586
587 /* shutdown key interrupt */
588 if (!is_cdma_version(system_rev))
589 gpio_set_value(MAHIMAHI_GPIO_35MM_KEY_INT_SHUTDOWN, 0);
590
591 /* disable microp interrupt to detect changes */
592 ret = microp_interrupt_disable(client, IRQ_REMOTEKEY);
593 if (ret < 0) {
594 dev_err(&client->dev, "%s: failed to disable irqs\n",
595 __func__);
596 return ret;
597 }
598 return 0;
599}
600
601static int get_remote_keycode(int *keycode)
602{
603 struct i2c_client *client = private_microp_client;
604 int ret;
605 uint8_t data[2];
606
607 ret = i2c_read_block(client, MICROP_I2C_RCMD_REMOTE_KEYCODE, data, 2);
608 if (ret < 0) {
609 dev_err(&client->dev, "%s: read remote keycode fail\n",
610 __func__);
611 return -EIO;
612 }
613 pr_debug("%s: key = 0x%x\n", __func__, data[1]);
614 if (!data[1]) {
615 *keycode = 0;
616 return 1; /* no keycode */
617 } else {
618 *keycode = data[1];
619 }
620 return 0;
621}
622
623static ssize_t microp_i2c_remotekey_adc_show(struct device *dev,
624 struct device_attribute *attr, char *buf)
625{
626 struct i2c_client *client;
627 uint16_t value;
628 int i, button = 0;
629 int ret;
630
631 client = to_i2c_client(dev);
632
633 microp_read_adc(MICROP_REMOTE_KEY_ADC_CHAN, &value);
634
635 for (i = 0; i < 3; i++) {
636 if ((value >= remote_key_adc_table[2 * i]) &&
637 (value <= remote_key_adc_table[2 * i + 1])) {
638 button = i + 1;
639 }
640
641 }
642
643 ret = sprintf(buf, "Remote Key[0x%03X] => button %d\n",
644 value, button);
645
646 return ret;
647}
648
649static DEVICE_ATTR(key_adc, 0644, microp_i2c_remotekey_adc_show, NULL);
650
651/*
652 * LED support
653*/
654static int microp_i2c_write_led_mode(struct i2c_client *client,
655 struct led_classdev *led_cdev,
656 uint8_t mode, uint16_t off_timer)
657{
658 struct microp_i2c_client_data *cdata;
659 struct microp_led_data *ldata;
660 uint8_t data[7];
661 int ret;
662
663 cdata = i2c_get_clientdata(client);
664 ldata = container_of(led_cdev, struct microp_led_data, ldev);
665
666
667 if (ldata->type == GREEN_LED) {
668 data[0] = 0x01;
669 data[1] = mode;
670 data[2] = off_timer >> 8;
671 data[3] = off_timer & 0xFF;
672 data[4] = 0x00;
673 data[5] = 0x00;
674 data[6] = 0x00;
675 } else if (ldata->type == AMBER_LED) {
676 data[0] = 0x02;
677 data[1] = 0x00;
678 data[2] = 0x00;
679 data[3] = 0x00;
680 data[4] = mode;
681 data[5] = off_timer >> 8;
682 data[6] = off_timer & 0xFF;
683 } else if (ldata->type == RED_LED) {
684 data[0] = 0x02;
685 data[1] = 0x00;
686 data[2] = 0x00;
687 data[3] = 0x00;
688 data[4] = mode? 5: 0;
689 data[5] = off_timer >> 8;
690 data[6] = off_timer & 0xFF;
691 } else if (ldata->type == BLUE_LED) {
692 data[0] = 0x04;
693 data[1] = mode;
694 data[2] = off_timer >> 8;
695 data[3] = off_timer & 0xFF;
696 data[4] = 0x00;
697 data[5] = 0x00;
698 data[6] = 0x00;
699 }
700
701 ret = i2c_write_block(client, MICROP_I2C_WCMD_LED_MODE, data, 7);
702 if (ret == 0) {
703 mutex_lock(&ldata->led_data_mutex);
704 if (mode > 1)
705 ldata->blink = mode;
706 else
707 ldata->mode = mode;
708 mutex_unlock(&ldata->led_data_mutex);
709 }
710 return ret;
711}
712
713static ssize_t microp_i2c_led_blink_show(struct device *dev,
714 struct device_attribute *attr, char *buf)
715{
716 struct led_classdev *led_cdev;
717 struct microp_led_data *ldata;
718 int ret;
719
720 led_cdev = (struct led_classdev *)dev_get_drvdata(dev);
721 ldata = container_of(led_cdev, struct microp_led_data, ldev);
722
723 mutex_lock(&ldata->led_data_mutex);
724 ret = sprintf(buf, "%d\n", ldata->blink ? ldata->blink - 1 : 0);
725 mutex_unlock(&ldata->led_data_mutex);
726
727 return ret;
728}
729
730static ssize_t microp_i2c_led_blink_store(struct device *dev,
731 struct device_attribute *attr,
732 const char *buf, size_t count)
733{
734 struct led_classdev *led_cdev;
735 struct microp_led_data *ldata;
736 struct i2c_client *client;
737 int val, ret;
738 uint8_t mode;
739
740 val = -1;
741 sscanf(buf, "%u", &val);
742
743 led_cdev = (struct led_classdev *)dev_get_drvdata(dev);
744 ldata = container_of(led_cdev, struct microp_led_data, ldev);
745 client = to_i2c_client(dev->parent);
746
747 mutex_lock(&ldata->led_data_mutex);
748 switch (val) {
749 case 0: /* stop flashing */
750 mode = ldata->mode;
751 ldata->blink = 0;
752 break;
753 case 1:
754 case 2:
755 case 3:
756 mode = val + 1;
757 break;
758
759 default:
760 mutex_unlock(&ldata->led_data_mutex);
761 return -EINVAL;
762 }
763 mutex_unlock(&ldata->led_data_mutex);
764
765 ret = microp_i2c_write_led_mode(client, led_cdev, mode, 0xffff);
766 if (ret)
767 dev_err(&client->dev, "%s set blink failed\n", led_cdev->name);
768
769 return count;
770}
771
772static DEVICE_ATTR(blink, 0644, microp_i2c_led_blink_show,
773 microp_i2c_led_blink_store);
774
775static ssize_t microp_i2c_led_off_timer_show(struct device *dev,
776 struct device_attribute *attr, char *buf)
777{
778 struct microp_i2c_client_data *cdata;
779 struct led_classdev *led_cdev;
780 struct microp_led_data *ldata;
781 struct i2c_client *client;
782 uint8_t data[2];
783 int ret, offtime;
784
785
786 led_cdev = (struct led_classdev *)dev_get_drvdata(dev);
787 ldata = container_of(led_cdev, struct microp_led_data, ldev);
788 client = to_i2c_client(dev->parent);
789 cdata = i2c_get_clientdata(client);
790
791 dev_dbg(&client->dev, "Getting %s remaining time\n", led_cdev->name);
792
793 if (ldata->type == GREEN_LED) {
794 ret = i2c_read_block(client,
795 MICROP_I2C_RCMD_GREEN_LED_REMAIN_TIME, data, 2);
796 } else if (ldata->type == AMBER_LED) {
797 ret = i2c_read_block(client,
798 MICROP_I2C_RCMD_AMBER_RED_LED_REMAIN_TIME,
799 data, 2);
800 } else if (ldata->type == RED_LED) {
801 ret = i2c_read_block(client,
802 MICROP_I2C_RCMD_AMBER_RED_LED_REMAIN_TIME,
803 data, 2);
804 } else if (ldata->type == BLUE_LED) {
805 ret = i2c_read_block(client,
806 MICROP_I2C_RCMD_BLUE_LED_REMAIN_TIME, data, 2);
807 } else {
808 dev_err(&client->dev, "Unknown led %s\n", ldata->ldev.name);
809 return -EINVAL;
810 }
811
812 if (ret) {
813 dev_err(&client->dev,
814 "%s get off_timer failed\n", led_cdev->name);
815 }
816 offtime = (int)((data[1] | data[0] << 8) * 2);
817
818 ret = sprintf(buf, "Time remains %d:%d\n", offtime / 60, offtime % 60);
819 return ret;
820}
821
822static ssize_t microp_i2c_led_off_timer_store(struct device *dev,
823 struct device_attribute *attr,
824 const char *buf, size_t count)
825{
826 struct led_classdev *led_cdev;
827 struct microp_led_data *ldata;
828 struct i2c_client *client;
829 int min, sec, ret;
830 uint16_t off_timer;
831
832 min = -1;
833 sec = -1;
834 sscanf(buf, "%d %d", &min, &sec);
835
836 if (min < 0 || min > 255)
837 return -EINVAL;
838 if (sec < 0 || sec > 255)
839 return -EINVAL;
840
841 led_cdev = (struct led_classdev *)dev_get_drvdata(dev);
842 ldata = container_of(led_cdev, struct microp_led_data, ldev);
843 client = to_i2c_client(dev->parent);
844
845 dev_dbg(&client->dev, "Setting %s off_timer to %d min %d sec\n",
846 led_cdev->name, min, sec);
847
848 if (!min && !sec)
849 off_timer = 0xFFFF;
850 else
851 off_timer = (min * 60 + sec) / 2;
852
853 ret = microp_i2c_write_led_mode(client, led_cdev,
854 ldata->mode, off_timer);
855 if (ret) {
856 dev_err(&client->dev,
857 "%s set off_timer %d min %d sec failed\n",
858 led_cdev->name, min, sec);
859 }
860 return count;
861}
862
863static DEVICE_ATTR(off_timer, 0644, microp_i2c_led_off_timer_show,
864 microp_i2c_led_off_timer_store);
865
866static ssize_t microp_i2c_jogball_color_store(struct device *dev,
867 struct device_attribute *attr,
868 const char *buf, size_t count)
869{
870 struct led_classdev *led_cdev;
871 struct microp_led_data *ldata;
872 struct i2c_client *client;
873 int rpwm, gpwm, bpwm, ret;
874 uint8_t data[4];
875
876 rpwm = -1;
877 gpwm = -1;
878 bpwm = -1;
879 sscanf(buf, "%d %d %d", &rpwm, &gpwm, &bpwm);
880
881 if (rpwm < 0 || rpwm > 255)
882 return -EINVAL;
883 if (gpwm < 0 || gpwm > 255)
884 return -EINVAL;
885 if (bpwm < 0 || bpwm > 255)
886 return -EINVAL;
887
888 led_cdev = (struct led_classdev *)dev_get_drvdata(dev);
889 ldata = container_of(led_cdev, struct microp_led_data, ldev);
890 client = to_i2c_client(dev->parent);
891
892 dev_dbg(&client->dev, "Setting %s color to R=%d, G=%d, B=%d\n",
893 led_cdev->name, rpwm, gpwm, bpwm);
894
895 data[0] = rpwm;
896 data[1] = gpwm;
897 data[2] = bpwm;
898 data[3] = 0x00;
899
900 ret = i2c_write_block(client, MICROP_I2C_WCMD_JOGBALL_LED_PWM_SET,
901 data, 4);
902 if (ret) {
903 dev_err(&client->dev,
904 "%s set color R=%d G=%d B=%d failed\n",
905 led_cdev->name, rpwm, gpwm, bpwm);
906 }
907 return count;
908}
909
910static DEVICE_ATTR(color, 0644, NULL, microp_i2c_jogball_color_store);
911
912static ssize_t microp_i2c_jogball_period_store(struct device *dev,
913 struct device_attribute *attr,
914 const char *buf, size_t count)
915{
916 struct led_classdev *led_cdev;
917 struct microp_led_data *ldata;
918 struct i2c_client *client;
919 int period = -1;
920 int ret;
921 uint8_t data[4];
922
923 sscanf(buf, "%d", &period);
924
925 if (period < 2 || period > 12)
926 return -EINVAL;
927
928 led_cdev = (struct led_classdev *)dev_get_drvdata(dev);
929 ldata = container_of(led_cdev, struct microp_led_data, ldev);
930 client = to_i2c_client(dev->parent);
931
932 dev_info(&client->dev, "Setting Jogball flash period to %d\n", period);
933
934 data[0] = 0x00;
935 data[1] = period;
936
937 ret = i2c_write_block(client, MICROP_I2C_WCMD_JOGBALL_LED_PERIOD_SET,
938 data, 2);
939 if (ret) {
940 dev_err(&client->dev, "%s set period=%d failed\n",
941 led_cdev->name, period);
942 }
943 return count;
944}
945
946static DEVICE_ATTR(period, 0644, NULL, microp_i2c_jogball_period_store);
947
948static void microp_brightness_set(struct led_classdev *led_cdev,
949 enum led_brightness brightness)
950{
951 unsigned long flags;
952 struct i2c_client *client = to_i2c_client(led_cdev->dev->parent);
953 struct microp_led_data *ldata =
954 container_of(led_cdev, struct microp_led_data, ldev);
955
956 dev_dbg(&client->dev, "Setting %s brightness current %d new %d\n",
957 led_cdev->name, led_cdev->brightness, brightness);
958
959 if (brightness > 255)
960 brightness = 255;
961 led_cdev->brightness = brightness;
962
963 spin_lock_irqsave(&ldata->brightness_lock, flags);
964 ldata->brightness = brightness;
965 spin_unlock_irqrestore(&ldata->brightness_lock, flags);
966
967 schedule_work(&ldata->brightness_work);
968}
969
970static void microp_led_brightness_set_work(struct work_struct *work)
971{
972 unsigned long flags;
973 struct microp_led_data *ldata =
974 container_of(work, struct microp_led_data, brightness_work);
975 struct led_classdev *led_cdev = &ldata->ldev;
976
977 struct i2c_client *client = to_i2c_client(led_cdev->dev->parent);
978
979 enum led_brightness brightness;
980 int ret;
981 uint8_t mode;
982
983 spin_lock_irqsave(&ldata->brightness_lock, flags);
984 brightness = ldata->brightness;
985 spin_unlock_irqrestore(&ldata->brightness_lock, flags);
986
987 if (brightness)
988 mode = 1;
989 else
990 mode = 0;
991
992 ret = microp_i2c_write_led_mode(client, led_cdev, mode, 0xffff);
993 if (ret) {
994 dev_err(&client->dev,
995 "led_brightness_set failed to set mode\n");
996 }
997}
998
999struct device_attribute *green_amber_attrs[] = {
1000 &dev_attr_blink,
1001 &dev_attr_off_timer,
1002};
1003
1004struct device_attribute *jogball_attrs[] = {
1005 &dev_attr_color,
1006 &dev_attr_period,
1007};
1008
1009static void microp_led_buttons_brightness_set_work(struct work_struct *work)
1010{
1011
1012 unsigned long flags;
1013 struct microp_led_data *ldata =
1014 container_of(work, struct microp_led_data, brightness_work);
1015 struct led_classdev *led_cdev = &ldata->ldev;
1016
1017 struct i2c_client *client = to_i2c_client(led_cdev->dev->parent);
1018 struct microp_i2c_client_data *cdata = i2c_get_clientdata(client);
1019
1020
1021 uint8_t data[4] = {0, 0, 0};
1022 int ret = 0;
1023 enum led_brightness brightness;
1024 uint8_t value;
1025
1026
1027 spin_lock_irqsave(&ldata->brightness_lock, flags);
1028 brightness = ldata->brightness;
1029 spin_unlock_irqrestore(&ldata->brightness_lock, flags);
1030
1031 value = brightness >= 255 ? 0x20 : 0;
1032
1033 /* avoid a flicker that can occur when writing the same value */
1034 if (cdata->button_led_value == value)
1035 return;
1036 cdata->button_led_value = value;
1037
1038 /* in 40ms */
1039 data[0] = 0x05;
1040 /* duty cycle 0-255 */
1041 data[1] = value;
1042 /* bit2 == change brightness */
1043 data[3] = 0x04;
1044
1045 ret = i2c_write_block(client, MICROP_I2C_WCMD_BUTTONS_LED_CTRL,
1046 data, 4);
1047 if (ret < 0)
1048 dev_err(&client->dev, "%s failed on set buttons\n", __func__);
1049}
1050
1051static void microp_led_jogball_brightness_set_work(struct work_struct *work)
1052{
1053 unsigned long flags;
1054 struct microp_led_data *ldata =
1055 container_of(work, struct microp_led_data, brightness_work);
1056 struct led_classdev *led_cdev = &ldata->ldev;
1057
1058 struct i2c_client *client = to_i2c_client(led_cdev->dev->parent);
1059 uint8_t data[3] = {0, 0, 0};
1060 int ret = 0;
1061 enum led_brightness brightness;
1062
1063 spin_lock_irqsave(&ldata->brightness_lock, flags);
1064 brightness = ldata->brightness;
1065 spin_unlock_irqrestore(&ldata->brightness_lock, flags);
1066
1067 switch (brightness) {
1068 case 0:
1069 data[0] = 0;
1070 break;
1071 case 3:
1072 data[0] = 1;
1073 data[1] = data[2] = 0xFF;
1074 break;
1075 case 7:
1076 data[0] = 2;
1077 data[1] = 0;
1078 data[2] = 60;
1079 break;
1080 default:
1081 dev_warn(&client->dev, "%s: unknown value: %d\n",
1082 __func__, brightness);
1083 break;
1084 }
1085 ret = i2c_write_block(client, MICROP_I2C_WCMD_JOGBALL_LED_MODE,
1086 data, 3);
1087 if (ret < 0)
1088 dev_err(&client->dev, "%s failed on set jogball mode:0x%2.2X\n",
1089 __func__, data[0]);
1090}
1091
1092/*
1093 * Light Sensor Support
1094 */
1095static int microp_i2c_auto_backlight_mode(struct i2c_client *client,
1096 uint8_t enabled)
1097{
1098 uint8_t data[2];
1099 int ret = 0;
1100
1101 data[0] = 0;
1102 if (enabled)
1103 data[1] = 1;
1104 else
1105 data[1] = 0;
1106
1107 ret = i2c_write_block(client, MICROP_I2C_WCMD_AUTO_BL_CTL, data, 2);
1108 if (ret != 0)
1109 pr_err("%s: set auto light sensor fail\n", __func__);
1110
1111 return ret;
1112}
1113
1114static int lightsensor_enable(void)
1115{
1116 struct i2c_client *client;
1117 struct microp_i2c_client_data *cdata;
1118 int ret;
1119
1120 client = private_microp_client;
1121 cdata = i2c_get_clientdata(client);
1122
1123 if (cdata->microp_is_suspend) {
1124 pr_err("%s: abort, uP is going to suspend after #\n",
1125 __func__);
1126 return -EIO;
1127 }
1128
1129 disable_irq(client->irq);
1130 ret = microp_i2c_auto_backlight_mode(client, 1);
1131 if (ret < 0) {
1132 pr_err("%s: set auto light sensor fail\n", __func__);
1133 enable_irq(client->irq);
1134 return ret;
1135 }
1136
1137 cdata->auto_backlight_enabled = 1;
1138 /* TEMPORARY HACK: schedule a deferred light sensor read
1139 * to work around sensor manager race condition
1140 */
1141 schedule_delayed_work(&cdata->ls_read_work, LS_READ_DELAY);
1142 schedule_work(&cdata->work.work);
1143
1144 return 0;
1145}
1146
1147static int lightsensor_disable(void)
1148{
1149 /* update trigger data when done */
1150 struct i2c_client *client;
1151 struct microp_i2c_client_data *cdata;
1152 int ret;
1153
1154 client = private_microp_client;
1155 cdata = i2c_get_clientdata(client);
1156
1157 if (cdata->microp_is_suspend) {
1158 pr_err("%s: abort, uP is going to suspend after #\n",
1159 __func__);
1160 return -EIO;
1161 }
1162
1163 cancel_delayed_work(&cdata->ls_read_work);
1164
1165 ret = microp_i2c_auto_backlight_mode(client, 0);
1166 if (ret < 0)
1167 pr_err("%s: disable auto light sensor fail\n",
1168 __func__);
1169 else
1170 cdata->auto_backlight_enabled = 0;
1171 return 0;
1172}
1173
1174static int microp_lightsensor_read(uint16_t *adc_value,
1175 uint8_t *adc_level)
1176{
1177 struct i2c_client *client;
1178 struct microp_i2c_client_data *cdata;
1179 uint8_t i;
1180 int ret;
1181
1182 client = private_microp_client;
1183 cdata = i2c_get_clientdata(client);
1184
1185 ret = microp_read_adc(MICROP_LSENSOR_ADC_CHAN, adc_value);
1186 if (ret != 0)
1187 return -1;
1188
1189 if (*adc_value > 0x3FF) {
1190 pr_warning("%s: get wrong value: 0x%X\n",
1191 __func__, *adc_value);
1192 return -1;
1193 } else {
1194 if (!cdata->als_calibrating) {
1195 *adc_value = *adc_value
1196 * cdata->als_gadc / cdata->als_kadc;
1197 if (*adc_value > 0x3FF)
1198 *adc_value = 0x3FF;
1199 }
1200
1201 *adc_level = ARRAY_SIZE(lsensor_adc_table) - 1;
1202 for (i = 0; i < ARRAY_SIZE(lsensor_adc_table); i++) {
1203 if (*adc_value <= lsensor_adc_table[i]) {
1204 *adc_level = i;
1205 break;
1206 }
1207 }
1208 pr_debug("%s: ADC value: 0x%X, level: %d #\n",
1209 __func__, *adc_value, *adc_level);
1210 }
1211
1212 return 0;
1213}
1214
1215static ssize_t microp_i2c_lightsensor_adc_show(struct device *dev,
1216 struct device_attribute *attr, char *buf)
1217{
1218 uint8_t adc_level = 0;
1219 uint16_t adc_value = 0;
1220 int ret;
1221
1222 ret = microp_lightsensor_read(&adc_value, &adc_level);
1223
1224 ret = sprintf(buf, "ADC[0x%03X] => level %d\n", adc_value, adc_level);
1225
1226 return ret;
1227}
1228
1229static DEVICE_ATTR(ls_adc, 0644, microp_i2c_lightsensor_adc_show, NULL);
1230
1231static ssize_t microp_i2c_ls_auto_show(struct device *dev,
1232 struct device_attribute *attr, char *buf)
1233{
1234 struct i2c_client *client;
1235 uint8_t data[2] = {0, 0};
1236 int ret;
1237
1238 client = to_i2c_client(dev);
1239
1240 i2c_read_block(client, MICROP_I2C_RCMD_SPI_BL_STATUS, data, 2);
1241 ret = sprintf(buf, "Light sensor Auto = %d, SPI enable = %d\n",
1242 data[0], data[1]);
1243
1244 return ret;
1245}
1246
1247static ssize_t microp_i2c_ls_auto_store(struct device *dev,
1248 struct device_attribute *attr,
1249 const char *buf, size_t count)
1250{
1251 struct i2c_client *client;
1252 struct microp_i2c_client_data *cdata;
1253 uint8_t enable = 0;
1254 int ls_auto;
1255
1256 ls_auto = -1;
1257 sscanf(buf, "%d", &ls_auto);
1258
1259 if (ls_auto != 0 && ls_auto != 1 && ls_auto != ALS_CALIBRATE_MODE)
1260 return -EINVAL;
1261
1262 client = to_i2c_client(dev);
1263 cdata = i2c_get_clientdata(client);
1264
1265 if (ls_auto) {
1266 enable = 1;
1267 cdata->als_calibrating = (ls_auto == ALS_CALIBRATE_MODE) ? 1 : 0;
1268 cdata->auto_backlight_enabled = 1;
1269 } else {
1270 enable = 0;
1271 cdata->als_calibrating = 0;
1272 cdata->auto_backlight_enabled = 0;
1273 }
1274
1275 microp_i2c_auto_backlight_mode(client, enable);
1276
1277 return count;
1278}
1279
1280static DEVICE_ATTR(ls_auto, 0644, microp_i2c_ls_auto_show,
1281 microp_i2c_ls_auto_store);
1282
1283DEFINE_MUTEX(api_lock);
1284static int lightsensor_opened;
1285
1286static int lightsensor_open(struct inode *inode, struct file *file)
1287{
1288 int rc = 0;
1289 pr_debug("%s\n", __func__);
1290 mutex_lock(&api_lock);
1291 if (lightsensor_opened) {
1292 pr_err("%s: already opened\n", __func__);
1293 rc = -EBUSY;
1294 }
1295 lightsensor_opened = 1;
1296 mutex_unlock(&api_lock);
1297 return rc;
1298}
1299
1300static int lightsensor_release(struct inode *inode, struct file *file)
1301{
1302 pr_debug("%s\n", __func__);
1303 mutex_lock(&api_lock);
1304 lightsensor_opened = 0;
1305 mutex_unlock(&api_lock);
1306 return 0;
1307}
1308
1309static long lightsensor_ioctl(struct file *file, unsigned int cmd,
1310 unsigned long arg)
1311{
1312 int rc, val;
1313 struct i2c_client *client;
1314 struct microp_i2c_client_data *cdata;
1315
1316 mutex_lock(&api_lock);
1317
1318 client = private_microp_client;
1319 cdata = i2c_get_clientdata(client);
1320
1321 pr_debug("%s cmd %d\n", __func__, _IOC_NR(cmd));
1322
1323 switch (cmd) {
1324 case LIGHTSENSOR_IOCTL_ENABLE:
1325 if (get_user(val, (unsigned long __user *)arg)) {
1326 rc = -EFAULT;
1327 break;
1328 }
1329 rc = val ? lightsensor_enable() : lightsensor_disable();
1330 break;
1331 case LIGHTSENSOR_IOCTL_GET_ENABLED:
1332 val = cdata->auto_backlight_enabled;
1333 pr_debug("%s enabled %d\n", __func__, val);
1334 rc = put_user(val, (unsigned long __user *)arg);
1335 break;
1336 default:
1337 pr_err("%s: invalid cmd %d\n", __func__, _IOC_NR(cmd));
1338 rc = -EINVAL;
1339 }
1340
1341 mutex_unlock(&api_lock);
1342 return rc;
1343}
1344
1345static struct file_operations lightsensor_fops = {
1346 .owner = THIS_MODULE,
1347 .open = lightsensor_open,
1348 .release = lightsensor_release,
1349 .unlocked_ioctl = lightsensor_ioctl
1350};
1351
1352struct miscdevice lightsensor_misc = {
1353 .minor = MISC_DYNAMIC_MINOR,
1354 .name = "lightsensor",
1355 .fops = &lightsensor_fops
1356};
1357
1358/*
1359 * G-sensor
1360 */
1361static int microp_spi_enable(uint8_t on)
1362{
1363 struct i2c_client *client;
1364 int ret;
1365
1366 client = private_microp_client;
1367 ret = i2c_write_block(client, MICROP_I2C_WCMD_SPI_EN, &on, 1);
1368 if (ret < 0) {
1369 dev_err(&client->dev,"%s: i2c_write_block fail\n", __func__);
1370 return ret;
1371 }
1372 msleep(10);
1373 return ret;
1374}
1375
1376static int gsensor_read_reg(uint8_t reg, uint8_t *data)
1377{
1378 struct i2c_client *client;
1379 int ret;
1380 uint8_t tmp[2];
1381
1382 client = private_microp_client;
1383 ret = i2c_write_block(client, MICROP_I2C_WCMD_GSENSOR_REG_DATA_REQ,
1384 &reg, 1);
1385 if (ret < 0) {
1386 dev_err(&client->dev,"%s: i2c_write_block fail\n", __func__);
1387 return ret;
1388 }
1389 msleep(10);
1390
1391 ret = i2c_read_block(client, MICROP_I2C_RCMD_GSENSOR_REG_DATA, tmp, 2);
1392 if (ret < 0) {
1393 dev_err(&client->dev,"%s: i2c_read_block fail\n", __func__);
1394 return ret;
1395 }
1396 *data = tmp[1];
1397 return ret;
1398}
1399
1400static int gsensor_write_reg(uint8_t reg, uint8_t data)
1401{
1402 struct i2c_client *client;
1403 int ret;
1404 uint8_t tmp[2];
1405
1406 client = private_microp_client;
1407
1408 tmp[0] = reg;
1409 tmp[1] = data;
1410 ret = i2c_write_block(client, MICROP_I2C_WCMD_GSENSOR_REG, tmp, 2);
1411 if (ret < 0) {
1412 dev_err(&client->dev,"%s: i2c_write_block fail\n", __func__);
1413 return ret;
1414 }
1415
1416 return ret;
1417}
1418
1419static int gsensor_read_acceleration(short *buf)
1420{
1421 struct i2c_client *client;
1422 int ret;
1423 uint8_t tmp[6];
1424 struct microp_i2c_client_data *cdata;
1425
1426 client = private_microp_client;
1427
1428 cdata = i2c_get_clientdata(client);
1429
1430 tmp[0] = 1;
1431 ret = i2c_write_block(client, MICROP_I2C_WCMD_GSENSOR_DATA_REQ,
1432 tmp, 1);
1433 if (ret < 0) {
1434 dev_err(&client->dev,"%s: i2c_write_block fail\n", __func__);
1435 return ret;
1436 }
1437
1438 msleep(10);
1439
1440 if (cdata->version <= 0x615) {
1441 /*
1442 * Note the data is a 10bit signed value from the chip.
1443 */
1444 ret = i2c_read_block(client, MICROP_I2C_RCMD_GSENSOR_X_DATA,
1445 tmp, 2);
1446 if (ret < 0) {
1447 dev_err(&client->dev, "%s: i2c_read_block fail\n",
1448 __func__);
1449 return ret;
1450 }
1451 buf[0] = (short)(tmp[0] << 8 | tmp[1]);
1452 buf[0] >>= 6;
1453
1454 ret = i2c_read_block(client, MICROP_I2C_RCMD_GSENSOR_Y_DATA,
1455 tmp, 2);
1456 if (ret < 0) {
1457 dev_err(&client->dev, "%s: i2c_read_block fail\n",
1458 __func__);
1459 return ret;
1460 }
1461 buf[1] = (short)(tmp[0] << 8 | tmp[1]);
1462 buf[1] >>= 6;
1463
1464 ret = i2c_read_block(client, MICROP_I2C_RCMD_GSENSOR_Z_DATA,
1465 tmp, 2);
1466 if (ret < 0) {
1467 dev_err(&client->dev, "%s: i2c_read_block fail\n",
1468 __func__);
1469 return ret;
1470 }
1471 buf[2] = (short)(tmp[0] << 8 | tmp[1]);
1472 buf[2] >>= 6;
1473 } else {
1474 ret = i2c_read_block(client, MICROP_I2C_RCMD_GSENSOR_DATA,
1475 tmp, 6);
1476 if (ret < 0) {
1477 dev_err(&client->dev, "%s: i2c_read_block fail\n",
1478 __func__);
1479 return ret;
1480 }
1481 buf[0] = (short)(tmp[0] << 8 | tmp[1]);
1482 buf[0] >>= 6;
1483 buf[1] = (short)(tmp[2] << 8 | tmp[3]);
1484 buf[1] >>= 6;
1485 buf[2] = (short)(tmp[4] << 8 | tmp[5]);
1486 buf[2] >>= 6;
1487 }
1488
1489#ifdef DEBUG_BMA150
1490 /* Log this to debugfs */
1491 gsensor_log_status(ktime_get(), buf[0], buf[1], buf[2]);
1492#endif
1493 return 1;
1494}
1495
1496static int gsensor_init_hw(void)
1497{
1498 uint8_t reg;
1499 int ret;
1500
1501 pr_debug("%s\n", __func__);
1502
1503 microp_spi_enable(1);
1504
1505 ret = gsensor_read_reg(RANGE_BWIDTH_REG, &reg);
1506 if (ret < 0 )
1507 return -EIO;
1508 reg &= 0xe0;
1509 ret = gsensor_write_reg(RANGE_BWIDTH_REG, reg);
1510 if (ret < 0 )
1511 return -EIO;
1512
1513 ret = gsensor_read_reg(SMB150_CONF2_REG, &reg);
1514 if (ret < 0 )
1515 return -EIO;
1516 reg |= (1 << 3);
1517 ret = gsensor_write_reg(SMB150_CONF2_REG, reg);
1518
1519 return ret;
1520}
1521
1522static int bma150_set_mode(char mode)
1523{
1524 uint8_t reg;
1525 int ret;
1526
1527 pr_debug("%s mode = %d\n", __func__, mode);
1528 if (mode == BMA_MODE_NORMAL)
1529 microp_spi_enable(1);
1530
1531
1532 ret = gsensor_read_reg(SMB150_CTRL_REG, &reg);
1533 if (ret < 0 )
1534 return -EIO;
1535 reg = (reg & 0xfe) | mode;
1536 ret = gsensor_write_reg(SMB150_CTRL_REG, reg);
1537
1538 if (mode == BMA_MODE_SLEEP)
1539 microp_spi_enable(0);
1540
1541 return ret;
1542}
1543static int gsensor_read(uint8_t *data)
1544{
1545 int ret;
1546 uint8_t reg = data[0];
1547
1548 ret = gsensor_read_reg(reg, &data[1]);
1549 pr_debug("%s reg = %x data = %x\n", __func__, reg, data[1]);
1550 return ret;
1551}
1552
1553static int gsensor_write(uint8_t *data)
1554{
1555 int ret;
1556 uint8_t reg = data[0];
1557
1558 pr_debug("%s reg = %x data = %x\n", __func__, reg, data[1]);
1559 ret = gsensor_write_reg(reg, data[1]);
1560 return ret;
1561}
1562
1563static int bma150_open(struct inode *inode, struct file *file)
1564{
1565 pr_debug("%s\n", __func__);
1566 return nonseekable_open(inode, file);
1567}
1568
1569static int bma150_release(struct inode *inode, struct file *file)
1570{
1571 return 0;
1572}
1573
1574static int bma150_ioctl(struct inode *inode, struct file *file,
1575 unsigned int cmd, unsigned long arg)
1576{
1577 void __user *argp = (void __user *)arg;
1578 char rwbuf[8];
1579 int ret = -1;
1580 short buf[8], temp;
1581
1582 switch (cmd) {
1583 case BMA_IOCTL_READ:
1584 case BMA_IOCTL_WRITE:
1585 case BMA_IOCTL_SET_MODE:
1586 if (copy_from_user(&rwbuf, argp, sizeof(rwbuf)))
1587 return -EFAULT;
1588 break;
1589 case BMA_IOCTL_READ_ACCELERATION:
1590 if (copy_from_user(&buf, argp, sizeof(buf)))
1591 return -EFAULT;
1592 break;
1593 default:
1594 break;
1595 }
1596
1597 switch (cmd) {
1598 case BMA_IOCTL_INIT:
1599 ret = gsensor_init_hw();
1600 if (ret < 0)
1601 return ret;
1602 break;
1603
1604 case BMA_IOCTL_READ:
1605 if (rwbuf[0] < 1)
1606 return -EINVAL;
1607 ret = gsensor_read(rwbuf);
1608 if (ret < 0)
1609 return ret;
1610 break;
1611 case BMA_IOCTL_WRITE:
1612 if (rwbuf[0] < 2)
1613 return -EINVAL;
1614 ret = gsensor_write(rwbuf);
1615 if (ret < 0)
1616 return ret;
1617 break;
1618 case BMA_IOCTL_READ_ACCELERATION:
1619 ret = gsensor_read_acceleration(&buf[0]);
1620 if (ret < 0)
1621 return ret;
1622 break;
1623 case BMA_IOCTL_SET_MODE:
1624 bma150_set_mode(rwbuf[0]);
1625 break;
1626 case BMA_IOCTL_GET_INT:
1627 temp = 0;
1628 break;
1629 default:
1630 return -ENOTTY;
1631 }
1632
1633 switch (cmd) {
1634 case BMA_IOCTL_READ:
1635 if (copy_to_user(argp, &rwbuf, sizeof(rwbuf)))
1636 return -EFAULT;
1637 break;
1638 case BMA_IOCTL_READ_ACCELERATION:
1639 if (copy_to_user(argp, &buf, sizeof(buf)))
1640 return -EFAULT;
1641 break;
1642 case BMA_IOCTL_GET_INT:
1643 if (copy_to_user(argp, &temp, sizeof(temp)))
1644 return -EFAULT;
1645 break;
1646 default:
1647 break;
1648 }
1649
1650 return 0;
1651}
1652
1653static struct file_operations bma_fops = {
1654 .owner = THIS_MODULE,
1655 .open = bma150_open,
1656 .release = bma150_release,
1657 .ioctl = bma150_ioctl,
1658};
1659
1660static struct miscdevice spi_bma_device = {
1661 .minor = MISC_DYNAMIC_MINOR,
1662 .name = BMA150_G_SENSOR_NAME,
1663 .fops = &bma_fops,
1664};
1665
1666/*
1667 * Interrupt
1668 */
1669static irqreturn_t microp_i2c_intr_irq_handler(int irq, void *dev_id)
1670{
1671 struct i2c_client *client;
1672 struct microp_i2c_client_data *cdata;
1673
1674 client = to_i2c_client(dev_id);
1675 cdata = i2c_get_clientdata(client);
1676
1677 dev_dbg(&client->dev, "intr_irq_handler\n");
1678
1679 disable_irq_nosync(client->irq);
1680 schedule_work(&cdata->work.work);
1681 return IRQ_HANDLED;
1682}
1683
1684static void microp_i2c_intr_work_func(struct work_struct *work)
1685{
1686 struct microp_i2c_work *up_work;
1687 struct i2c_client *client;
1688 struct microp_i2c_client_data *cdata;
1689 uint8_t data[3], adc_level;
1690 uint16_t intr_status = 0, adc_value, gpi_status = 0;
1691 int keycode = 0, ret = 0;
1692
1693 up_work = container_of(work, struct microp_i2c_work, work);
1694 client = up_work->client;
1695 cdata = i2c_get_clientdata(client);
1696
1697 ret = i2c_read_block(client, MICROP_I2C_RCMD_GPI_INT_STATUS, data, 2);
1698 if (ret < 0) {
1699 dev_err(&client->dev, "%s: read interrupt status fail\n",
1700 __func__);
1701 }
1702
1703 intr_status = data[0]<<8 | data[1];
1704 ret = i2c_write_block(client, MICROP_I2C_WCMD_GPI_INT_STATUS_CLR,
1705 data, 2);
1706 if (ret < 0) {
1707 dev_err(&client->dev, "%s: clear interrupt status fail\n",
1708 __func__);
1709 }
1710 pr_debug("intr_status=0x%02x\n", intr_status);
1711
1712 if ((intr_status & IRQ_LSENSOR) || cdata->force_light_sensor_read) {
1713 ret = microp_lightsensor_read(&adc_value, &adc_level);
1714 if (cdata->force_light_sensor_read) {
1715 /* report an invalid value first to ensure we trigger an event
1716 * when adc_level is zero.
1717 */
1718 input_report_abs(cdata->ls_input_dev, ABS_MISC, -1);
1719 input_sync(cdata->ls_input_dev);
1720 cdata->force_light_sensor_read = 0;
1721 }
1722 input_report_abs(cdata->ls_input_dev, ABS_MISC, (int)adc_level);
1723 input_sync(cdata->ls_input_dev);
1724 }
1725
1726 if (intr_status & IRQ_SDCARD) {
1727 microp_read_gpi_status(client, &gpi_status);
1728 mahimahi_microp_sdslot_update_status(gpi_status);
1729 }
1730
1731 if (intr_status & IRQ_HEADSETIN) {
1732 cdata->is_hpin_pin_stable = 0;
1733 wake_lock_timeout(&microp_i2c_wakelock, 3*HZ);
1734 if (!cdata->headset_is_in)
1735 schedule_delayed_work(&cdata->hpin_debounce_work,
1736 msecs_to_jiffies(500));
1737 else
1738 schedule_delayed_work(&cdata->hpin_debounce_work,
1739 msecs_to_jiffies(300));
1740 }
1741 if (intr_status & IRQ_REMOTEKEY) {
1742 if ((get_remote_keycode(&keycode) == 0) &&
1743 (cdata->is_hpin_pin_stable)) {
1744 htc_35mm_key_event(keycode, &cdata->is_hpin_pin_stable);
1745 }
1746 }
1747
1748 enable_irq(client->irq);
1749}
1750
1751static void ls_read_do_work(struct work_struct *work)
1752{
1753 struct i2c_client *client = private_microp_client;
1754 struct microp_i2c_client_data *cdata = i2c_get_clientdata(client);
1755
1756 /* force a light sensor reading */
1757 disable_irq(client->irq);
1758 cdata->force_light_sensor_read = 1;
1759 schedule_work(&cdata->work.work);
1760}
1761
1762static int microp_function_initialize(struct i2c_client *client)
1763{
1764 struct microp_i2c_client_data *cdata;
1765 uint8_t data[20];
1766 uint16_t stat, interrupts = 0;
1767 int i;
1768 int ret;
1769 struct led_classdev *led_cdev;
1770
1771 cdata = i2c_get_clientdata(client);
1772
1773 /* Light Sensor */
1774 if (als_kadc >> 16 == ALS_CALIBRATED)
1775 cdata->als_kadc = als_kadc & 0xFFFF;
1776 else {
1777 cdata->als_kadc = 0;
1778 pr_info("%s: no ALS calibrated\n", __func__);
1779 }
1780
1781 if (cdata->als_kadc && golden_adc) {
1782 cdata->als_kadc =
1783 (cdata->als_kadc > 0 && cdata->als_kadc < 0x400)
1784 ? cdata->als_kadc : golden_adc;
1785 cdata->als_gadc =
1786 (golden_adc > 0)
1787 ? golden_adc : cdata->als_kadc;
1788 } else {
1789 cdata->als_kadc = 1;
1790 cdata->als_gadc = 1;
1791 }
1792 pr_info("%s: als_kadc=0x%x, als_gadc=0x%x\n",
1793 __func__, cdata->als_kadc, cdata->als_gadc);
1794
1795 for (i = 0; i < 10; i++) {
1796 data[i] = (uint8_t)(lsensor_adc_table[i]
1797 * cdata->als_kadc / cdata->als_gadc >> 8);
1798 data[i + 10] = (uint8_t)(lsensor_adc_table[i]
1799 * cdata->als_kadc / cdata->als_gadc);
1800 }
1801 ret = i2c_write_block(client, MICROP_I2C_WCMD_ADC_TABLE, data, 20);
1802 if (ret)
1803 goto exit;
1804
1805 ret = gpio_request(MAHIMAHI_GPIO_LS_EN_N, "microp_i2c");
1806 if (ret < 0) {
1807 dev_err(&client->dev, "failed on request gpio ls_on\n");
1808 goto exit;
1809 }
1810 ret = gpio_direction_output(MAHIMAHI_GPIO_LS_EN_N, 0);
1811 if (ret < 0) {
1812 dev_err(&client->dev, "failed on gpio_direction_output"
1813 "ls_on\n");
1814 goto err_gpio_ls;
1815 }
1816 cdata->light_sensor_enabled = 1;
1817
1818 /* Headset */
1819 for (i = 0; i < 6; i++) {
1820 data[i] = (uint8_t)(remote_key_adc_table[i] >> 8);
1821 data[i + 6] = (uint8_t)(remote_key_adc_table[i]);
1822 }
1823 ret = i2c_write_block(client,
1824 MICROP_I2C_WCMD_REMOTEKEY_TABLE, data, 12);
1825 if (ret)
1826 goto exit;
1827
1828 INIT_DELAYED_WORK(
1829 &cdata->hpin_debounce_work, hpin_debounce_do_work);
1830 INIT_DELAYED_WORK(
1831 &cdata->ls_read_work, ls_read_do_work);
1832
1833 /* SD Card */
1834 interrupts |= IRQ_SDCARD;
1835
1836 /* set LED initial state */
1837 for (i = 0; i < BLUE_LED; i++) {
1838 led_cdev = &cdata->leds[i].ldev;
1839 microp_i2c_write_led_mode(client, led_cdev, 0, 0xffff);
1840 }
1841
1842 /* enable the interrupts */
1843 ret = microp_interrupt_enable(client, interrupts);
1844 if (ret < 0) {
1845 dev_err(&client->dev, "%s: failed to enable gpi irqs\n",
1846 __func__);
1847 goto err_irq_en;
1848 }
1849
1850 microp_read_gpi_status(client, &stat);
1851 mahimahi_microp_sdslot_update_status(stat);
1852
1853 return 0;
1854
1855err_irq_en:
1856err_gpio_ls:
1857 gpio_free(MAHIMAHI_GPIO_LS_EN_N);
1858exit:
1859 return ret;
1860}
1861
1862#ifdef CONFIG_HAS_EARLYSUSPEND
1863void microp_early_suspend(struct early_suspend *h)
1864{
1865 struct microp_i2c_client_data *cdata;
1866 struct i2c_client *client = private_microp_client;
1867 int ret;
1868
1869 if (!client) {
1870 pr_err("%s: dataset: client is empty\n", __func__);
1871 return;
1872 }
1873 cdata = i2c_get_clientdata(client);
1874
1875 cdata->microp_is_suspend = 1;
1876
1877 disable_irq(client->irq);
1878 ret = cancel_work_sync(&cdata->work.work);
1879 if (ret != 0) {
1880 enable_irq(client->irq);
1881 }
1882
1883 if (cdata->auto_backlight_enabled)
1884 microp_i2c_auto_backlight_mode(client, 0);
1885 if (cdata->light_sensor_enabled == 1) {
1886 gpio_set_value(MAHIMAHI_GPIO_LS_EN_N, 1);
1887 cdata->light_sensor_enabled = 0;
1888 }
1889}
1890
1891void microp_early_resume(struct early_suspend *h)
1892{
1893 struct i2c_client *client = private_microp_client;
1894 struct microp_i2c_client_data *cdata;
1895
1896 if (!client) {
1897 pr_err("%s: dataset: client is empty\n", __func__);
1898 return;
1899 }
1900 cdata = i2c_get_clientdata(client);
1901
1902 gpio_set_value(MAHIMAHI_GPIO_LS_EN_N, 0);
1903 cdata->light_sensor_enabled = 1;
1904
1905 if (cdata->auto_backlight_enabled)
1906 microp_i2c_auto_backlight_mode(client, 1);
1907
1908 cdata->microp_is_suspend = 0;
1909 enable_irq(client->irq);
1910}
1911#endif
1912
1913static int microp_i2c_suspend(struct i2c_client *client,
1914 pm_message_t mesg)
1915{
1916 return 0;
1917}
1918
1919static int microp_i2c_resume(struct i2c_client *client)
1920{
1921 return 0;
1922}
1923
1924static struct {
1925 const char *name;
1926 void (*led_set_work)(struct work_struct *);
1927 struct device_attribute **attrs;
1928 int attr_cnt;
1929} microp_leds[] = {
1930 [GREEN_LED] = {
1931 .name = "green",
1932 .led_set_work = microp_led_brightness_set_work,
1933 .attrs = green_amber_attrs,
1934 .attr_cnt = ARRAY_SIZE(green_amber_attrs)
1935 },
1936 [AMBER_LED] = {
1937 .name = "amber",
1938 .led_set_work = microp_led_brightness_set_work,
1939 .attrs = green_amber_attrs,
1940 .attr_cnt = ARRAY_SIZE(green_amber_attrs)
1941 },
1942 [RED_LED] = {
1943 .name = "red",
1944 .led_set_work = microp_led_brightness_set_work,
1945 .attrs = green_amber_attrs,
1946 .attr_cnt = ARRAY_SIZE(green_amber_attrs)
1947 },
1948 [BLUE_LED] = {
1949 .name = "blue",
1950 .led_set_work = microp_led_brightness_set_work,
1951 .attrs = green_amber_attrs,
1952 .attr_cnt = ARRAY_SIZE(green_amber_attrs)
1953 },
1954 [JOGBALL_LED] = {
1955 .name = "jogball-backlight",
1956 .led_set_work = microp_led_jogball_brightness_set_work,
1957 .attrs = jogball_attrs,
1958 .attr_cnt = ARRAY_SIZE(jogball_attrs)
1959 },
1960 [BUTTONS_LED] = {
1961 .name = "button-backlight",
1962 .led_set_work = microp_led_buttons_brightness_set_work
1963 },
1964};
1965
1966static int microp_i2c_probe(struct i2c_client *client,
1967 const struct i2c_device_id *id)
1968{
1969 struct microp_i2c_client_data *cdata;
1970 uint8_t data[6];
1971 int ret;
1972 int i;
1973 int j;
1974
1975 private_microp_client = client;
1976 ret = i2c_read_block(client, MICROP_I2C_RCMD_VERSION, data, 2);
1977 if (ret || !(data[0] && data[1])) {
1978 ret = -ENODEV;
1979 dev_err(&client->dev, "failed on get microp version\n");
1980 goto err_exit;
1981 }
1982 dev_info(&client->dev, "microp version [%02X][%02X]\n",
1983 data[0], data[1]);
1984
1985 ret = gpio_request(MAHIMAHI_GPIO_UP_RESET_N, "microp_i2c_wm");
1986 if (ret < 0) {
1987 dev_err(&client->dev, "failed on request gpio reset\n");
1988 goto err_exit;
1989 }
1990 ret = gpio_direction_output(MAHIMAHI_GPIO_UP_RESET_N, 1);
1991 if (ret < 0) {
1992 dev_err(&client->dev,
1993 "failed on gpio_direction_output reset\n");
1994 goto err_gpio_reset;
1995 }
1996
1997 cdata = kzalloc(sizeof(struct microp_i2c_client_data), GFP_KERNEL);
1998 if (!cdata) {
1999 ret = -ENOMEM;
2000 dev_err(&client->dev, "failed on allocat cdata\n");
2001 goto err_cdata;
2002 }
2003
2004 i2c_set_clientdata(client, cdata);
2005 cdata->version = data[0] << 8 | data[1];
2006 cdata->microp_is_suspend = 0;
2007 cdata->auto_backlight_enabled = 0;
2008 cdata->light_sensor_enabled = 0;
2009
2010 wake_lock_init(&microp_i2c_wakelock, WAKE_LOCK_SUSPEND,
2011 "microp_i2c_present");
2012
2013 /* Light Sensor */
2014 ret = device_create_file(&client->dev, &dev_attr_ls_adc);
2015 ret = device_create_file(&client->dev, &dev_attr_ls_auto);
2016 cdata->ls_input_dev = input_allocate_device();
2017 if (!cdata->ls_input_dev) {
2018 pr_err("%s: could not allocate input device\n", __func__);
2019 ret = -ENOMEM;
2020 goto err_request_input_dev;
2021 }
2022 cdata->ls_input_dev->name = "lightsensor-level";
2023 set_bit(EV_ABS, cdata->ls_input_dev->evbit);
2024 input_set_abs_params(cdata->ls_input_dev, ABS_MISC, 0, 9, 0, 0);
2025
2026 ret = input_register_device(cdata->ls_input_dev);
2027 if (ret < 0) {
2028 dev_err(&client->dev, "%s: can not register input device\n",
2029 __func__);
2030 goto err_register_input_dev;
2031 }
2032
2033 ret = misc_register(&lightsensor_misc);
2034 if (ret < 0) {
2035 dev_err(&client->dev, "%s: can not register misc device\n",
2036 __func__);
2037 goto err_register_misc_register;
2038 }
2039
2040 /* LEDs */
2041 ret = 0;
2042 for (i = 0; i < ARRAY_SIZE(microp_leds) && !ret; ++i) {
2043 struct microp_led_data *ldata = &cdata->leds[i];
2044
2045 ldata->type = i;
2046 ldata->ldev.name = microp_leds[i].name;
2047 ldata->ldev.brightness_set = microp_brightness_set;
2048 mutex_init(&ldata->led_data_mutex);
2049 INIT_WORK(&ldata->brightness_work, microp_leds[i].led_set_work);
2050 spin_lock_init(&ldata->brightness_lock);
2051 ret = led_classdev_register(&client->dev, &ldata->ldev);
2052 if (ret) {
2053 ldata->ldev.name = NULL;
2054 break;
2055 }
2056
2057 for (j = 0; j < microp_leds[i].attr_cnt && !ret; ++j)
2058 ret = device_create_file(ldata->ldev.dev,
2059 microp_leds[i].attrs[j]);
2060 }
2061 if (ret) {
2062 dev_err(&client->dev, "failed to add leds\n");
2063 goto err_add_leds;
2064 }
2065
2066 /* Headset */
2067 cdata->headset_is_in = 0;
2068 cdata->is_hpin_pin_stable = 1;
2069 platform_device_register(&mahimahi_h35mm);
2070
2071 ret = device_create_file(&client->dev, &dev_attr_key_adc);
2072
2073 /* G-sensor */
2074 ret = misc_register(&spi_bma_device);
2075 if (ret < 0) {
2076 pr_err("%s: init bma150 misc_register fail\n",
2077 __func__);
2078 goto err_register_bma150;
2079 }
2080#ifdef DEBUG_BMA150
2081 debugfs_create_file("gsensor_log", 0444, NULL, NULL, &gsensor_log_fops);
2082#endif
2083 /* Setup IRQ handler */
2084 INIT_WORK(&cdata->work.work, microp_i2c_intr_work_func);
2085 cdata->work.client = client;
2086
2087 ret = request_irq(client->irq,
2088 microp_i2c_intr_irq_handler,
2089 IRQF_TRIGGER_LOW,
2090 "microp_interrupt",
2091 &client->dev);
2092 if (ret) {
2093 dev_err(&client->dev, "request_irq failed\n");
2094 goto err_intr;
2095 }
2096 ret = set_irq_wake(client->irq, 1);
2097 if (ret) {
2098 dev_err(&client->dev, "set_irq_wake failed\n");
2099 goto err_intr;
2100 }
2101
2102#ifdef CONFIG_HAS_EARLYSUSPEND
2103 if (cdata->enable_early_suspend) {
2104 cdata->early_suspend.level =
2105 EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
2106 cdata->early_suspend.suspend = microp_early_suspend;
2107 cdata->early_suspend.resume = microp_early_resume;
2108 register_early_suspend(&cdata->early_suspend);
2109 }
2110#endif
2111
2112 ret = microp_function_initialize(client);
2113 if (ret) {
2114 dev_err(&client->dev, "failed on microp function initialize\n");
2115 goto err_fun_init;
2116 }
2117
2118 return 0;
2119
2120err_fun_init:
2121err_intr:
2122 misc_deregister(&spi_bma_device);
2123
2124err_register_bma150:
2125 platform_device_unregister(&mahimahi_h35mm);
2126 device_remove_file(&client->dev, &dev_attr_key_adc);
2127
2128err_add_leds:
2129 for (i = 0; i < ARRAY_SIZE(microp_leds); ++i) {
2130 if (!cdata->leds[i].ldev.name)
2131 continue;
2132 led_classdev_unregister(&cdata->leds[i].ldev);
2133 for (j = 0; j < microp_leds[i].attr_cnt; ++j)
2134 device_remove_file(cdata->leds[i].ldev.dev,
2135 microp_leds[i].attrs[j]);
2136 }
2137
2138 misc_deregister(&lightsensor_misc);
2139
2140err_register_misc_register:
2141 input_unregister_device(cdata->ls_input_dev);
2142
2143err_register_input_dev:
2144 input_free_device(cdata->ls_input_dev);
2145
2146err_request_input_dev:
2147 wake_lock_destroy(&microp_i2c_wakelock);
2148 device_remove_file(&client->dev, &dev_attr_ls_adc);
2149 device_remove_file(&client->dev, &dev_attr_ls_auto);
2150 kfree(cdata);
2151 i2c_set_clientdata(client, NULL);
2152
2153err_cdata:
2154err_gpio_reset:
2155 gpio_free(MAHIMAHI_GPIO_UP_RESET_N);
2156err_exit:
2157 return ret;
2158}
2159
2160static int __devexit microp_i2c_remove(struct i2c_client *client)
2161{
2162 struct microp_i2c_client_data *cdata;
2163 int i;
2164 int j;
2165
2166 cdata = i2c_get_clientdata(client);
2167
2168 for (i = 0; i < ARRAY_SIZE(microp_leds); ++i) {
2169 struct microp_led_data *ldata = &cdata->leds[i];
2170 cancel_work_sync(&ldata->brightness_work);
2171 }
2172
2173#ifdef CONFIG_HAS_EARLYSUSPEND
2174 if (cdata->enable_early_suspend) {
2175 unregister_early_suspend(&cdata->early_suspend);
2176 }
2177#endif
2178
2179 for (i = 0; i < ARRAY_SIZE(microp_leds); ++i) {
2180 if (!cdata->leds[i].ldev.name)
2181 continue;
2182 led_classdev_unregister(&cdata->leds[i].ldev);
2183 for (j = 0; j < microp_leds[i].attr_cnt; ++j)
2184 device_remove_file(cdata->leds[i].ldev.dev,
2185 microp_leds[i].attrs[j]);
2186 }
2187
2188 free_irq(client->irq, &client->dev);
2189
2190 gpio_free(MAHIMAHI_GPIO_UP_RESET_N);
2191
2192 misc_deregister(&lightsensor_misc);
2193 input_unregister_device(cdata->ls_input_dev);
2194 input_free_device(cdata->ls_input_dev);
2195 device_remove_file(&client->dev, &dev_attr_ls_adc);
2196 device_remove_file(&client->dev, &dev_attr_key_adc);
2197 device_remove_file(&client->dev, &dev_attr_ls_auto);
2198
2199 platform_device_unregister(&mahimahi_h35mm);
2200
2201 /* G-sensor */
2202 misc_deregister(&spi_bma_device);
2203
2204 kfree(cdata);
2205
2206 return 0;
2207}
2208
2209#define ATAG_ALS 0x5441001b
2210static int __init parse_tag_als_kadc(const struct tag *tags)
2211{
2212 int found = 0;
2213 struct tag *t = (struct tag *)tags;
2214
2215 for (; t->hdr.size; t = tag_next(t)) {
2216 if (t->hdr.tag == ATAG_ALS) {
2217 found = 1;
2218 break;
2219 }
2220 }
2221
2222 if (found)
2223 als_kadc = t->u.revision.rev;
2224 pr_debug("%s: als_kadc = 0x%x\n", __func__, als_kadc);
2225 return 0;
2226}
2227__tagtable(ATAG_ALS, parse_tag_als_kadc);
2228
2229static const struct i2c_device_id microp_i2c_id[] = {
2230 { MICROP_I2C_NAME, 0 },
2231 { }
2232};
2233
2234static struct i2c_driver microp_i2c_driver = {
2235 .driver = {
2236 .name = MICROP_I2C_NAME,
2237 },
2238 .id_table = microp_i2c_id,
2239 .probe = microp_i2c_probe,
2240 .suspend = microp_i2c_suspend,
2241 .resume = microp_i2c_resume,
2242 .remove = __devexit_p(microp_i2c_remove),
2243};
2244
2245
2246static int __init microp_i2c_init(void)
2247{
2248 return i2c_add_driver(&microp_i2c_driver);
2249}
2250
2251static void __exit microp_i2c_exit(void)
2252{
2253 i2c_del_driver(&microp_i2c_driver);
2254}
2255
2256module_init(microp_i2c_init);
2257module_exit(microp_i2c_exit);
2258
2259MODULE_AUTHOR("Eric Olsen <eolsen@android.com>");
2260MODULE_DESCRIPTION("MicroP I2C driver");
2261MODULE_LICENSE("GPL");