12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591 |
- /*
- * stk3x1x.c - Linux kernel modules for sensortek stk301x, stk321x and stk331x
- * proximity/ambient light sensor
- *
- * Copyright (c) 2013, The Linux Foundation. All Rights Reserved.
- * Copyright (C) 2012 Lex Hsieh / sensortek <lex_hsieh@sitronix.com.tw> or
- * <lex_hsieh@sensortek.com.tw>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * Linux Foundation chooses to take subject only to the GPLv2 license
- * terms, and distributes only under these terms.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/slab.h>
- #include <linux/i2c.h>
- #include <linux/mutex.h>
- #include <linux/kdev_t.h>
- #include <linux/fs.h>
- #include <linux/input.h>
- #include <linux/sensors.h>
- #include <linux/workqueue.h>
- #include <linux/irq.h>
- #include <linux/delay.h>
- #include <linux/sched.h>
- #include <linux/kthread.h>
- #include <linux/errno.h>
- #include <linux/wakelock.h>
- #include <linux/interrupt.h>
- #include <linux/gpio.h>
- #include <linux/fs.h>
- #include <linux/uaccess.h>
- #include <linux/regulator/consumer.h>
- #ifdef CONFIG_OF
- #include <linux/of_gpio.h>
- #endif
- #ifdef CONFIG_HAS_EARLYSUSPEND
- #include <linux/earlysuspend.h>
- #endif
- #include "linux/stk3x1x.h"
- #define DRIVER_VERSION "3.4.4ts"
- /* Driver Settings */
- #define CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
- #ifdef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
- #define STK_ALS_CHANGE_THD 20 /* The threshold to trigger ALS interrupt, unit: lux */
- #endif /* #ifdef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD */
- #define STK_INT_PS_MODE 1 /* 1, 2, or 3 */
- #define STK_POLL_PS
- #define STK_POLL_ALS /* ALS interrupt is valid only when STK_PS_INT_MODE = 1 or 4*/
- /* Define Register Map */
- #define STK_STATE_REG 0x00
- #define STK_PSCTRL_REG 0x01
- #define STK_ALSCTRL_REG 0x02
- #define STK_LEDCTRL_REG 0x03
- #define STK_INT_REG 0x04
- #define STK_WAIT_REG 0x05
- #define STK_THDH1_PS_REG 0x06
- #define STK_THDH2_PS_REG 0x07
- #define STK_THDL1_PS_REG 0x08
- #define STK_THDL2_PS_REG 0x09
- #define STK_THDH1_ALS_REG 0x0A
- #define STK_THDH2_ALS_REG 0x0B
- #define STK_THDL1_ALS_REG 0x0C
- #define STK_THDL2_ALS_REG 0x0D
- #define STK_FLAG_REG 0x10
- #define STK_DATA1_PS_REG 0x11
- #define STK_DATA2_PS_REG 0x12
- #define STK_DATA1_ALS_REG 0x13
- #define STK_DATA2_ALS_REG 0x14
- #define STK_DATA1_OFFSET_REG 0x15
- #define STK_DATA2_OFFSET_REG 0x16
- #define STK_DATA1_IR_REG 0x17
- #define STK_DATA2_IR_REG 0x18
- #define STK_PDT_ID_REG 0x3E
- #define STK_RSRVD_REG 0x3F
- #define STK_SW_RESET_REG 0x80
- /* Define state reg */
- #define STK_STATE_EN_IRS_SHIFT 7
- #define STK_STATE_EN_AK_SHIFT 6
- #define STK_STATE_EN_ASO_SHIFT 5
- #define STK_STATE_EN_IRO_SHIFT 4
- #define STK_STATE_EN_WAIT_SHIFT 2
- #define STK_STATE_EN_ALS_SHIFT 1
- #define STK_STATE_EN_PS_SHIFT 0
- #define STK_STATE_EN_IRS_MASK 0x80
- #define STK_STATE_EN_AK_MASK 0x40
- #define STK_STATE_EN_ASO_MASK 0x20
- #define STK_STATE_EN_IRO_MASK 0x10
- #define STK_STATE_EN_WAIT_MASK 0x04
- #define STK_STATE_EN_ALS_MASK 0x02
- #define STK_STATE_EN_PS_MASK 0x01
- /* Define PS ctrl reg */
- #define STK_PS_PRS_SHIFT 6
- #define STK_PS_GAIN_SHIFT 4
- #define STK_PS_IT_SHIFT 0
- #define STK_PS_PRS_MASK 0xC0
- #define STK_PS_GAIN_MASK 0x30
- #define STK_PS_IT_MASK 0x0F
- /* Define ALS ctrl reg */
- #define STK_ALS_PRS_SHIFT 6
- #define STK_ALS_GAIN_SHIFT 4
- #define STK_ALS_IT_SHIFT 0
- #define STK_ALS_PRS_MASK 0xC0
- #define STK_ALS_GAIN_MASK 0x30
- #define STK_ALS_IT_MASK 0x0F
- /* Define LED ctrl reg */
- #define STK_LED_IRDR_SHIFT 6
- #define STK_LED_DT_SHIFT 0
- #define STK_LED_IRDR_MASK 0xC0
- #define STK_LED_DT_MASK 0x3F
- /* Define interrupt reg */
- #define STK_INT_CTRL_SHIFT 7
- #define STK_INT_OUI_SHIFT 4
- #define STK_INT_ALS_SHIFT 3
- #define STK_INT_PS_SHIFT 0
- #define STK_INT_CTRL_MASK 0x80
- #define STK_INT_OUI_MASK 0x10
- #define STK_INT_ALS_MASK 0x08
- #define STK_INT_PS_MASK 0x07
- #define STK_INT_ALS 0x08
- /* Define flag reg */
- #define STK_FLG_ALSDR_SHIFT 7
- #define STK_FLG_PSDR_SHIFT 6
- #define STK_FLG_ALSINT_SHIFT 5
- #define STK_FLG_PSINT_SHIFT 4
- #define STK_FLG_OUI_SHIFT 2
- #define STK_FLG_IR_RDY_SHIFT 1
- #define STK_FLG_NF_SHIFT 0
- #define STK_FLG_ALSDR_MASK 0x80
- #define STK_FLG_PSDR_MASK 0x40
- #define STK_FLG_ALSINT_MASK 0x20
- #define STK_FLG_PSINT_MASK 0x10
- #define STK_FLG_OUI_MASK 0x04
- #define STK_FLG_IR_RDY_MASK 0x02
- #define STK_FLG_NF_MASK 0x01
- /* misc define */
- #define MIN_ALS_POLL_DELAY_NS 110000000
- #define DEVICE_NAME "stk_ps"
- #define ALS_NAME "stk3x1x-ls"
- #define PS_NAME "proximity"
- /* POWER SUPPLY VOLTAGE RANGE */
- #define STK3X1X_VDD_MIN_UV 2000000
- #define STK3X1X_VDD_MAX_UV 3300000
- #define STK3X1X_VIO_MIN_UV 1750000
- #define STK3X1X_VIO_MAX_UV 1950000
- #define STK_FIR_LEN 16
- #define MAX_FIR_LEN 32
- static struct sensors_classdev sensors_light_cdev = {
- .name = "stk3x1x-light",
- .vendor = "Sensortek",
- .version = 1,
- .handle = SENSORS_LIGHT_HANDLE,
- .type = SENSOR_TYPE_LIGHT,
- .max_range = "6500",
- .resolution = "0.0625",
- .sensor_power = "0.09",
- .min_delay = (MIN_ALS_POLL_DELAY_NS / 1000), /* us */
- .fifo_reserved_event_count = 0,
- .fifo_max_event_count = 0,
- .enabled = 0,
- .delay_msec = 200,
- .sensors_enable = NULL,
- .sensors_poll_delay = NULL,
- };
- static struct sensors_classdev sensors_proximity_cdev = {
- .name = "stk3x1x-proximity",
- .vendor = "Sensortek",
- .version = 1,
- .handle = SENSORS_PROXIMITY_HANDLE,
- .type = SENSOR_TYPE_PROXIMITY,
- .max_range = "5.0",
- .resolution = "5.0",
- .sensor_power = "0.1",
- .min_delay = 0,
- .fifo_reserved_event_count = 0,
- .fifo_max_event_count = 0,
- .enabled = 0,
- .delay_msec = 200,
- .sensors_enable = NULL,
- .sensors_poll_delay = NULL,
- };
- struct data_filter {
- u16 raw[MAX_FIR_LEN];
- int sum;
- int number;
- int idx;
- };
- struct stk3x1x_data {
- struct i2c_client *client;
- struct stk3x1x_platform_data *pdata;
- struct sensors_classdev als_cdev;
- struct sensors_classdev ps_cdev;
- #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
- int32_t irq;
- struct work_struct stk_work;
- struct workqueue_struct *stk_wq;
- #endif
- int int_pin;
- uint8_t wait_reg;
- #ifdef CONFIG_HAS_EARLYSUSPEND
- struct early_suspend stk_early_suspend;
- #endif
- uint16_t ps_thd_h;
- uint16_t ps_thd_l;
- struct mutex io_lock;
- struct input_dev *ps_input_dev;
- int32_t ps_distance_last;
- bool ps_enabled;
- struct wake_lock ps_wakelock;
- struct work_struct stk_ps_work;
- struct workqueue_struct *stk_ps_wq;
- #ifdef STK_POLL_PS
- struct wake_lock ps_nosuspend_wl;
- #endif
- struct input_dev *als_input_dev;
- int32_t als_lux_last;
- uint32_t als_transmittance;
- bool als_enabled;
- struct hrtimer als_timer;
- struct hrtimer ps_timer;
- ktime_t als_poll_delay;
- ktime_t ps_poll_delay;
- #ifdef STK_POLL_ALS
- struct work_struct stk_als_work;
- struct workqueue_struct *stk_als_wq;
- #endif
- struct regulator *vdd;
- struct regulator *vio;
- bool power_enabled;
- bool use_fir;
- struct data_filter fir;
- atomic_t firlength;
- };
- #if( !defined(CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD))
- static uint32_t lux_threshold_table[] =
- {
- 3,
- 10,
- 40,
- 65,
- 145,
- 300,
- 550,
- 930,
- 1250,
- 1700,
- };
- #define LUX_THD_TABLE_SIZE (sizeof(lux_threshold_table)/sizeof(uint32_t)+1)
- static uint16_t code_threshold_table[LUX_THD_TABLE_SIZE+1];
- #endif
- static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable);
- static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable);
- static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l);
- static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h);
- static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l);
- static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h);
- static int stk3x1x_device_ctl(struct stk3x1x_data *ps_data, bool enable);
- //static int32_t stk3x1x_set_ps_aoffset(struct stk3x1x_data *ps_data, uint16_t offset);
- inline uint32_t stk_alscode2lux(struct stk3x1x_data *ps_data, uint32_t alscode)
- {
- alscode += ((alscode<<7)+(alscode<<3)+(alscode>>1));
- alscode<<=3;
- alscode/=ps_data->als_transmittance;
- return alscode;
- }
- inline uint32_t stk_lux2alscode(struct stk3x1x_data *ps_data, uint32_t lux)
- {
- lux*=ps_data->als_transmittance;
- lux/=1100;
- if (unlikely(lux>=(1<<16)))
- lux = (1<<16) -1;
- return lux;
- }
- #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
- static void stk_init_code_threshold_table(struct stk3x1x_data *ps_data)
- {
- uint32_t i,j;
- uint32_t alscode;
- code_threshold_table[0] = 0;
- #ifdef STK_DEBUG_PRINTF
- printk(KERN_INFO "alscode[0]=%d\n",0);
- #endif
- for (i=1,j=0;i<LUX_THD_TABLE_SIZE;i++,j++)
- {
- alscode = stk_lux2alscode(ps_data, lux_threshold_table[j]);
- dev_dbg(&ps_data->client->dev, "alscode[%d]=%d\n", i, alscode);
- code_threshold_table[i] = (uint16_t)(alscode);
- }
- code_threshold_table[i] = 0xffff;
- dev_dbg(&ps_data->client->dev, "alscode[%d]=%d\n", i, alscode);
- }
- static uint32_t stk_get_lux_interval_index(uint16_t alscode)
- {
- uint32_t i;
- for (i=1;i<=LUX_THD_TABLE_SIZE;i++)
- {
- if ((alscode>=code_threshold_table[i-1])&&(alscode<code_threshold_table[i]))
- {
- return i;
- }
- }
- return LUX_THD_TABLE_SIZE;
- }
- #else
- inline void stk_als_set_new_thd(struct stk3x1x_data *ps_data, uint16_t alscode)
- {
- int32_t high_thd,low_thd;
- high_thd = alscode + stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD);
- low_thd = alscode - stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD);
- if (high_thd >= (1<<16))
- high_thd = (1<<16) -1;
- if (low_thd <0)
- low_thd = 0;
- stk3x1x_set_als_thd_h(ps_data, (uint16_t)high_thd);
- stk3x1x_set_als_thd_l(ps_data, (uint16_t)low_thd);
- }
- #endif // CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
- static int32_t stk3x1x_init_all_reg(struct stk3x1x_data *ps_data, struct stk3x1x_platform_data *plat_data)
- {
- int32_t ret;
- uint8_t w_reg;
- w_reg = plat_data->state_reg;
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- ps_data->ps_thd_h = plat_data->ps_thd_h;
- ps_data->ps_thd_l = plat_data->ps_thd_l;
- w_reg = plat_data->psctrl_reg;
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_PSCTRL_REG, w_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- w_reg = plat_data->alsctrl_reg;
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_ALSCTRL_REG, w_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- w_reg = plat_data->ledctrl_reg;
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_LEDCTRL_REG, w_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- ps_data->wait_reg = plat_data->wait_reg;
- if(ps_data->wait_reg < 2)
- {
- printk(KERN_WARNING "%s: wait_reg should be larger than 2, force to write 2\n", __func__);
- ps_data->wait_reg = 2;
- }
- else if (ps_data->wait_reg > 0xFF)
- {
- printk(KERN_WARNING "%s: wait_reg should be less than 0xFF, force to write 0xFF\n", __func__);
- ps_data->wait_reg = 0xFF;
- }
- w_reg = plat_data->wait_reg;
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_WAIT_REG, w_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- stk3x1x_set_ps_thd_h(ps_data, ps_data->ps_thd_h);
- stk3x1x_set_ps_thd_l(ps_data, ps_data->ps_thd_l);
- w_reg = 0;
- #ifndef STK_POLL_PS
- w_reg |= STK_INT_PS_MODE;
- #else
- w_reg |= 0x01;
- #endif
- #if (!defined(STK_POLL_ALS) && (STK_INT_PS_MODE != 0x02) && (STK_INT_PS_MODE != 0x03))
- w_reg |= STK_INT_ALS;
- #endif
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_INT_REG, w_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- ret = i2c_smbus_write_byte_data(ps_data->client, 0x87, 0x60);
- if (ret < 0) {
- dev_err(&ps_data->client->dev,
- "%s: write i2c error\n", __func__);
- return ret;
- }
- return 0;
- }
- static int32_t stk3x1x_check_pid(struct stk3x1x_data *ps_data)
- {
- int32_t err1, err2;
- err1 = i2c_smbus_read_byte_data(ps_data->client,STK_PDT_ID_REG);
- if (err1 < 0)
- {
- printk(KERN_ERR "%s: read i2c error, err=%d\n", __func__, err1);
- return err1;
- }
- err2 = i2c_smbus_read_byte_data(ps_data->client,STK_RSRVD_REG);
- if (err2 < 0)
- {
- printk(KERN_ERR "%s: read i2c error, err=%d\n", __func__, err2);
- return -1;
- }
- if(err2 == 0xC0)
- printk(KERN_INFO "%s: RID=0xC0!!!!!!!!!!!!!\n", __func__);
- return 0;
- }
- static int32_t stk3x1x_software_reset(struct stk3x1x_data *ps_data)
- {
- int32_t r;
- uint8_t w_reg;
- w_reg = 0x7F;
- r = i2c_smbus_write_byte_data(ps_data->client,STK_WAIT_REG,w_reg);
- if (r<0)
- {
- printk(KERN_ERR "%s: software reset: write i2c error, ret=%d\n", __func__, r);
- return r;
- }
- r = i2c_smbus_read_byte_data(ps_data->client,STK_WAIT_REG);
- if (w_reg != r)
- {
- printk(KERN_ERR "%s: software reset: read-back value is not the same\n", __func__);
- return -1;
- }
- r = i2c_smbus_write_byte_data(ps_data->client,STK_SW_RESET_REG,0);
- if (r<0)
- {
- printk(KERN_ERR "%s: software reset: read error after reset\n", __func__);
- return r;
- }
- msleep(1);
- return 0;
- }
- static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l)
- {
- uint8_t temp;
- uint8_t* pSrc = (uint8_t*)&thd_l;
- temp = *pSrc;
- *pSrc = *(pSrc+1);
- *(pSrc+1) = temp;
- return i2c_smbus_write_word_data(ps_data->client,STK_THDL1_ALS_REG,thd_l);
- }
- static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h)
- {
- uint8_t temp;
- uint8_t* pSrc = (uint8_t*)&thd_h;
- temp = *pSrc;
- *pSrc = *(pSrc+1);
- *(pSrc+1) = temp;
- return i2c_smbus_write_word_data(ps_data->client,STK_THDH1_ALS_REG,thd_h);
- }
- static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l)
- {
- uint8_t temp;
- uint8_t* pSrc = (uint8_t*)&thd_l;
- temp = *pSrc;
- *pSrc = *(pSrc+1);
- *(pSrc+1) = temp;
- ps_data->ps_thd_l = thd_l;
- return i2c_smbus_write_word_data(ps_data->client,STK_THDL1_PS_REG,thd_l);
- }
- static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h)
- {
- uint8_t temp;
- uint8_t* pSrc = (uint8_t*)&thd_h;
- temp = *pSrc;
- *pSrc = *(pSrc+1);
- *(pSrc+1) = temp;
- ps_data->ps_thd_h = thd_h;
- return i2c_smbus_write_word_data(ps_data->client,STK_THDH1_PS_REG,thd_h);
- }
- /*
- static int32_t stk3x1x_set_ps_foffset(struct stk3x1x_data *ps_data, uint16_t offset)
- {
- uint8_t temp;
- uint8_t* pSrc = (uint8_t*)&offset;
- temp = *pSrc;
- *pSrc = *(pSrc+1);
- *(pSrc+1) = temp;
- return i2c_smbus_write_word_data(ps_data->client,STK_DATA1_OFFSET_REG,offset);
- }
- static int32_t stk3x1x_set_ps_aoffset(struct stk3x1x_data *ps_data, uint16_t offset)
- {
- uint8_t temp;
- uint8_t* pSrc = (uint8_t*)&offset;
- int ret;
- uint8_t w_state_reg;
- uint8_t re_en;
- ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- re_en = (ret & STK_STATE_EN_AK_MASK) ? 1: 0;
- if(re_en)
- {
- w_state_reg = (uint8_t)(ret & (~STK_STATE_EN_AK_MASK));
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- msleep(1);
- }
- temp = *pSrc;
- *pSrc = *(pSrc+1);
- *(pSrc+1) = temp;
- ret = i2c_smbus_write_word_data(ps_data->client,0x0E,offset);
- if(!re_en)
- return ret;
- w_state_reg |= STK_STATE_EN_AK_MASK;
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- return 0;
- }
- */
- static inline uint32_t stk3x1x_get_ps_reading(struct stk3x1x_data *ps_data)
- {
- int32_t word_data, tmp_word_data;
- tmp_word_data = i2c_smbus_read_word_data(ps_data->client,STK_DATA1_PS_REG);
- if(tmp_word_data < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
- return tmp_word_data;
- }
- word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
- return word_data;
- }
- static int32_t stk3x1x_set_flag(struct stk3x1x_data *ps_data, uint8_t org_flag_reg, uint8_t clr)
- {
- uint8_t w_flag;
- w_flag = org_flag_reg | (STK_FLG_ALSINT_MASK | STK_FLG_PSINT_MASK | STK_FLG_OUI_MASK | STK_FLG_IR_RDY_MASK);
- w_flag &= (~clr);
- //printk(KERN_INFO "%s: org_flag_reg=0x%x, w_flag = 0x%x\n", __func__, org_flag_reg, w_flag);
- return i2c_smbus_write_byte_data(ps_data->client,STK_FLAG_REG, w_flag);
- }
- static int32_t stk3x1x_get_flag(struct stk3x1x_data *ps_data)
- {
- return i2c_smbus_read_byte_data(ps_data->client,STK_FLAG_REG);
- }
- static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable)
- {
- int32_t ret;
- uint8_t w_state_reg;
- uint8_t curr_ps_enable;
- curr_ps_enable = ps_data->ps_enabled?1:0;
- if(curr_ps_enable == enable)
- return 0;
- if (enable) {
- ret = stk3x1x_device_ctl(ps_data, enable);
- if (ret)
- return ret;
- }
- ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret);
- return ret;
- }
- w_state_reg = ret;
- w_state_reg &= ~(STK_STATE_EN_PS_MASK | STK_STATE_EN_WAIT_MASK | 0x60);
- if(enable)
- {
- w_state_reg |= STK_STATE_EN_PS_MASK;
- if(!(ps_data->als_enabled))
- w_state_reg |= STK_STATE_EN_WAIT_MASK;
- }
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret);
- return ret;
- }
- if(enable)
- {
- #ifdef STK_POLL_PS
- hrtimer_start(&ps_data->ps_timer, ps_data->ps_poll_delay, HRTIMER_MODE_REL);
- ps_data->ps_distance_last = -1;
- #endif
- ps_data->ps_enabled = true;
- #ifndef STK_POLL_PS
- #ifndef STK_POLL_ALS
- if(!(ps_data->als_enabled))
- #endif /* #ifndef STK_POLL_ALS */
- enable_irq(ps_data->irq);
- msleep(1);
- ret = stk3x1x_get_flag(ps_data);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: read i2c error, ret=%d\n", __func__, ret);
- return ret;
- }
- near_far_state = ret & STK_FLG_NF_MASK;
- ps_data->ps_distance_last = near_far_state;
- input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
- input_sync(ps_data->ps_input_dev);
- wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
- reading = stk3x1x_get_ps_reading(ps_data);
- dev_dbg(&ps_data->client->dev,
- "%s: ps input event=%d, ps code = %d\n",
- __func__, near_far_state, reading);
- #endif /* #ifndef STK_POLL_PS */
- }
- else
- {
- #ifdef STK_POLL_PS
- hrtimer_cancel(&ps_data->ps_timer);
- #else
- #ifndef STK_POLL_ALS
- if(!(ps_data->als_enabled))
- #endif
- disable_irq(ps_data->irq);
- #endif
- ps_data->ps_enabled = false;
- }
- if (!enable) {
- ret = stk3x1x_device_ctl(ps_data, enable);
- if (ret)
- return ret;
- }
- return ret;
- }
- static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable)
- {
- int32_t ret;
- uint8_t w_state_reg;
- uint8_t curr_als_enable = (ps_data->als_enabled)?1:0;
- if(curr_als_enable == enable)
- return 0;
- if (enable) {
- ret = stk3x1x_device_ctl(ps_data, enable);
- if (ret)
- return ret;
- }
- #ifndef STK_POLL_ALS
- if (enable)
- {
- stk3x1x_set_als_thd_h(ps_data, 0x0000);
- stk3x1x_set_als_thd_l(ps_data, 0xFFFF);
- }
- #endif
- ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- w_state_reg = (uint8_t)(ret & (~(STK_STATE_EN_ALS_MASK | STK_STATE_EN_WAIT_MASK)));
- if(enable)
- w_state_reg |= STK_STATE_EN_ALS_MASK;
- else if (ps_data->ps_enabled)
- w_state_reg |= STK_STATE_EN_WAIT_MASK;
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- if (enable)
- {
- ps_data->als_enabled = true;
- #ifdef STK_POLL_ALS
- hrtimer_start(&ps_data->als_timer, ps_data->als_poll_delay, HRTIMER_MODE_REL);
- #else
- #ifndef STK_POLL_PS
- if(!(ps_data->ps_enabled))
- #endif
- enable_irq(ps_data->irq);
- #endif
- }
- else
- {
- ps_data->als_enabled = false;
- #ifdef STK_POLL_ALS
- hrtimer_cancel(&ps_data->als_timer);
- #else
- #ifndef STK_POLL_PS
- if(!(ps_data->ps_enabled))
- #endif
- disable_irq(ps_data->irq);
- #endif
- }
- if (!enable) {
- ret = stk3x1x_device_ctl(ps_data, enable);
- if (ret)
- return ret;
- }
- return ret;
- }
- static inline int32_t stk3x1x_filter_reading(struct stk3x1x_data *ps_data,
- int32_t word_data)
- {
- int index;
- int firlen = atomic_read(&ps_data->firlength);
- if (ps_data->fir.number < firlen) {
- ps_data->fir.raw[ps_data->fir.number] = word_data;
- ps_data->fir.sum += word_data;
- ps_data->fir.number++;
- ps_data->fir.idx++;
- } else {
- index = ps_data->fir.idx % firlen;
- ps_data->fir.sum -= ps_data->fir.raw[index];
- ps_data->fir.raw[index] = word_data;
- ps_data->fir.sum += word_data;
- ps_data->fir.idx++;
- word_data = ps_data->fir.sum/firlen;
- }
- return word_data;
- }
- static inline int32_t stk3x1x_get_als_reading(struct stk3x1x_data *ps_data)
- {
- int32_t word_data, tmp_word_data;
- tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_ALS_REG);
- if(tmp_word_data < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
- return tmp_word_data;
- }
- word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
- if (ps_data->use_fir)
- word_data = stk3x1x_filter_reading(ps_data, word_data);
- return word_data;
- }
- static int32_t stk3x1x_get_ir_reading(struct stk3x1x_data *ps_data)
- {
- int32_t word_data, tmp_word_data;
- int32_t ret;
- uint8_t w_reg, retry = 0;
- if(ps_data->ps_enabled)
- {
- stk3x1x_enable_ps(ps_data, 0);
- ps_data->ps_enabled = true;
- }
- ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- w_reg = (uint8_t)(ret & (~STK_STATE_EN_IRS_MASK));
- w_reg |= STK_STATE_EN_IRS_MASK;
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- msleep(100);
- do
- {
- msleep(50);
- ret = stk3x1x_get_flag(ps_data);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- retry++;
- }while(retry < 5 && ((ret&STK_FLG_IR_RDY_MASK) == 0));
- if(retry == 5)
- {
- printk(KERN_ERR "%s: ir data is not ready for 300ms\n", __func__);
- return -EINVAL;
- }
- ret = stk3x1x_get_flag(ps_data);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- ret = stk3x1x_set_flag(ps_data, ret, STK_FLG_IR_RDY_MASK);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_IR_REG);
- if(tmp_word_data < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
- return tmp_word_data;
- }
- word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
- if(ps_data->ps_enabled)
- stk3x1x_enable_ps(ps_data, 1);
- return word_data;
- }
- static ssize_t stk_als_code_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- int32_t reading;
- reading = stk3x1x_get_als_reading(ps_data);
- return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
- }
- static ssize_t stk_als_enable_set(struct sensors_classdev *sensors_cdev,
- unsigned int enabled)
- {
- struct stk3x1x_data *als_data = container_of(sensors_cdev,
- struct stk3x1x_data, als_cdev);
- int err;
- mutex_lock(&als_data->io_lock);
- err = stk3x1x_enable_als(als_data, enabled);
- mutex_unlock(&als_data->io_lock);
- if (err < 0)
- return err;
- return 0;
- }
- static ssize_t stk_als_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- int32_t enable, ret;
- mutex_lock(&ps_data->io_lock);
- enable = (ps_data->als_enabled)?1:0;
- mutex_unlock(&ps_data->io_lock);
- ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG);
- ret = (ret & STK_STATE_EN_ALS_MASK)?1:0;
- if(enable != ret)
- printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret);
- return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
- }
- static ssize_t stk_als_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- uint8_t en;
- if (sysfs_streq(buf, "1"))
- en = 1;
- else if (sysfs_streq(buf, "0"))
- en = 0;
- else
- {
- printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
- return -EINVAL;
- }
- dev_dbg(dev, "%s: Enable ALS : %d\n", __func__, en);
- mutex_lock(&ps_data->io_lock);
- stk3x1x_enable_als(ps_data, en);
- mutex_unlock(&ps_data->io_lock);
- return size;
- }
- static ssize_t stk_als_lux_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- int32_t als_reading;
- uint32_t als_lux;
- als_reading = stk3x1x_get_als_reading(ps_data);
- mutex_lock(&ps_data->io_lock);
- als_lux = stk_alscode2lux(ps_data, als_reading);
- mutex_unlock(&ps_data->io_lock);
- return scnprintf(buf, PAGE_SIZE, "%d lux\n", als_lux);
- }
- static ssize_t stk_als_lux_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- unsigned long value = 0;
- int ret;
- ret = kstrtoul(buf, 16, &value);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- mutex_lock(&ps_data->io_lock);
- ps_data->als_lux_last = value;
- input_report_abs(ps_data->als_input_dev, ABS_MISC, value);
- input_sync(ps_data->als_input_dev);
- mutex_unlock(&ps_data->io_lock);
- dev_dbg(dev, "%s: als input event %ld lux\n", __func__, value);
- return size;
- }
- static ssize_t stk_als_transmittance_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- int32_t transmittance;
- mutex_lock(&ps_data->io_lock);
- transmittance = ps_data->als_transmittance;
- mutex_unlock(&ps_data->io_lock);
- return scnprintf(buf, PAGE_SIZE, "%d\n", transmittance);
- }
- static ssize_t stk_als_transmittance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- unsigned long value = 0;
- int ret;
- ret = kstrtoul(buf, 10, &value);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- mutex_lock(&ps_data->io_lock);
- ps_data->als_transmittance = value;
- mutex_unlock(&ps_data->io_lock);
- return size;
- }
- static ssize_t stk_als_delay_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- return scnprintf(buf, PAGE_SIZE, "%u\n",
- (u32)ktime_to_ms(ps_data->als_poll_delay));
- }
- static inline void stk_als_delay_store_fir(struct stk3x1x_data *ps_data)
- {
- ps_data->fir.number = 0;
- ps_data->fir.idx = 0;
- ps_data->fir.sum = 0;
- }
- static ssize_t stk_als_poll_delay_set(struct sensors_classdev *sensors_cdev,
- unsigned int delay_msec)
- {
- struct stk3x1x_data *als_data = container_of(sensors_cdev,
- struct stk3x1x_data, als_cdev);
- uint64_t value = 0;
- value = delay_msec * 1000000;
- if (value < MIN_ALS_POLL_DELAY_NS)
- value = MIN_ALS_POLL_DELAY_NS;
- mutex_lock(&als_data->io_lock);
- if (value != ktime_to_ns(als_data->als_poll_delay))
- als_data->als_poll_delay = ns_to_ktime(value);
- if (als_data->use_fir)
- stk_als_delay_store_fir(als_data);
- mutex_unlock(&als_data->io_lock);
- return 0;
- }
- static ssize_t stk_als_delay_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- uint64_t value = 0;
- int ret;
- struct stk3x1x_data *als_data = dev_get_drvdata(dev);
- ret = kstrtoull(buf, 10, &value);
- if(ret < 0)
- {
- dev_err(dev, "%s:kstrtoull failed, ret=0x%x\n", __func__, ret);
- return ret;
- }
- #ifdef STK_DEBUG_PRINTF
- dev_dbg(dev, "%s: set als poll delay=%lld\n", __func__, value);
- #endif
- ret = stk_als_poll_delay_set(&als_data->als_cdev, value);
- if (ret < 0)
- return ret;
- return size;
- }
- static ssize_t stk_als_ir_code_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- int32_t reading;
- reading = stk3x1x_get_ir_reading(ps_data);
- return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
- }
- static ssize_t stk_als_firlen_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- int len = atomic_read(&ps_data->firlength);
- dev_dbg(dev, "%s: len = %2d, idx = %2d\n",
- __func__, len, ps_data->fir.idx);
- dev_dbg(dev, "%s: sum = %5d, ave = %5d\n",
- __func__, ps_data->fir.sum, ps_data->fir.sum/len);
- return scnprintf(buf, PAGE_SIZE, "%d\n", len);
- }
- static ssize_t stk_als_firlen_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t size)
- {
- uint64_t value = 0;
- int ret;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- ret = kstrtoull(buf, 10, &value);
- if (ret < 0) {
- dev_err(dev, "%s:strict_strtoull failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- if (value > MAX_FIR_LEN) {
- dev_err(dev, "%s: firlen exceed maximum filter length\n",
- __func__);
- } else if (value < 1) {
- atomic_set(&ps_data->firlength, 1);
- memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
- } else {
- atomic_set(&ps_data->firlength, value);
- memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
- }
- return size;
- }
- static ssize_t stk_als_fir_enable_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- return scnprintf(buf, PAGE_SIZE, "%d\n", ps_data->use_fir);
- }
- static ssize_t stk_als_fir_enable_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t size)
- {
- uint64_t value = 0;
- int ret;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- ret = kstrtoull(buf, 10, &value);
- if (ret < 0) {
- dev_err(dev, "%s:strict_strtoull failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- if (value) {
- ps_data->use_fir = true;
- memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
- } else {
- ps_data->use_fir = false;
- }
- return size;
- }
- static ssize_t stk_ps_code_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- uint32_t reading;
- reading = stk3x1x_get_ps_reading(ps_data);
- return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
- }
- static ssize_t stk_ps_enable_set(struct sensors_classdev *sensors_cdev,
- unsigned int enabled)
- {
- struct stk3x1x_data *ps_data = container_of(sensors_cdev,
- struct stk3x1x_data, ps_cdev);
- int err;
- mutex_lock(&ps_data->io_lock);
- err = stk3x1x_enable_ps(ps_data, enabled);
- mutex_unlock(&ps_data->io_lock);
- if (err < 0)
- return err;
- return 0;
- }
- static ssize_t stk_ps_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- int32_t enable, ret;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- mutex_lock(&ps_data->io_lock);
- enable = (ps_data->ps_enabled)?1:0;
- mutex_unlock(&ps_data->io_lock);
- ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG);
- ret = (ret & STK_STATE_EN_PS_MASK)?1:0;
- if(enable != ret)
- printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret);
- return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
- }
- static ssize_t stk_ps_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- uint8_t en;
- if (sysfs_streq(buf, "1"))
- en = 1;
- else if (sysfs_streq(buf, "0"))
- en = 0;
- else
- {
- printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
- return -EINVAL;
- }
- dev_dbg(dev, "%s: Enable PS : %d\n", __func__, en);
- mutex_lock(&ps_data->io_lock);
- stk3x1x_enable_ps(ps_data, en);
- mutex_unlock(&ps_data->io_lock);
- return size;
- }
- static ssize_t stk_ps_enable_aso_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- int32_t ret;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG);
- ret = (ret & STK_STATE_EN_ASO_MASK)?1:0;
- return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
- }
- static ssize_t stk_ps_enable_aso_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- uint8_t en;
- int32_t ret;
- uint8_t w_state_reg;
- if (sysfs_streq(buf, "1"))
- en = 1;
- else if (sysfs_streq(buf, "0"))
- en = 0;
- else
- {
- printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
- return -EINVAL;
- }
- dev_dbg(dev, "%s: Enable PS ASO : %d\n", __func__, en);
- ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- w_state_reg = (uint8_t)(ret & (~STK_STATE_EN_ASO_MASK));
- if(en)
- w_state_reg |= STK_STATE_EN_ASO_MASK;
- ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
- if (ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- return size;
- }
- static ssize_t stk_ps_offset_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- int32_t word_data, tmp_word_data;
- tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_OFFSET_REG);
- if(tmp_word_data < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
- return tmp_word_data;
- }
- word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
- return scnprintf(buf, PAGE_SIZE, "%d\n", word_data);
- }
- static ssize_t stk_ps_offset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- unsigned long value = 0;
- int ret;
- uint16_t offset;
- ret = kstrtoul(buf, 10, &value);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- if(value > 65535)
- {
- printk(KERN_ERR "%s: invalid value, offset=%ld\n", __func__, value);
- return -EINVAL;
- }
- offset = (uint16_t) ((value&0x00FF) << 8) | ((value&0xFF00) >>8);
- ret = i2c_smbus_write_word_data(ps_data->client,STK_DATA1_OFFSET_REG,offset);
- if(ret < 0)
- {
- printk(KERN_ERR "%s: write i2c error\n", __func__);
- return ret;
- }
- return size;
- }
- static ssize_t stk_ps_distance_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- int32_t dist=1, ret;
- mutex_lock(&ps_data->io_lock);
- ret = stk3x1x_get_flag(ps_data);
- if(ret < 0)
- {
- printk(KERN_ERR "%s: stk3x1x_get_flag failed, ret=0x%x\n", __func__, ret);
- return ret;
- }
- dist = (ret & STK_FLG_NF_MASK)?1:0;
- ps_data->ps_distance_last = dist;
- input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, dist);
- input_sync(ps_data->ps_input_dev);
- mutex_unlock(&ps_data->io_lock);
- wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
- dev_dbg(dev, "%s: ps input event %d cm\n", __func__, dist);
- return scnprintf(buf, PAGE_SIZE, "%d\n", dist);
- }
- static ssize_t stk_ps_distance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- unsigned long value = 0;
- int ret;
- ret = kstrtoul(buf, 10, &value);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- mutex_lock(&ps_data->io_lock);
- ps_data->ps_distance_last = value;
- input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, value);
- input_sync(ps_data->ps_input_dev);
- mutex_unlock(&ps_data->io_lock);
- wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
- dev_dbg(dev, "%s: ps input event %ld cm\n", __func__, value);
- return size;
- }
- static ssize_t stk_ps_code_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- int32_t ps_thd_l1_reg, ps_thd_l2_reg;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- mutex_lock(&ps_data->io_lock);
- ps_thd_l1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL1_PS_REG);
- if(ps_thd_l1_reg < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l1_reg);
- return -EINVAL;
- }
- ps_thd_l2_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL2_PS_REG);
- if(ps_thd_l2_reg < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l2_reg);
- return -EINVAL;
- }
- mutex_unlock(&ps_data->io_lock);
- ps_thd_l1_reg = ps_thd_l1_reg<<8 | ps_thd_l2_reg;
- return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_l1_reg);
- }
- static ssize_t stk_ps_code_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- unsigned long value = 0;
- int ret;
- ret = kstrtoul(buf, 10, &value);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- mutex_lock(&ps_data->io_lock);
- stk3x1x_set_ps_thd_l(ps_data, value);
- mutex_unlock(&ps_data->io_lock);
- return size;
- }
- static ssize_t stk_ps_code_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- int32_t ps_thd_h1_reg, ps_thd_h2_reg;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- mutex_lock(&ps_data->io_lock);
- ps_thd_h1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH1_PS_REG);
- if(ps_thd_h1_reg < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h1_reg);
- return -EINVAL;
- }
- ps_thd_h2_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH2_PS_REG);
- if(ps_thd_h2_reg < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h2_reg);
- return -EINVAL;
- }
- mutex_unlock(&ps_data->io_lock);
- ps_thd_h1_reg = ps_thd_h1_reg<<8 | ps_thd_h2_reg;
- return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_h1_reg);
- }
- static ssize_t stk_ps_code_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- unsigned long value = 0;
- int ret;
- ret = kstrtoul(buf, 10, &value);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- mutex_lock(&ps_data->io_lock);
- stk3x1x_set_ps_thd_h(ps_data, value);
- mutex_unlock(&ps_data->io_lock);
- return size;
- }
- #if 0
- static ssize_t stk_als_lux_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- int32_t als_thd_l0_reg,als_thd_l1_reg;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- uint32_t als_lux;
- mutex_lock(&ps_data->io_lock);
- als_thd_l0_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL1_ALS_REG);
- als_thd_l1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL2_ALS_REG);
- if(als_thd_l0_reg < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l0_reg);
- return -EINVAL;
- }
- if(als_thd_l1_reg < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l1_reg);
- return -EINVAL;
- }
- als_thd_l0_reg|=(als_thd_l1_reg<<8);
- als_lux = stk_alscode2lux(ps_data, als_thd_l0_reg);
- mutex_unlock(&ps_data->io_lock);
- return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux);
- }
- static ssize_t stk_als_lux_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- unsigned long value = 0;
- int ret;
- ret = kstrtoul(buf, 10, &value);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- mutex_lock(&ps_data->io_lock);
- value = stk_lux2alscode(ps_data, value);
- stk3x1x_set_als_thd_l(ps_data, value);
- mutex_unlock(&ps_data->io_lock);
- return size;
- }
- static ssize_t stk_als_lux_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- int32_t als_thd_h0_reg,als_thd_h1_reg;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- uint32_t als_lux;
- mutex_lock(&ps_data->io_lock);
- als_thd_h0_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH1_ALS_REG);
- als_thd_h1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH2_ALS_REG);
- if(als_thd_h0_reg < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h0_reg);
- return -EINVAL;
- }
- if(als_thd_h1_reg < 0)
- {
- printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h1_reg);
- return -EINVAL;
- }
- als_thd_h0_reg|=(als_thd_h1_reg<<8);
- als_lux = stk_alscode2lux(ps_data, als_thd_h0_reg);
- mutex_unlock(&ps_data->io_lock);
- return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux);
- }
- static ssize_t stk_als_lux_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- unsigned long value = 0;
- int ret;
- ret = strict_strtoul(buf, 10, &value);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
- return ret;
- }
- mutex_lock(&ps_data->io_lock);
- value = stk_lux2alscode(ps_data, value);
- stk3x1x_set_als_thd_h(ps_data, value);
- mutex_unlock(&ps_data->io_lock);
- return size;
- }
- #endif
- static ssize_t stk_all_reg_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- int32_t ps_reg[27];
- uint8_t cnt;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- mutex_lock(&ps_data->io_lock);
- for(cnt=0;cnt<25;cnt++)
- {
- ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, (cnt));
- if(ps_reg[cnt] < 0)
- {
- mutex_unlock(&ps_data->io_lock);
- printk(KERN_ERR "stk_all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]);
- return -EINVAL;
- }
- else
- {
- dev_dbg(dev, "reg[0x%2X]=0x%2X\n", cnt, ps_reg[cnt]);
- }
- }
- ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, STK_PDT_ID_REG);
- if(ps_reg[cnt] < 0)
- {
- mutex_unlock(&ps_data->io_lock);
- printk( KERN_ERR "all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]);
- return -EINVAL;
- }
- dev_dbg(dev, "reg[0x%x]=0x%2X\n", STK_PDT_ID_REG, ps_reg[cnt]);
- cnt++;
- ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, STK_RSRVD_REG);
- if(ps_reg[cnt] < 0)
- {
- mutex_unlock(&ps_data->io_lock);
- printk( KERN_ERR "all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]);
- return -EINVAL;
- }
- dev_dbg(dev, "reg[0x%x]=0x%2X\n", STK_RSRVD_REG, ps_reg[cnt]);
- mutex_unlock(&ps_data->io_lock);
- 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",
- 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],
- 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],
- 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]);
- }
- static ssize_t stk_recv_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- return 0;
- }
- static ssize_t stk_recv_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- unsigned long value = 0;
- int ret;
- int32_t recv_data;
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- ret = kstrtoul(buf, 16, &value);
- if (ret < 0) {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- recv_data = i2c_smbus_read_byte_data(ps_data->client,value);
- printk("%s: reg 0x%x=0x%x\n", __func__, (int)value, recv_data);
- return size;
- }
- static ssize_t stk_send_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- return 0;
- }
- static ssize_t stk_send_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
- {
- int addr, cmd;
- u8 addr_u8, cmd_u8;
- int32_t ret, i;
- char *token[10];
- struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
- for (i = 0; i < 2; i++)
- token[i] = strsep((char **)&buf, " ");
- ret = kstrtoul(token[0], 16, (unsigned long *)&(addr));
- if (ret < 0) {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- ret = kstrtoul(token[1], 16, (unsigned long *)&(cmd));
- if (ret < 0) {
- printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
- __func__, ret);
- return ret;
- }
- dev_dbg(dev, "%s: write reg 0x%x=0x%x\n", __func__, addr, cmd);
- addr_u8 = (u8) addr;
- cmd_u8 = (u8) cmd;
- //mutex_lock(&ps_data->io_lock);
- ret = i2c_smbus_write_byte_data(ps_data->client,addr_u8,cmd_u8);
- //mutex_unlock(&ps_data->io_lock);
- if (0 != ret)
- {
- printk(KERN_ERR "%s: i2c_smbus_write_byte_data fail\n", __func__);
- return ret;
- }
- return size;
- }
- static struct device_attribute als_enable_attribute = __ATTR(enable,0664,stk_als_enable_show,stk_als_enable_store);
- static struct device_attribute als_lux_attribute = __ATTR(lux,0664,stk_als_lux_show,stk_als_lux_store);
- static struct device_attribute als_code_attribute = __ATTR(code, 0444, stk_als_code_show, NULL);
- static struct device_attribute als_transmittance_attribute = __ATTR(transmittance,0664,stk_als_transmittance_show,stk_als_transmittance_store);
- static struct device_attribute als_poll_delay_attribute =
- __ATTR(poll_delay, 0664, stk_als_delay_show, stk_als_delay_store);
- static struct device_attribute als_ir_code_attribute = __ATTR(ircode,0444,stk_als_ir_code_show,NULL);
- static struct device_attribute als_firlen_attribute =
- __ATTR(firlen, 0664, stk_als_firlen_show, stk_als_firlen_store);
- static struct device_attribute als_fir_enable_attribute =
- __ATTR(fir_enable, 0664, stk_als_fir_enable_show,
- stk_als_fir_enable_store);
- static struct attribute *stk_als_attrs [] =
- {
- &als_enable_attribute.attr,
- &als_lux_attribute.attr,
- &als_code_attribute.attr,
- &als_transmittance_attribute.attr,
- &als_poll_delay_attribute.attr,
- &als_ir_code_attribute.attr,
- &als_firlen_attribute.attr,
- &als_fir_enable_attribute.attr,
- NULL
- };
- static struct attribute_group stk_als_attribute_group = {
- .attrs = stk_als_attrs,
- };
- static struct device_attribute ps_enable_attribute = __ATTR(enable,0664,stk_ps_enable_show,stk_ps_enable_store);
- static struct device_attribute ps_enable_aso_attribute = __ATTR(enableaso,0664,stk_ps_enable_aso_show,stk_ps_enable_aso_store);
- static struct device_attribute ps_distance_attribute = __ATTR(distance,0664,stk_ps_distance_show, stk_ps_distance_store);
- static struct device_attribute ps_offset_attribute = __ATTR(offset,0664,stk_ps_offset_show, stk_ps_offset_store);
- static struct device_attribute ps_code_attribute = __ATTR(code, 0444, stk_ps_code_show, NULL);
- static struct device_attribute ps_code_thd_l_attribute = __ATTR(codethdl,0664,stk_ps_code_thd_l_show,stk_ps_code_thd_l_store);
- static struct device_attribute ps_code_thd_h_attribute = __ATTR(codethdh,0664,stk_ps_code_thd_h_show,stk_ps_code_thd_h_store);
- static struct device_attribute recv_attribute = __ATTR(recv,0664,stk_recv_show,stk_recv_store);
- static struct device_attribute send_attribute = __ATTR(send,0664,stk_send_show, stk_send_store);
- static struct device_attribute all_reg_attribute = __ATTR(allreg, 0444, stk_all_reg_show, NULL);
- static struct attribute *stk_ps_attrs [] =
- {
- &ps_enable_attribute.attr,
- &ps_enable_aso_attribute.attr,
- &ps_distance_attribute.attr,
- &ps_offset_attribute.attr,
- &ps_code_attribute.attr,
- &ps_code_thd_l_attribute.attr,
- &ps_code_thd_h_attribute.attr,
- &recv_attribute.attr,
- &send_attribute.attr,
- &all_reg_attribute.attr,
- NULL
- };
- static struct attribute_group stk_ps_attribute_group = {
- .attrs = stk_ps_attrs,
- };
- #ifdef STK_POLL_ALS
- static enum hrtimer_restart stk_als_timer_func(struct hrtimer *timer)
- {
- struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, als_timer);
- queue_work(ps_data->stk_als_wq, &ps_data->stk_als_work);
- hrtimer_forward_now(&ps_data->als_timer, ps_data->als_poll_delay);
- return HRTIMER_RESTART;
- }
- static void stk_als_work_func(struct work_struct *work)
- {
- struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_als_work);
- int32_t reading;
- mutex_lock(&ps_data->io_lock);
- reading = stk3x1x_get_als_reading(ps_data);
- if(reading < 0)
- return;
- ps_data->als_lux_last = stk_alscode2lux(ps_data, reading);
- input_report_abs(ps_data->als_input_dev, ABS_MISC, ps_data->als_lux_last);
- input_sync(ps_data->als_input_dev);
- mutex_unlock(&ps_data->io_lock);
- }
- #endif
- static enum hrtimer_restart stk_ps_timer_func(struct hrtimer *timer)
- {
- struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, ps_timer);
- queue_work(ps_data->stk_ps_wq, &ps_data->stk_ps_work);
- #ifdef STK_POLL_PS
- hrtimer_forward_now(&ps_data->ps_timer, ps_data->ps_poll_delay);
- return HRTIMER_RESTART;
- #else
- hrtimer_cancel(&ps_data->ps_timer);
- return HRTIMER_NORESTART;
- #endif
- }
- static void stk_ps_work_func(struct work_struct *work)
- {
- struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_ps_work);
- uint32_t reading;
- int32_t near_far_state;
- uint8_t org_flag_reg;
- int32_t ret;
- uint8_t disable_flag = 0;
- mutex_lock(&ps_data->io_lock);
- org_flag_reg = stk3x1x_get_flag(ps_data);
- if(org_flag_reg < 0)
- {
- printk(KERN_ERR "%s: get_status_reg fail, ret=%d", __func__, org_flag_reg);
- goto err_i2c_rw;
- }
- near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0;
- reading = stk3x1x_get_ps_reading(ps_data);
- if(ps_data->ps_distance_last != near_far_state)
- {
- ps_data->ps_distance_last = near_far_state;
- input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
- input_sync(ps_data->ps_input_dev);
- wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
- #ifdef STK_DEBUG_PRINTF
- printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n",__func__, near_far_state, reading);
- #endif
- }
- ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:stk3x1x_set_flag fail, ret=%d\n", __func__, ret);
- goto err_i2c_rw;
- }
- mutex_unlock(&ps_data->io_lock);
- return;
- err_i2c_rw:
- mutex_unlock(&ps_data->io_lock);
- msleep(30);
- return;
- }
- #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
- static void stk_work_func(struct work_struct *work)
- {
- uint32_t reading;
- #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02))
- int32_t ret;
- uint8_t disable_flag = 0;
- uint8_t org_flag_reg;
- #endif /* #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02)) */
- #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
- uint32_t nLuxIndex;
- #endif
- struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_work);
- int32_t near_far_state;
- mutex_lock(&ps_data->io_lock);
- #if (STK_INT_PS_MODE == 0x03)
- near_far_state = gpio_get_value(ps_data->int_pin);
- #elif (STK_INT_PS_MODE == 0x02)
- near_far_state = !(gpio_get_value(ps_data->int_pin));
- #endif
- #if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02))
- ps_data->ps_distance_last = near_far_state;
- input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
- input_sync(ps_data->ps_input_dev);
- wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
- reading = stk3x1x_get_ps_reading(ps_data);
- #ifdef STK_DEBUG_PRINTF
- printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n",__func__, near_far_state, reading);
- #endif
- #else
- /* mode 0x01 or 0x04 */
- org_flag_reg = stk3x1x_get_flag(ps_data);
- if(org_flag_reg < 0)
- {
- printk(KERN_ERR "%s: get_status_reg fail, org_flag_reg=%d", __func__, org_flag_reg);
- goto err_i2c_rw;
- }
- if (org_flag_reg & STK_FLG_ALSINT_MASK)
- {
- disable_flag |= STK_FLG_ALSINT_MASK;
- reading = stk3x1x_get_als_reading(ps_data);
- if(reading < 0)
- {
- printk(KERN_ERR "%s: stk3x1x_get_als_reading fail, ret=%d", __func__, reading);
- goto err_i2c_rw;
- }
- #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
- nLuxIndex = stk_get_lux_interval_index(reading);
- stk3x1x_set_als_thd_h(ps_data, code_threshold_table[nLuxIndex]);
- stk3x1x_set_als_thd_l(ps_data, code_threshold_table[nLuxIndex-1]);
- #else
- stk_als_set_new_thd(ps_data, reading);
- #endif //CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
- ps_data->als_lux_last = stk_alscode2lux(ps_data, reading);
- input_report_abs(ps_data->als_input_dev, ABS_MISC, ps_data->als_lux_last);
- input_sync(ps_data->als_input_dev);
- #ifdef STK_DEBUG_PRINTF
- printk(KERN_INFO "%s: als input event %d lux\n",__func__, ps_data->als_lux_last);
- #endif
- }
- if (org_flag_reg & STK_FLG_PSINT_MASK)
- {
- disable_flag |= STK_FLG_PSINT_MASK;
- near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0;
- ps_data->ps_distance_last = near_far_state;
- input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
- input_sync(ps_data->ps_input_dev);
- wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
- reading = stk3x1x_get_ps_reading(ps_data);
- #ifdef STK_DEBUG_PRINTF
- printk(KERN_INFO "%s: ps input event=%d, ps code = %d\n",__func__, near_far_state, reading);
- #endif
- }
- ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
- if(ret < 0)
- {
- printk(KERN_ERR "%s:reset_int_flag fail, ret=%d\n", __func__, ret);
- goto err_i2c_rw;
- }
- #endif
- msleep(1);
- enable_irq(ps_data->irq);
- mutex_unlock(&ps_data->io_lock);
- return;
- err_i2c_rw:
- mutex_unlock(&ps_data->io_lock);
- msleep(30);
- enable_irq(ps_data->irq);
- return;
- }
- #endif
- #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
- static irqreturn_t stk_oss_irq_handler(int irq, void *data)
- {
- struct stk3x1x_data *pData = data;
- disable_irq_nosync(irq);
- queue_work(pData->stk_wq,&pData->stk_work);
- return IRQ_HANDLED;
- }
- #endif /* #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS)) */
- static inline void stk3x1x_init_fir(struct stk3x1x_data *ps_data)
- {
- memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
- atomic_set(&ps_data->firlength, STK_FIR_LEN);
- }
- static int32_t stk3x1x_init_all_setting(struct i2c_client *client, struct stk3x1x_platform_data *plat_data)
- {
- int32_t ret;
- struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
- ret = stk3x1x_software_reset(ps_data);
- if(ret < 0)
- return ret;
- stk3x1x_check_pid(ps_data);
- if(ret < 0)
- return ret;
- ret = stk3x1x_init_all_reg(ps_data, plat_data);
- if(ret < 0)
- return ret;
- #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
- stk_init_code_threshold_table(ps_data);
- #endif
- if (plat_data->use_fir)
- stk3x1x_init_fir(ps_data);
- return 0;
- }
- #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
- static int stk3x1x_setup_irq(struct i2c_client *client)
- {
- int irq, err = -EIO;
- struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
- irq = gpio_to_irq(ps_data->int_pin);
- #ifdef STK_DEBUG_PRINTF
- printk(KERN_INFO "%s: int pin #=%d, irq=%d\n",__func__, ps_data->int_pin, irq);
- #endif
- if (irq <= 0)
- {
- printk(KERN_ERR "irq number is not specified, irq # = %d, int pin=%d\n",irq, ps_data->int_pin);
- return irq;
- }
- ps_data->irq = irq;
- err = gpio_request(ps_data->int_pin,"stk-int");
- if(err < 0)
- {
- printk(KERN_ERR "%s: gpio_request, err=%d", __func__, err);
- return err;
- }
- err = gpio_direction_input(ps_data->int_pin);
- if(err < 0)
- {
- printk(KERN_ERR "%s: gpio_direction_input, err=%d", __func__, err);
- return err;
- }
- #if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02))
- err = request_any_context_irq(irq, stk_oss_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, DEVICE_NAME, ps_data);
- #else
- err = request_any_context_irq(irq, stk_oss_irq_handler, IRQF_TRIGGER_LOW, DEVICE_NAME, ps_data);
- #endif
- if (err < 0)
- {
- printk(KERN_WARNING "%s: request_any_context_irq(%d) failed for (%d)\n", __func__, irq, err);
- goto err_request_any_context_irq;
- }
- disable_irq(irq);
- return 0;
- err_request_any_context_irq:
- gpio_free(ps_data->int_pin);
- return err;
- }
- #endif
- #ifdef CONFIG_HAS_EARLYSUSPEND
- static void stk3x1x_early_suspend(struct early_suspend *h)
- {
- struct stk3x1x_data *ps_data = container_of(h, struct stk3x1x_data, stk_early_suspend);
- #ifndef STK_POLL_PS
- int err;
- #endif
- mutex_lock(&ps_data->io_lock);
- if(ps_data->als_enabled)
- {
- stk3x1x_enable_als(ps_data, 0);
- ps_data->als_enabled = true;
- }
- if(ps_data->ps_enabled)
- {
- #ifdef STK_POLL_PS
- wake_lock(&ps_data->ps_nosuspend_wl);
- #else
- err = enable_irq_wake(ps_data->irq);
- if (err)
- printk(KERN_WARNING "%s: set_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err);
- #endif
- }
- mutex_unlock(&ps_data->io_lock);
- return;
- }
- static void stk3x1x_late_resume(struct early_suspend *h)
- {
- struct stk3x1x_data *ps_data = container_of(h, struct stk3x1x_data, stk_early_suspend);
- #ifndef STK_POLL_PS
- int err;
- #endif
- mutex_lock(&ps_data->io_lock);
- if(ps_data->als_enabled)
- stk3x1x_enable_als(ps_data, 1);
- if(ps_data->ps_enabled)
- {
- #ifdef STK_POLL_PS
- wake_lock(&ps_data->ps_nosuspend_wl);
- #else
- err = disable_irq_wake(ps_data->irq);
- if (err)
- printk(KERN_WARNING "%s: disable_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err);
- #endif
- }
- mutex_unlock(&ps_data->io_lock);
- return;
- }
- #endif //#ifdef CONFIG_HAS_EARLYSUSPEND
- static int stk3x1x_power_ctl(struct stk3x1x_data *data, bool on)
- {
- int ret = 0;
- if (!on && data->power_enabled) {
- ret = regulator_disable(data->vdd);
- if (ret) {
- dev_err(&data->client->dev,
- "Regulator vdd disable failed ret=%d\n", ret);
- return ret;
- }
- ret = regulator_disable(data->vio);
- if (ret) {
- dev_err(&data->client->dev,
- "Regulator vio disable failed ret=%d\n", ret);
- regulator_enable(data->vdd);
- return ret;
- }
- data->power_enabled = on;
- dev_dbg(&data->client->dev, "stk3x1x_power_ctl on=%d\n",
- on);
- } else if (on && !data->power_enabled) {
- ret = regulator_enable(data->vdd);
- if (ret) {
- dev_err(&data->client->dev,
- "Regulator vdd enable failed ret=%d\n", ret);
- return ret;
- }
- ret = regulator_enable(data->vio);
- if (ret) {
- dev_err(&data->client->dev,
- "Regulator vio enable failed ret=%d\n", ret);
- regulator_disable(data->vdd);
- return ret;
- }
- data->power_enabled = on;
- dev_dbg(&data->client->dev, "stk3x1x_power_ctl on=%d\n",
- on);
- } else {
- dev_warn(&data->client->dev,
- "Power on=%d. enabled=%d\n",
- on, data->power_enabled);
- }
- return ret;
- }
- static int stk3x1x_power_init(struct stk3x1x_data *data, bool on)
- {
- int ret;
- if (!on) {
- if (regulator_count_voltages(data->vdd) > 0)
- regulator_set_voltage(data->vdd,
- 0, STK3X1X_VDD_MAX_UV);
- regulator_put(data->vdd);
- if (regulator_count_voltages(data->vio) > 0)
- regulator_set_voltage(data->vio,
- 0, STK3X1X_VIO_MAX_UV);
- regulator_put(data->vio);
- } else {
- data->vdd = regulator_get(&data->client->dev, "vdd");
- if (IS_ERR(data->vdd)) {
- ret = PTR_ERR(data->vdd);
- dev_err(&data->client->dev,
- "Regulator get failed vdd ret=%d\n", ret);
- return ret;
- }
- if (regulator_count_voltages(data->vdd) > 0) {
- ret = regulator_set_voltage(data->vdd,
- STK3X1X_VDD_MIN_UV,
- STK3X1X_VDD_MAX_UV);
- if (ret) {
- dev_err(&data->client->dev,
- "Regulator set failed vdd ret=%d\n",
- ret);
- goto reg_vdd_put;
- }
- }
- data->vio = regulator_get(&data->client->dev, "vio");
- if (IS_ERR(data->vio)) {
- ret = PTR_ERR(data->vio);
- dev_err(&data->client->dev,
- "Regulator get failed vio ret=%d\n", ret);
- goto reg_vdd_set;
- }
- if (regulator_count_voltages(data->vio) > 0) {
- ret = regulator_set_voltage(data->vio,
- STK3X1X_VIO_MIN_UV,
- STK3X1X_VIO_MAX_UV);
- if (ret) {
- dev_err(&data->client->dev,
- "Regulator set failed vio ret=%d\n", ret);
- goto reg_vio_put;
- }
- }
- }
- return 0;
- reg_vio_put:
- regulator_put(data->vio);
- reg_vdd_set:
- if (regulator_count_voltages(data->vdd) > 0)
- regulator_set_voltage(data->vdd, 0, STK3X1X_VDD_MAX_UV);
- reg_vdd_put:
- regulator_put(data->vdd);
- return ret;
- }
- static int stk3x1x_device_ctl(struct stk3x1x_data *ps_data, bool enable)
- {
- int ret;
- struct device *dev = &ps_data->client->dev;
- if (enable && !ps_data->power_enabled) {
- ret = stk3x1x_power_ctl(ps_data, true);
- if (ret) {
- dev_err(dev, "Failed to enable device power\n");
- goto err_exit;
- }
- ret = stk3x1x_init_all_setting(ps_data->client, ps_data->pdata);
- if (ret < 0) {
- stk3x1x_power_ctl(ps_data, false);
- dev_err(dev, "Failed to re-init device setting\n");
- goto err_exit;
- }
- } else if (!enable && ps_data->power_enabled) {
- if (!ps_data->als_enabled && !ps_data->ps_enabled) {
- ret = stk3x1x_power_ctl(ps_data, false);
- if (ret) {
- dev_err(dev, "Failed to disable device power\n");
- goto err_exit;
- }
- } else {
- dev_dbg(dev, "device control: als_enabled=%d, ps_enabled=%d\n",
- ps_data->als_enabled, ps_data->ps_enabled);
- }
- } else {
- dev_dbg(dev, "device control: enable=%d, power_enabled=%d\n",
- enable, ps_data->power_enabled);
- }
- return 0;
- err_exit:
- return ret;
- }
- #ifdef CONFIG_OF
- static int stk3x1x_parse_dt(struct device *dev,
- struct stk3x1x_platform_data *pdata)
- {
- int rc;
- struct device_node *np = dev->of_node;
- u32 temp_val;
- pdata->int_pin = of_get_named_gpio_flags(np, "stk,irq-gpio",
- 0, &pdata->int_flags);
- if (pdata->int_pin < 0) {
- dev_err(dev, "Unable to read irq-gpio\n");
- return pdata->int_pin;
- }
- rc = of_property_read_u32(np, "stk,transmittance", &temp_val);
- if (!rc)
- pdata->transmittance = temp_val;
- else {
- dev_err(dev, "Unable to read transmittance\n");
- return rc;
- }
- rc = of_property_read_u32(np, "stk,state-reg", &temp_val);
- if (!rc)
- pdata->state_reg = temp_val;
- else {
- dev_err(dev, "Unable to read state-reg\n");
- return rc;
- }
- rc = of_property_read_u32(np, "stk,psctrl-reg", &temp_val);
- if (!rc)
- pdata->psctrl_reg = (u8)temp_val;
- else {
- dev_err(dev, "Unable to read psctrl-reg\n");
- return rc;
- }
- rc = of_property_read_u32(np, "stk,alsctrl-reg", &temp_val);
- if (!rc)
- pdata->alsctrl_reg = (u8)temp_val;
- else {
- dev_err(dev, "Unable to read alsctrl-reg\n");
- return rc;
- }
- rc = of_property_read_u32(np, "stk,ledctrl-reg", &temp_val);
- if (!rc)
- pdata->ledctrl_reg = (u8)temp_val;
- else {
- dev_err(dev, "Unable to read ledctrl-reg\n");
- return rc;
- }
- rc = of_property_read_u32(np, "stk,wait-reg", &temp_val);
- if (!rc)
- pdata->wait_reg = (u8)temp_val;
- else {
- dev_err(dev, "Unable to read wait-reg\n");
- return rc;
- }
- rc = of_property_read_u32(np, "stk,ps-thdh", &temp_val);
- if (!rc)
- pdata->ps_thd_h = (u16)temp_val;
- else {
- dev_err(dev, "Unable to read ps-thdh\n");
- return rc;
- }
- rc = of_property_read_u32(np, "stk,ps-thdl", &temp_val);
- if (!rc)
- pdata->ps_thd_l = (u16)temp_val;
- else {
- dev_err(dev, "Unable to read ps-thdl\n");
- return rc;
- }
- pdata->use_fir = of_property_read_bool(np, "stk,use-fir");
- return 0;
- }
- #else
- static int stk3x1x_parse_dt(struct device *dev,
- struct stk3x1x_platform_data *pdata)
- {
- return -ENODEV;
- }
- #endif /* !CONFIG_OF */
- static int stk3x1x_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- int err = -ENODEV;
- struct stk3x1x_data *ps_data;
- struct stk3x1x_platform_data *plat_data;
- printk(KERN_INFO "%s: driver version = %s\n", __func__, DRIVER_VERSION);
- if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
- {
- printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_BYTE_DATA\n", __func__);
- return -ENODEV;
- }
- if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
- {
- printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_WORD_DATA\n", __func__);
- return -ENODEV;
- }
- ps_data = kzalloc(sizeof(struct stk3x1x_data),GFP_KERNEL);
- if(!ps_data)
- {
- printk(KERN_ERR "%s: failed to allocate stk3x1x_data\n", __func__);
- return -ENOMEM;
- }
- ps_data->client = client;
- i2c_set_clientdata(client,ps_data);
- mutex_init(&ps_data->io_lock);
- wake_lock_init(&ps_data->ps_wakelock,WAKE_LOCK_SUSPEND, "stk_input_wakelock");
- #ifdef STK_POLL_PS
- wake_lock_init(&ps_data->ps_nosuspend_wl,WAKE_LOCK_SUSPEND, "stk_nosuspend_wakelock");
- #endif
- if (client->dev.of_node) {
- plat_data = devm_kzalloc(&client->dev,
- sizeof(struct stk3x1x_platform_data), GFP_KERNEL);
- if (!plat_data) {
- dev_err(&client->dev, "Failed to allocate memory\n");
- return -ENOMEM;
- }
- err = stk3x1x_parse_dt(&client->dev, plat_data);
- dev_err(&client->dev,
- "%s: stk3x1x_parse_dt ret=%d\n", __func__, err);
- if (err)
- return err;
- } else
- plat_data = client->dev.platform_data;
- if (!plat_data) {
- dev_err(&client->dev,
- "%s: no stk3x1x platform data!\n", __func__);
- goto err_als_input_allocate;
- }
- ps_data->als_transmittance = plat_data->transmittance;
- ps_data->int_pin = plat_data->int_pin;
- ps_data->use_fir = plat_data->use_fir;
- ps_data->pdata = plat_data;
- if (ps_data->als_transmittance == 0) {
- dev_err(&client->dev,
- "%s: Please set als_transmittance\n", __func__);
- goto err_als_input_allocate;
- }
- ps_data->als_input_dev = input_allocate_device();
- if (ps_data->als_input_dev==NULL)
- {
- printk(KERN_ERR "%s: could not allocate als device\n", __func__);
- err = -ENOMEM;
- goto err_als_input_allocate;
- }
- ps_data->ps_input_dev = input_allocate_device();
- if (ps_data->ps_input_dev==NULL)
- {
- printk(KERN_ERR "%s: could not allocate ps device\n", __func__);
- err = -ENOMEM;
- goto err_ps_input_allocate;
- }
- ps_data->als_input_dev->name = ALS_NAME;
- ps_data->ps_input_dev->name = PS_NAME;
- set_bit(EV_ABS, ps_data->als_input_dev->evbit);
- set_bit(EV_ABS, ps_data->ps_input_dev->evbit);
- input_set_abs_params(ps_data->als_input_dev, ABS_MISC, 0, stk_alscode2lux(ps_data, (1<<16)-1), 0, 0);
- input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0,1, 0, 0);
- err = input_register_device(ps_data->als_input_dev);
- if (err<0)
- {
- printk(KERN_ERR "%s: can not register als input device\n", __func__);
- goto err_als_input_register;
- }
- err = input_register_device(ps_data->ps_input_dev);
- if (err<0)
- {
- printk(KERN_ERR "%s: can not register ps input device\n", __func__);
- goto err_ps_input_register;
- }
- err = sysfs_create_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
- if (err < 0)
- {
- printk(KERN_ERR "%s:could not create sysfs group for als\n", __func__);
- goto err_als_sysfs_create_group;
- }
- err = sysfs_create_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
- if (err < 0)
- {
- printk(KERN_ERR "%s:could not create sysfs group for ps\n", __func__);
- goto err_ps_sysfs_create_group;
- }
- input_set_drvdata(ps_data->als_input_dev, ps_data);
- input_set_drvdata(ps_data->ps_input_dev, ps_data);
- #ifdef STK_POLL_ALS
- ps_data->stk_als_wq = create_singlethread_workqueue("stk_als_wq");
- INIT_WORK(&ps_data->stk_als_work, stk_als_work_func);
- hrtimer_init(&ps_data->als_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
- ps_data->als_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC);
- ps_data->als_timer.function = stk_als_timer_func;
- #endif
- ps_data->stk_ps_wq = create_singlethread_workqueue("stk_ps_wq");
- INIT_WORK(&ps_data->stk_ps_work, stk_ps_work_func);
- hrtimer_init(&ps_data->ps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
- ps_data->ps_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC);
- ps_data->ps_timer.function = stk_ps_timer_func;
- #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
- ps_data->stk_wq = create_singlethread_workqueue("stk_wq");
- INIT_WORK(&ps_data->stk_work, stk_work_func);
- err = stk3x1x_setup_irq(client);
- if(err < 0)
- goto err_stk3x1x_setup_irq;
- #endif
- err = stk3x1x_power_init(ps_data, true);
- if (err)
- goto err_power_init;
- err = stk3x1x_power_ctl(ps_data, true);
- if (err)
- goto err_power_on;
- ps_data->als_enabled = false;
- ps_data->ps_enabled = false;
- #ifdef CONFIG_HAS_EARLYSUSPEND
- ps_data->stk_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
- ps_data->stk_early_suspend.suspend = stk3x1x_early_suspend;
- ps_data->stk_early_suspend.resume = stk3x1x_late_resume;
- register_early_suspend(&ps_data->stk_early_suspend);
- #endif
- /* make sure everything is ok before registering the class device */
- ps_data->als_cdev = sensors_light_cdev;
- ps_data->als_cdev.sensors_enable = stk_als_enable_set;
- ps_data->als_cdev.sensors_poll_delay = stk_als_poll_delay_set;
- err = sensors_classdev_register(&client->dev, &ps_data->als_cdev);
- if (err)
- goto err_power_on;
- ps_data->ps_cdev = sensors_proximity_cdev;
- ps_data->ps_cdev.sensors_enable = stk_ps_enable_set;
- err = sensors_classdev_register(&client->dev, &ps_data->ps_cdev);
- if (err)
- goto err_class_sysfs;
- /* enable device power only when it is enabled */
- err = stk3x1x_power_ctl(ps_data, false);
- if (err)
- goto err_init_all_setting;
- dev_dbg(&client->dev, "%s: probe successfully", __func__);
- return 0;
- err_init_all_setting:
- stk3x1x_power_ctl(ps_data, false);
- sensors_classdev_unregister(&ps_data->ps_cdev);
- err_class_sysfs:
- sensors_classdev_unregister(&ps_data->als_cdev);
- err_power_on:
- stk3x1x_power_init(ps_data, false);
- err_power_init:
- #ifndef STK_POLL_PS
- free_irq(ps_data->irq, ps_data);
- gpio_free(plat_data->int_pin);
- #endif
- #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
- err_stk3x1x_setup_irq:
- #endif
- #ifdef STK_POLL_ALS
- hrtimer_try_to_cancel(&ps_data->als_timer);
- destroy_workqueue(ps_data->stk_als_wq);
- #endif
- destroy_workqueue(ps_data->stk_ps_wq);
- #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
- destroy_workqueue(ps_data->stk_wq);
- #endif
- sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
- err_ps_sysfs_create_group:
- sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
- err_als_sysfs_create_group:
- input_unregister_device(ps_data->ps_input_dev);
- err_ps_input_register:
- input_unregister_device(ps_data->als_input_dev);
- err_als_input_register:
- input_free_device(ps_data->ps_input_dev);
- err_ps_input_allocate:
- input_free_device(ps_data->als_input_dev);
- err_als_input_allocate:
- #ifdef STK_POLL_PS
- wake_lock_destroy(&ps_data->ps_nosuspend_wl);
- #endif
- wake_lock_destroy(&ps_data->ps_wakelock);
- mutex_destroy(&ps_data->io_lock);
- kfree(ps_data);
- return err;
- }
- static int stk3x1x_remove(struct i2c_client *client)
- {
- struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
- #ifndef STK_POLL_PS
- free_irq(ps_data->irq, ps_data);
- gpio_free(ps_data->int_pin);
- #endif
- #ifdef STK_POLL_ALS
- hrtimer_try_to_cancel(&ps_data->als_timer);
- destroy_workqueue(ps_data->stk_als_wq);
- #endif
- destroy_workqueue(ps_data->stk_ps_wq);
- #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
- destroy_workqueue(ps_data->stk_wq);
- #endif
- sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
- sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
- input_unregister_device(ps_data->ps_input_dev);
- input_unregister_device(ps_data->als_input_dev);
- input_free_device(ps_data->ps_input_dev);
- input_free_device(ps_data->als_input_dev);
- #ifdef STK_POLL_PS
- wake_lock_destroy(&ps_data->ps_nosuspend_wl);
- #endif
- wake_lock_destroy(&ps_data->ps_wakelock);
- mutex_destroy(&ps_data->io_lock);
- kfree(ps_data);
- return 0;
- }
- static const struct i2c_device_id stk_ps_id[] =
- {
- { "stk_ps", 0},
- {}
- };
- MODULE_DEVICE_TABLE(i2c, stk_ps_id);
- static struct of_device_id stk_match_table[] = {
- { .compatible = "stk,stk3x1x", },
- { },
- };
- static struct i2c_driver stk_ps_driver =
- {
- .driver = {
- .name = DEVICE_NAME,
- .owner = THIS_MODULE,
- .of_match_table = stk_match_table,
- },
- .probe = stk3x1x_probe,
- .remove = stk3x1x_remove,
- .id_table = stk_ps_id,
- };
- static int __init stk3x1x_init(void)
- {
- int ret;
- ret = i2c_add_driver(&stk_ps_driver);
- if (ret)
- return ret;
- return 0;
- }
- static void __exit stk3x1x_exit(void)
- {
- i2c_del_driver(&stk_ps_driver);
- }
- module_init(stk3x1x_init);
- module_exit(stk3x1x_exit);
- MODULE_AUTHOR("Lex Hsieh <lex_hsieh@sitronix.com.tw>");
- MODULE_DESCRIPTION("Sensortek stk3x1x Proximity Sensor driver");
- MODULE_LICENSE("GPL");
- MODULE_VERSION(DRIVER_VERSION);
|