vfs61xx.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  1. /*
  2. * Copyright 2011 Validity Sensors, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <linux/regulator/consumer.h>
  20. #include "fingerprint.h"
  21. #include "vfs61xx.h"
  22. #ifdef CONFIG_OF
  23. #include <linux/of_gpio.h>
  24. #endif
  25. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  26. #include <mach/gpio.h>
  27. #endif
  28. /* Pass to VFSSPI_IOCTL_GET_FREQ_TABLE command */
  29. /**
  30. * vfsspi_iocFreqTable - structure to get supported SPI baud rates
  31. *
  32. * @table:table which contains supported SPI baud rates
  33. * @tblSize:table size
  34. */
  35. struct vfsspi_iocFreqTable {
  36. unsigned int *table;
  37. unsigned int tblSize;
  38. };
  39. /* The spi driver private structure. */
  40. struct vfsspi_devData {
  41. dev_t devt; /* Device ID */
  42. spinlock_t vfsSpiLock; /* The lock for the spi device */
  43. struct spi_device *spi; /* The spi device */
  44. struct list_head deviceEntry; /* Device entry list */
  45. struct mutex bufferMutex; /* The lock for the transfer buffer */
  46. unsigned int isOpened; /* Indicates that driver is opened */
  47. unsigned char *buffer; /* buffer for transmitting data */
  48. unsigned char *nullBuffer; /* buffer for transmitting zeros */
  49. unsigned char *streamBuffer;
  50. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  51. unsigned int *freqTable;
  52. unsigned int freqTableSize;
  53. #endif
  54. size_t streamBufSize;
  55. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  56. /* Storing user info data (device info obtained from announce packet) */
  57. struct vfsspi_iocUserData userInfoData;
  58. #endif
  59. unsigned int drdyPin; /* DRDY GPIO pin number */
  60. unsigned int sleepPin; /* Sleep GPIO pin number */
  61. unsigned int ldo_pin; /* Ldo GPIO pin number */
  62. unsigned int ldo_pin2; /* Ldo2 GPIO pin number */
  63. /* User process ID,
  64. * to which the kernel signal indicating DRDY event is to be sent */
  65. int userPID;
  66. /* Signal ID which kernel uses to
  67. * indicating user mode driver that DRDY is asserted */
  68. int signalID;
  69. unsigned int curSpiSpeed; /* Current baud rate */
  70. bool ldo_onoff;
  71. spinlock_t irq_lock;
  72. unsigned short drdy_irq_flag;
  73. unsigned int ldocontrol;
  74. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  75. unsigned int mosipin;
  76. unsigned int misopin;
  77. unsigned int cspin;
  78. unsigned int clkpin;
  79. bool isGpio_cfgDone;
  80. bool enabled_clk;
  81. unsigned int set_altfunc;
  82. unsigned int mosi_alt;
  83. unsigned int miso_alt;
  84. unsigned int cs_alt;
  85. unsigned int clk_alt;
  86. #ifdef FEATURE_SPI_WAKELOCK
  87. struct wake_lock fp_spi_lock;
  88. #endif
  89. #endif
  90. unsigned int orient;
  91. #ifdef CONFIG_SENSORS_FINGERPRINT_SYSFS
  92. struct device *fp_device;
  93. #endif
  94. unsigned int ocp_state;
  95. struct work_struct ocp_work;
  96. unsigned int ocp_pin;
  97. unsigned int ocp_irq;
  98. unsigned int ocp_en;
  99. struct wake_lock ocp_wake_lock;
  100. struct work_struct work_debug;
  101. struct workqueue_struct *wq_dbg;
  102. struct timer_list dbg_timer;
  103. bool tz_mode;
  104. int sensortype;
  105. };
  106. struct vfsspi_devData *g_data;
  107. /* The initial baud rate for communicating with Validity sensor.
  108. * The initial clock is configured with low speed as sensor can boot
  109. * with external oscillator clock. */
  110. #define SLOW_BAUD_RATE 4800000
  111. /* Max baud rate supported by Validity sensor. */
  112. #define MAX_BAUD_RATE 9600000
  113. /* The coefficient which is multiplying with value retrieved from the
  114. * VFSSPI_IOCTL_SET_CLK IOCTL command for getting the final baud rate. */
  115. #define BAUD_RATE_COEF 1000
  116. #define DRDY_IRQ_ENABLE 1
  117. #define DRDY_IRQ_DISABLE 0
  118. #undef PLATFORM_BIG_ENDIAN
  119. #define MSM8974_SPI_TABLE
  120. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  121. #ifdef MSM8974_SPI_TABLE
  122. unsigned int freqTable[] = {
  123. 960000,
  124. 4800000,
  125. 9600000,
  126. 15000000,
  127. 19200000,
  128. 25000000,
  129. 50000000,
  130. };
  131. #else
  132. unsigned int freqTable[] = {
  133. 1100000,
  134. 5400000,
  135. 10800000,
  136. 15060000,
  137. 24000000,
  138. 25600000,
  139. 27000000,
  140. 48000000,
  141. 51200000,
  142. };
  143. #endif
  144. #endif
  145. struct spi_device *gDevSpi;
  146. struct class *vfsSpiDevClass;
  147. int gpio_irq;
  148. static int vfsspi_majorno = VFSSPI_MAJOR;
  149. static DECLARE_WAIT_QUEUE_HEAD(wq);
  150. static LIST_HEAD(deviceList);
  151. static DEFINE_MUTEX(deviceListMutex);
  152. static DEFINE_MUTEX(kernel_lock);
  153. static int dataToRead;
  154. int vfsspi_enable_irq(struct vfsspi_devData *vfsSpiDev)
  155. {
  156. if (vfsSpiDev->drdy_irq_flag == DRDY_IRQ_ENABLE)
  157. return -EINVAL;
  158. spin_lock(&vfsSpiDev->irq_lock);
  159. enable_irq(gpio_irq);
  160. vfsSpiDev->drdy_irq_flag = DRDY_IRQ_ENABLE;
  161. spin_unlock(&vfsSpiDev->irq_lock);
  162. pr_info("%s\n", __func__);
  163. return 0;
  164. }
  165. int vfsspi_disable_irq(struct vfsspi_devData *vfsSpiDev)
  166. {
  167. if (vfsSpiDev->drdy_irq_flag == DRDY_IRQ_DISABLE)
  168. return -EINVAL;
  169. spin_lock(&vfsSpiDev->irq_lock);
  170. disable_irq_nosync(gpio_irq);
  171. vfsSpiDev->drdy_irq_flag = DRDY_IRQ_DISABLE;
  172. spin_unlock(&vfsSpiDev->irq_lock);
  173. pr_info("%s\n", __func__);
  174. return 0;
  175. }
  176. void shortToLittleEndian(char *buf, size_t len)
  177. {
  178. #ifdef PLATFORM_BIG_ENDIAN
  179. int i = 0;
  180. int j = 0;
  181. char LSB, MSB;
  182. for (i = 0; i < len; i++, j++) {
  183. LSB = buf[i];
  184. i++;
  185. MSB = buf[i];
  186. buf[j] = MSB;
  187. j++;
  188. buf[j] = LSB;
  189. }
  190. #else
  191. /* Empty function */
  192. #endif
  193. } /* shortToLittleEndian */
  194. int vfsspi_sendDrdySignal(struct vfsspi_devData *vfsSpiDev)
  195. {
  196. struct task_struct *t;
  197. int ret = 0;
  198. pr_debug("%s\n", __func__);
  199. if (vfsSpiDev->userPID != 0) {
  200. rcu_read_lock();
  201. /* find the task_struct associated with userpid */
  202. pr_debug("%s Searching task with PID=%08x\n",
  203. __func__, vfsSpiDev->userPID);
  204. t = pid_task(find_pid_ns(vfsSpiDev->userPID, &init_pid_ns),
  205. PIDTYPE_PID);
  206. if (t == NULL) {
  207. pr_err("%s No such pid\n", __func__);
  208. rcu_read_unlock();
  209. return -ENODEV;
  210. }
  211. rcu_read_unlock();
  212. /* notify DRDY signal to user process */
  213. ret =
  214. send_sig_info(vfsSpiDev->signalID, (struct siginfo *)1, t);
  215. if (ret < 0)
  216. pr_err("%s Error sending signal\n", __func__);
  217. } else {
  218. pr_debug("%s pid not received yet\n", __func__);
  219. }
  220. return ret;
  221. }
  222. irqreturn_t vfsspi_irq(int irq, void *context)
  223. {
  224. struct vfsspi_devData *vfsSpiDev = context;
  225. pr_info("%s\n", __func__);
  226. if ((gpio_get_value(vfsSpiDev->drdyPin)
  227. == DRDY_ACTIVE_STATUS)) {
  228. dataToRead = 1;
  229. wake_up_interruptible(&wq);
  230. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  231. vfsspi_disable_irq(vfsSpiDev);
  232. #endif
  233. vfsspi_sendDrdySignal(vfsSpiDev);
  234. }
  235. return IRQ_HANDLED;
  236. }
  237. /* Return no.of bytes written to device. Negative number for errors */
  238. static inline ssize_t vfsspi_writeSync(struct vfsspi_devData *vfsSpiDev,
  239. unsigned char *buf, size_t len)
  240. {
  241. int status = 0;
  242. struct spi_message m;
  243. struct spi_transfer t;
  244. pr_debug("%s\n", __func__);
  245. spi_message_init(&m);
  246. memset(&t, 0, sizeof(t));
  247. t.rx_buf = NULL;
  248. t.tx_buf = buf;
  249. t.len = len;
  250. t.speed_hz = vfsSpiDev->curSpiSpeed;
  251. spi_message_add_tail(&t, &m);
  252. status = spi_sync(vfsSpiDev->spi, &m);
  253. if (status == 0)
  254. status = m.actual_length;
  255. pr_debug("%s vfsspi_writeSync,length=%d\n",
  256. __func__, m.actual_length);
  257. return status;
  258. }
  259. /* Return no.of bytes read >0. negative integer incase of error. */
  260. inline ssize_t vfsspi_readSync(struct vfsspi_devData *vfsSpiDev,
  261. unsigned char *buf, size_t len)
  262. {
  263. int status = 0;
  264. struct spi_message m;
  265. struct spi_transfer t;
  266. pr_debug("%s\n", __func__);
  267. spi_message_init(&m);
  268. memset(&t, 0x0, sizeof(t));
  269. t.tx_buf = NULL;
  270. t.rx_buf = buf;
  271. t.len = len;
  272. t.speed_hz = vfsSpiDev->curSpiSpeed;
  273. spi_message_add_tail(&t, &m);
  274. status = spi_sync(vfsSpiDev->spi, &m);
  275. pr_debug("%s: status=%d\n", __func__, status);
  276. if (status == 0) {
  277. pr_debug("%s: m.actual_length=%d\n",
  278. __func__, m.actual_length);
  279. /* FIXME: This is temporary workaround for Fluid,
  280. instead of returning actual read data length
  281. spi_sync is returning 0 */
  282. status = len;
  283. }
  284. pr_debug("%s length=%d\n",
  285. __func__, len);
  286. return status;
  287. }
  288. ssize_t vfsspi_write(struct file *filp, const char __user *buf, size_t count,
  289. loff_t *fPos)
  290. {
  291. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  292. return 0;
  293. #else
  294. struct vfsspi_devData *vfsSpiDev = NULL;
  295. ssize_t status = 0;
  296. pr_debug("%s\n", __func__);
  297. if (count > DEFAULT_BUFFER_SIZE || count == 0)
  298. return -EMSGSIZE;
  299. vfsSpiDev = filp->private_data;
  300. mutex_lock(&vfsSpiDev->bufferMutex);
  301. if (vfsSpiDev->buffer) {
  302. unsigned long missing = 0;
  303. missing = copy_from_user(vfsSpiDev->buffer, buf, count);
  304. shortToLittleEndian((char *)vfsSpiDev->buffer, count);
  305. if (missing == 0)
  306. status = vfsspi_writeSync(vfsSpiDev,
  307. vfsSpiDev->buffer, count);
  308. else
  309. status = -EFAULT;
  310. }
  311. mutex_unlock(&vfsSpiDev->bufferMutex);
  312. return status;
  313. #endif
  314. }
  315. ssize_t vfsspi_read(struct file *filp, char __user *buf, size_t count,
  316. loff_t *fPos)
  317. {
  318. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  319. return 0;
  320. #else
  321. struct vfsspi_devData *vfsSpiDev = NULL;
  322. unsigned char *readBuf = NULL;
  323. ssize_t status = 0;
  324. pr_debug("%s\n", __func__);
  325. vfsSpiDev = filp->private_data;
  326. if (vfsSpiDev->streamBuffer != NULL
  327. && count <= vfsSpiDev->streamBufSize)
  328. readBuf = vfsSpiDev->streamBuffer;
  329. else if (count <= DEFAULT_BUFFER_SIZE)
  330. readBuf = vfsSpiDev->buffer;
  331. else
  332. return -EMSGSIZE;
  333. if (buf == NULL)
  334. return -EFAULT;
  335. mutex_lock(&vfsSpiDev->bufferMutex);
  336. status = vfsspi_readSync(vfsSpiDev, readBuf, count);
  337. if (status > 0) {
  338. unsigned long missing = 0;
  339. /* data read. Copy to user buffer. */
  340. shortToLittleEndian((char *)readBuf, status);
  341. missing = copy_to_user(buf, readBuf, status);
  342. if (missing == status) {
  343. pr_err("%s copy_to_user failed\n", __func__);
  344. /* Nothing was copied to user space buffer. */
  345. status = -EFAULT;
  346. } else {
  347. status = status - missing;
  348. pr_debug("%s status=%d\n", __func__, status);
  349. }
  350. } else
  351. pr_err("%s err status=%d\n", __func__, status);
  352. mutex_unlock(&vfsSpiDev->bufferMutex);
  353. return status;
  354. #endif
  355. }
  356. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  357. int vfsspi_xfer(struct vfsspi_devData *vfsSpiDev, struct vfsspi_iocTransfer *tr)
  358. {
  359. int status = 0;
  360. struct spi_message m;
  361. struct spi_transfer t;
  362. pr_debug("%s\n", __func__);
  363. if (vfsSpiDev == NULL || tr == NULL)
  364. return -EFAULT;
  365. if (tr->len > DEFAULT_BUFFER_SIZE || tr->len <= 0)
  366. return -EMSGSIZE;
  367. if (tr->txBuffer != NULL) {
  368. if (copy_from_user(vfsSpiDev->nullBuffer, tr->txBuffer, tr->len)
  369. != 0) {
  370. pr_err("%s copy_from_user err\n", __func__);
  371. return -EFAULT;
  372. }
  373. shortToLittleEndian((char *)vfsSpiDev->nullBuffer, tr->len);
  374. } else
  375. pr_err("%s tr->txBuffer is NULL\n", __func__);
  376. spi_message_init(&m);
  377. memset(&t, 0, sizeof(t));
  378. t.tx_buf = vfsSpiDev->nullBuffer;
  379. t.rx_buf = vfsSpiDev->buffer;
  380. t.len = tr->len;
  381. t.speed_hz = vfsSpiDev->curSpiSpeed;
  382. spi_message_add_tail(&t, &m);
  383. status = spi_sync(vfsSpiDev->spi, &m);
  384. if (status == 0) {
  385. if (tr->rxBuffer != NULL) {
  386. unsigned missing = 0;
  387. shortToLittleEndian((char *)vfsSpiDev->buffer, tr->len);
  388. missing =
  389. copy_to_user(tr->rxBuffer, vfsSpiDev->buffer,
  390. tr->len);
  391. if (missing != 0)
  392. tr->len = tr->len - missing;
  393. }
  394. }
  395. pr_debug("%s length=%d, status=%d\n",
  396. __func__, tr->len, status);
  397. return status;
  398. }
  399. #endif
  400. void vfsspi_pin_control(bool pin_set)
  401. {
  402. if (pin_set)
  403. gpio_tlmm_config(GPIO_CFG(g_data->drdyPin, 0,
  404. GPIO_CFG_INPUT, GPIO_CFG_PULL_UP,
  405. GPIO_CFG_2MA), 1);
  406. else
  407. gpio_tlmm_config(GPIO_CFG(g_data->drdyPin, 0,
  408. GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN,
  409. GPIO_CFG_2MA), 1);
  410. }
  411. void vfsspi_regulator_onoff(struct vfsspi_devData *vfsSpiDev, bool onoff)
  412. {
  413. if (vfsSpiDev->ldo_pin) {
  414. if (vfsSpiDev->ldocontrol) {
  415. if (onoff) {
  416. vfsspi_pin_control(true);
  417. if (vfsSpiDev->ocp_en) {
  418. gpio_set_value(vfsSpiDev->ocp_en, 1);
  419. usleep_range(2950, 3000);
  420. }
  421. gpio_set_value(vfsSpiDev->ldo_pin, 1);
  422. if (vfsSpiDev->ldo_pin2) {
  423. mdelay(1);
  424. gpio_set_value(vfsSpiDev->ldo_pin2, 1);
  425. }
  426. } else {
  427. if (vfsSpiDev->ldo_pin2) {
  428. gpio_set_value(vfsSpiDev->ldo_pin2, 0);
  429. mdelay(1);
  430. }
  431. gpio_set_value(vfsSpiDev->ldo_pin, 0);
  432. if (vfsSpiDev->ocp_en)
  433. gpio_set_value(vfsSpiDev->ocp_en, 0);
  434. vfsspi_pin_control(false);
  435. }
  436. vfsSpiDev->ldo_onoff = onoff;
  437. pr_info("%s: %s ocp_en: %s\n",
  438. __func__, onoff ? "on" : "off",
  439. vfsSpiDev->ocp_en ? "enable" : "disable");
  440. } else
  441. pr_info("%s: can't control in this revion\n", __func__);
  442. }
  443. }
  444. void vfsspi_suspend(struct vfsspi_devData *vfsSpiDev)
  445. {
  446. if (vfsSpiDev != NULL) {
  447. spin_lock(&vfsSpiDev->vfsSpiLock);
  448. dataToRead = 0;
  449. gpio_set_value(vfsSpiDev->sleepPin, 1);
  450. spin_unlock(&vfsSpiDev->vfsSpiLock);
  451. pr_info("%s\n", __func__);
  452. }
  453. }
  454. void vfsspi_hardReset(struct vfsspi_devData *vfsSpiDev)
  455. {
  456. if (vfsSpiDev != NULL) {
  457. dataToRead = 0;
  458. if (gpio_get_value(vfsSpiDev->sleepPin)) {
  459. gpio_set_value(vfsSpiDev->sleepPin, 0);
  460. usleep_range(3000, 3100);
  461. }
  462. gpio_set_value(vfsSpiDev->sleepPin, 1);
  463. mdelay(1); /* for exact time of reset */
  464. gpio_set_value(vfsSpiDev->sleepPin, 0);
  465. usleep_range(5000, 5100);
  466. pr_info("%s\n", __func__);
  467. }
  468. }
  469. #undef TEST_FIXED_FREQUENCY
  470. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  471. static void vfsspi_gpio_config(struct vfsspi_devData *data, int onoff)
  472. {
  473. if (onoff) {
  474. if (data->set_altfunc) {
  475. pr_info("%s: set_altfunc\n", __func__);
  476. if (data->mosipin)
  477. gpio_tlmm_config(GPIO_CFG(data->mosipin,
  478. data->mosi_alt, GPIO_CFG_INPUT,
  479. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), 1);
  480. else
  481. pr_err("%s: can't get mosipin\n", __func__);
  482. if (data->misopin)
  483. gpio_tlmm_config(GPIO_CFG(data->misopin,
  484. data->miso_alt, GPIO_CFG_INPUT,
  485. GPIO_CFG_PULL_UP, GPIO_CFG_2MA), 1);
  486. else
  487. pr_err("%s: can't get misopin\n", __func__);
  488. if (data->cspin)
  489. gpio_tlmm_config(GPIO_CFG(data->cspin,
  490. data->cs_alt, GPIO_CFG_INPUT,
  491. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), 1);
  492. else
  493. pr_err("%s: can't get cspin\n", __func__);
  494. if (data->clkpin)
  495. gpio_tlmm_config(GPIO_CFG(data->clkpin,
  496. data->clk_alt, GPIO_CFG_INPUT,
  497. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), 1);
  498. else
  499. pr_err("%s: can't get clkpin\n", __func__);
  500. } else { /* initial code for K project*/
  501. if (data->mosipin)
  502. gpio_tlmm_config(GPIO_CFG(data->mosipin, 2,
  503. GPIO_CFG_INPUT, GPIO_CFG_NO_PULL,
  504. GPIO_CFG_2MA), 1);
  505. else
  506. pr_err("%s: can't get mosipin\n", __func__);
  507. if (data->misopin)
  508. gpio_tlmm_config(GPIO_CFG(data->misopin, 2,
  509. GPIO_CFG_INPUT, GPIO_CFG_PULL_UP,
  510. GPIO_CFG_2MA), 1);
  511. else
  512. pr_err("%s: can't get misopin\n", __func__);
  513. if (data->cspin)
  514. gpio_tlmm_config(GPIO_CFG(data->cspin, 2,
  515. GPIO_CFG_INPUT, GPIO_CFG_NO_PULL,
  516. GPIO_CFG_2MA), 1);
  517. else
  518. pr_err("%s: can't get cspin\n", __func__);
  519. if (data->clkpin)
  520. gpio_tlmm_config(GPIO_CFG(data->clkpin, 3,
  521. GPIO_CFG_INPUT, GPIO_CFG_NO_PULL,
  522. GPIO_CFG_2MA), 1);
  523. else
  524. pr_err("%s: can't get clkpin\n", __func__);
  525. }
  526. } else {
  527. if (data->mosipin)
  528. gpio_tlmm_config(GPIO_CFG(data->mosipin, 0,
  529. GPIO_CFG_INPUT, GPIO_CFG_NO_PULL,
  530. GPIO_CFG_4MA), 1);
  531. else
  532. pr_err("%s: can't get mosipin\n", __func__);
  533. if (data->misopin)
  534. gpio_tlmm_config(GPIO_CFG(data->misopin, 0,
  535. GPIO_CFG_INPUT, GPIO_CFG_NO_PULL,
  536. GPIO_CFG_4MA), 1);
  537. else
  538. pr_err("%s: can't get misopin\n", __func__);
  539. if (data->cspin)
  540. gpio_tlmm_config(GPIO_CFG(data->cspin, 0,
  541. GPIO_CFG_INPUT, GPIO_CFG_NO_PULL,
  542. GPIO_CFG_4MA), 1);
  543. else
  544. pr_err("%s: can't get cspin\n", __func__);
  545. if (data->clkpin)
  546. gpio_tlmm_config(GPIO_CFG(data->clkpin, 0,
  547. GPIO_CFG_INPUT, GPIO_CFG_NO_PULL,
  548. GPIO_CFG_4MA), 1);
  549. else
  550. pr_err("%s: can't get clkpin\n", __func__);
  551. }
  552. pr_info("%s : %s\n", __func__, onoff ? "set" : "reset");
  553. }
  554. #endif
  555. long vfsspi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  556. {
  557. int retVal = 0;
  558. struct vfsspi_devData *vfsSpiDev = NULL;
  559. struct vfsspi_iocRegSignal usrSignal;
  560. unsigned short clock = 0;
  561. unsigned short drdy_enable_flag;
  562. struct spi_device *spidev = NULL;
  563. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  564. struct vfsspi_iocTransfer *dup = NULL;
  565. struct vfsspi_iocUserData tmpUserData;
  566. unsigned int streamDataSize;
  567. struct vfsspi_iocFreqTable tmpFreqData;
  568. #endif
  569. pr_debug("%s\n", __func__);
  570. if (_IOC_TYPE(cmd) != VFSSPI_IOCTL_MAGIC) {
  571. pr_err("%s invalid cmd= 0x%X Received= 0x%X Expected= 0x%X\n",
  572. __func__, cmd, _IOC_TYPE(cmd), VFSSPI_IOCTL_MAGIC);
  573. return -ENOTTY;
  574. }
  575. vfsSpiDev = filp->private_data;
  576. mutex_lock(&vfsSpiDev->bufferMutex);
  577. switch (cmd) {
  578. case VFSSPI_IOCTL_DEVICE_SUSPEND:
  579. pr_debug("VFSSPI_IOCTL_DEVICE_SUSPEND:\n");
  580. vfsspi_suspend(vfsSpiDev);
  581. break;
  582. case VFSSPI_IOCTL_DEVICE_RESET:
  583. pr_debug("%s VFSSPI_IOCTL_DEVICE_RESET:\n", __func__);
  584. vfsspi_hardReset(vfsSpiDev);
  585. break;
  586. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  587. case VFSSPI_IOCTL_RW_SPI_MESSAGE:
  588. pr_debug("%s VFSSPI_IOCTL_RW_SPI_MESSAGE\n", __func__);
  589. dup = kmalloc(sizeof(struct vfsspi_iocTransfer), GFP_KERNEL);
  590. if (dup != NULL) {
  591. if (copy_from_user(dup, (void *)arg,
  592. sizeof(struct vfsspi_iocTransfer)) == 0) {
  593. retVal = vfsspi_xfer(vfsSpiDev, dup);
  594. if (retVal == 0) {
  595. if (copy_to_user((void *)arg, dup,
  596. sizeof(
  597. struct vfsspi_iocTransfer))
  598. != 0) {
  599. pr_err("%s copy to user fail\n",
  600. __func__);
  601. retVal = -EFAULT;
  602. }
  603. }
  604. } else {
  605. pr_err("%s copy from user fail\n", __func__);
  606. retVal = -EFAULT;
  607. }
  608. kfree(dup);
  609. } else
  610. retVal = -ENOMEM;
  611. break;
  612. #endif
  613. case VFSSPI_IOCTL_SET_CLK:
  614. pr_debug("%s VFSSPI_IOCTL_SET_CLK", __func__);
  615. if (copy_from_user(&clock, (void *)arg,
  616. sizeof(unsigned short)) == 0) {
  617. spin_lock_irq(&vfsSpiDev->vfsSpiLock);
  618. spidev = spi_dev_get(vfsSpiDev->spi);
  619. spin_unlock_irq(&vfsSpiDev->vfsSpiLock);
  620. pr_debug("%s: clock=%d\n", __func__, clock);
  621. if (spidev != NULL) {
  622. switch (clock) {
  623. case 0:
  624. /* Running baud rate. */
  625. pr_debug("%s Running baud rate.\n",
  626. __func__);
  627. spidev->max_speed_hz = MAX_BAUD_RATE;
  628. vfsSpiDev->curSpiSpeed = MAX_BAUD_RATE;
  629. break;
  630. case 0xFFFF:
  631. /* Slow baud rate */
  632. pr_debug("%s slow baud rate.\n",
  633. __func__);
  634. spidev->max_speed_hz = SLOW_BAUD_RATE;
  635. vfsSpiDev->curSpiSpeed = SLOW_BAUD_RATE;
  636. break;
  637. default:
  638. pr_debug("%s baud rate is %d.\n",
  639. __func__, clock);
  640. #ifdef TEST_FIXED_FREQUENCY
  641. if (clock <= 4800)
  642. vfsSpiDev->curSpiSpeed =
  643. SLOW_BAUD_RATE;
  644. if (vfsSpiDev->curSpiSpeed
  645. > MAX_BAUD_RATE)
  646. vfsSpiDev->curSpiSpeed
  647. = MAX_BAUD_RATE;
  648. spidev->max_speed_hz =
  649. vfsSpiDev->curSpiSpeed;
  650. #else
  651. vfsSpiDev->curSpiSpeed =
  652. clock * BAUD_RATE_COEF;
  653. if (vfsSpiDev->curSpiSpeed
  654. > MAX_BAUD_RATE)
  655. vfsSpiDev->curSpiSpeed
  656. = MAX_BAUD_RATE;
  657. spidev->max_speed_hz =
  658. vfsSpiDev->curSpiSpeed;
  659. #endif
  660. break;
  661. }
  662. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  663. if (!vfsSpiDev->enabled_clk) {
  664. pr_info("%s ENABLE_SPI_CLOCK\n", __func__);
  665. retVal = fp_spi_clock_enable(spidev);
  666. if (retVal < 0)
  667. pr_err("%s: Unable to enable spi clk\n",
  668. __func__);
  669. else {
  670. retVal = fp_spi_clock_set_rate(spidev);
  671. if (retVal < 0)
  672. pr_err("%s: Unable to set spi clk rate\n",
  673. __func__);
  674. }
  675. usleep_range(950, 1000);
  676. #ifdef FEATURE_SPI_WAKELOCK
  677. wake_lock(&vfsSpiDev->fp_spi_lock);
  678. #endif
  679. vfsSpiDev->enabled_clk = true;
  680. }
  681. #endif
  682. spi_dev_put(spidev);
  683. }
  684. } else {
  685. pr_err("%s Failed copy from user.\n", __func__);
  686. retVal = -EFAULT;
  687. }
  688. break;
  689. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  690. case VFSSPI_IOCTL_CHECK_DRDY:
  691. retVal = -ETIMEDOUT;
  692. pr_debug("%s: VFSSPI_IOCTL_CHECK_DRDY",
  693. __func__);
  694. dataToRead = 0;
  695. if (gpio_get_value(vfsSpiDev->drdyPin)
  696. == DRDY_ACTIVE_STATUS) {
  697. retVal = 0;
  698. } else {
  699. unsigned long timeout =
  700. msecs_to_jiffies(DRDY_TIMEOUT_MS);
  701. unsigned long startTime = jiffies;
  702. unsigned long endTime = 0;
  703. do {
  704. wait_event_interruptible_timeout(wq,
  705. dataToRead != 0, timeout);
  706. dataToRead = 0;
  707. if (gpio_get_value(vfsSpiDev->drdyPin)
  708. == DRDY_ACTIVE_STATUS) {
  709. retVal = 0;
  710. break;
  711. }
  712. endTime = jiffies;
  713. /* Timed out for waiting to wake up event. */
  714. if (endTime - startTime >= timeout)
  715. timeout = 0;
  716. else {
  717. /* Thread is woke up by spurious event.
  718. Calculate a new timeout and
  719. continue to wait DRDY signal assertion. */
  720. timeout -= endTime - startTime;
  721. startTime = endTime;
  722. }
  723. } while (timeout > 0);
  724. }
  725. dataToRead = 0;
  726. break;
  727. #endif
  728. case VFSSPI_IOCTL_REGISTER_DRDY_SIGNAL:
  729. pr_debug("%s VFSSPI_IOCTL_REGISTER_DRDY_SIGNAL\n", __func__);
  730. if (copy_from_user(&usrSignal, (void *)arg,
  731. sizeof(usrSignal)) != 0) {
  732. pr_err("%s Failed copy from user.\n", __func__);
  733. retVal = -EFAULT;
  734. } else {
  735. vfsSpiDev->userPID = usrSignal.userPID;
  736. vfsSpiDev->signalID = usrSignal.signalID;
  737. }
  738. break;
  739. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  740. case VFSSPI_IOCTL_SET_USER_DATA:
  741. pr_debug("%s VFSSPI_IOCTL_SET_USER_DATA\n", __func__);
  742. if ((void *)arg == NULL) {
  743. pr_err("%s VFSSPI_IOCTL_SET_USER_DATA is failed\n",
  744. __func__);
  745. retVal = -EINVAL;
  746. break;
  747. }
  748. if (copy_from_user(&tmpUserData, (void *)arg,
  749. sizeof(tmpUserData)) == 0) {
  750. if (vfsSpiDev->userInfoData.buffer != NULL)
  751. kfree(vfsSpiDev->userInfoData.buffer);
  752. vfsSpiDev->userInfoData.buffer =
  753. kmalloc(tmpUserData.len, GFP_KERNEL);
  754. if (vfsSpiDev->userInfoData.buffer != NULL) {
  755. vfsSpiDev->userInfoData.len =
  756. tmpUserData.len;
  757. if (tmpUserData.buffer != NULL) {
  758. if (copy_from_user
  759. (vfsSpiDev->userInfoData.buffer,
  760. tmpUserData.buffer,
  761. tmpUserData.len) != 0) {
  762. pr_err("%s cp from user fail\n",
  763. __func__);
  764. retVal = -EFAULT;
  765. }
  766. } else {
  767. retVal = -EFAULT;
  768. }
  769. } else {
  770. retVal = -ENOMEM;
  771. }
  772. } else {
  773. pr_err("%s copy from user failed\n", __func__);
  774. retVal = -EFAULT;
  775. }
  776. break;
  777. case VFSSPI_IOCTL_GET_USER_DATA:
  778. retVal = -EFAULT;
  779. pr_debug("%s VFSSPI_IOCTL_GET_USER_DATA\n", __func__);
  780. if (vfsSpiDev->userInfoData.buffer != NULL
  781. && (void *)arg != NULL) {
  782. if (copy_from_user(&tmpUserData, (void *)arg,
  783. sizeof(struct vfsspi_iocUserData)) != 0) {
  784. pr_err("%s copy from user failed\n", __func__);
  785. break;
  786. }
  787. if (tmpUserData.len ==
  788. vfsSpiDev->userInfoData.len
  789. && tmpUserData.buffer != NULL) {
  790. pr_err("%s userInfoData incorrect\n", __func__);
  791. break;
  792. }
  793. if (copy_to_user(tmpUserData.buffer,
  794. vfsSpiDev->userInfoData.buffer,
  795. tmpUserData.len) == 0) {
  796. pr_err("%s copy to user fail\n", __func__);
  797. break;
  798. }
  799. if (copy_to_user((void *)arg,
  800. &(tmpUserData),
  801. sizeof(struct vfsspi_iocUserData))
  802. != 0) {
  803. pr_err("%s cp to user fail\n", __func__);
  804. break;
  805. }
  806. retVal = 0;
  807. } else
  808. pr_err("%s VFSSPI_IOCTL_GET_USER_DATA failed\n",
  809. __func__);
  810. break;
  811. #endif
  812. case VFSSPI_IOCTL_SET_DRDY_INT:
  813. pr_debug("%s VFSSPI_IOCTL_SET_DRDY_INT\n", __func__);
  814. if (copy_from_user(&drdy_enable_flag,
  815. (void *)arg, sizeof(drdy_enable_flag)) != 0) {
  816. pr_err("%s Failed copy from user.\n", __func__);
  817. retVal = -EFAULT;
  818. } else {
  819. if (drdy_enable_flag == 0)
  820. vfsspi_disable_irq(vfsSpiDev);
  821. else
  822. vfsspi_enable_irq(vfsSpiDev);
  823. }
  824. break;
  825. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  826. case VFSSPI_IOCTL_STREAM_READ_START:
  827. pr_debug("VFSSPI_IOCTL_STREAM_READ_START");
  828. if (copy_from_user(&streamDataSize, (void *)arg,
  829. sizeof(unsigned int)) != 0) {
  830. pr_err("Failed copy from user.\n");
  831. retVal = -EFAULT;
  832. } else {
  833. if (vfsSpiDev->streamBuffer != NULL)
  834. kfree(vfsSpiDev->streamBuffer);
  835. vfsSpiDev->streamBuffer =
  836. kmalloc(streamDataSize, GFP_KERNEL);
  837. if (vfsSpiDev->streamBuffer == NULL)
  838. retVal = -ENOMEM;
  839. else
  840. vfsSpiDev->streamBufSize = streamDataSize;
  841. }
  842. break;
  843. case VFSSPI_IOCTL_STREAM_READ_STOP:
  844. pr_debug("VFSSPI_IOCTL_STREAM_READ_STOP");
  845. if (vfsSpiDev->streamBuffer != NULL) {
  846. kfree(vfsSpiDev->streamBuffer);
  847. vfsSpiDev->streamBuffer = NULL;
  848. vfsSpiDev->streamBufSize = 0;
  849. }
  850. break;
  851. case VFSSPI_IOCTL_GET_FREQ_TABLE:
  852. pr_debug("%s: VFSSPI_IOCTL_GET_FREQ_TABLE\n",
  853. __func__);
  854. retVal = -EINVAL;
  855. if (copy_from_user(&tmpFreqData, (void *)arg,
  856. sizeof(tmpFreqData)) != 0) {
  857. pr_err("Failed copy from user.\n");
  858. break;
  859. }
  860. tmpFreqData.tblSize = 0;
  861. if (vfsSpiDev->freqTable != NULL) {
  862. tmpFreqData.tblSize = vfsSpiDev->freqTableSize;
  863. if (tmpFreqData.table != NULL) {
  864. if (copy_to_user(tmpFreqData.table,
  865. vfsSpiDev->freqTable,
  866. vfsSpiDev->freqTableSize) != 0) {
  867. pr_err("Failed copy to user.\n");
  868. break;
  869. }
  870. }
  871. }
  872. if (copy_to_user((void *)arg, &(tmpFreqData),
  873. sizeof(tmpFreqData)) == 0)
  874. retVal = 0;
  875. else
  876. pr_err("copy to user failed\n");
  877. break;
  878. #endif
  879. case VFSSPI_IOCTL_POWER_ON:
  880. pr_debug("%s VFSSPI_IOCTL_POWER_ON\n", __func__);
  881. if (vfsSpiDev->ldocontrol && !vfsSpiDev->ldo_onoff
  882. && !vfsSpiDev->ocp_state) {
  883. vfsspi_regulator_onoff(vfsSpiDev, true);
  884. } else
  885. pr_info("%s can't turn on ldo in this rev, or already on\n",
  886. __func__);
  887. if (vfsSpiDev->ocp_state)
  888. pr_info("%s ocp flag high\n", __func__);
  889. break;
  890. case VFSSPI_IOCTL_POWER_OFF:
  891. pr_debug("%s VFSSPI_IOCTL_POWER_OFF\n", __func__);
  892. if (vfsSpiDev->ldocontrol && vfsSpiDev->ldo_onoff) {
  893. vfsspi_regulator_onoff(vfsSpiDev, false);
  894. /* prevent floating */
  895. gpio_set_value(vfsSpiDev->sleepPin, 0);
  896. } else
  897. pr_info("%s can't turn off ldo in this rev, or already off\n",
  898. __func__);
  899. if (vfsSpiDev->ocp_state)
  900. pr_info("%s ocp flag high\n", __func__);
  901. break;
  902. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  903. case VFSSPI_IOCTL_DISABLE_SPI_CLOCK:
  904. if (vfsSpiDev->enabled_clk) {
  905. pr_info("%s DISABLE_SPI_CLOCK\n", __func__);
  906. spin_lock_irq(&vfsSpiDev->vfsSpiLock);
  907. spidev = spi_dev_get(vfsSpiDev->spi);
  908. spin_unlock_irq(&vfsSpiDev->vfsSpiLock);
  909. retVal = fp_spi_clock_disable(spidev);
  910. if (retVal < 0)
  911. pr_err("%s: couldn't disable spi clks\n", __func__);
  912. spi_dev_put(spidev);
  913. usleep_range(950, 1000);
  914. #ifdef FEATURE_SPI_WAKELOCK
  915. wake_unlock(&vfsSpiDev->fp_spi_lock);
  916. #endif
  917. vfsSpiDev->enabled_clk = false;
  918. }
  919. break;
  920. case VFSSPI_IOCTL_SET_SPI_CONFIGURATION:
  921. if (!vfsSpiDev->isGpio_cfgDone) {
  922. pr_debug("%s SET_SPI_CONFIGURATION\n", __func__);
  923. vfsspi_gpio_config(vfsSpiDev, 1);
  924. vfsSpiDev->isGpio_cfgDone = true;
  925. }
  926. break;
  927. case VFSSPI_IOCTL_RESET_SPI_CONFIGURATION:
  928. /* for TZ temporary function test */
  929. /* vfsspi_gpio_config(vfsSpiDev, 0); */
  930. break;
  931. #endif
  932. case VFSSPI_IOCTL_GET_SENSOR_ORIENT:
  933. pr_debug("%s: orient is %d(0: normal, 1: upsidedown)\n",
  934. __func__, vfsSpiDev->orient);
  935. if (copy_to_user((void *)arg,
  936. &(vfsSpiDev->orient),
  937. sizeof(vfsSpiDev->orient))
  938. != 0) {
  939. retVal = -EFAULT;
  940. pr_err("%s cp to user fail\n", __func__);
  941. }
  942. break;
  943. default:
  944. retVal = -EFAULT;
  945. break;
  946. }
  947. mutex_unlock(&vfsSpiDev->bufferMutex);
  948. return retVal;
  949. }
  950. int vfsspi_open(struct inode *inode, struct file *filp)
  951. {
  952. struct vfsspi_devData *vfsSpiDev = NULL;
  953. int status = -ENXIO;
  954. pr_info("%s\n", __func__);
  955. mutex_lock(&kernel_lock);
  956. mutex_lock(&deviceListMutex);
  957. list_for_each_entry(vfsSpiDev, &deviceList, deviceEntry) {
  958. if (vfsSpiDev->devt == inode->i_rdev) {
  959. status = 0;
  960. break;
  961. }
  962. }
  963. if (!vfsSpiDev->ocp_state) {
  964. vfsspi_regulator_onoff(vfsSpiDev, true);
  965. msleep(1000);
  966. }
  967. if (status == 0) {
  968. if (vfsSpiDev->isOpened != 0) {
  969. status = -EBUSY;
  970. } else {
  971. vfsSpiDev->userPID = 0;
  972. if (vfsSpiDev->buffer == NULL) {
  973. vfsSpiDev->nullBuffer =
  974. kmalloc(DEFAULT_BUFFER_SIZE, GFP_KERNEL);
  975. vfsSpiDev->buffer =
  976. kmalloc(DEFAULT_BUFFER_SIZE, GFP_KERNEL);
  977. if (vfsSpiDev->buffer == NULL
  978. || vfsSpiDev->nullBuffer == NULL) {
  979. status = -ENOMEM;
  980. } else {
  981. vfsSpiDev->isOpened = 1;
  982. filp->private_data = vfsSpiDev;
  983. nonseekable_open(inode, filp);
  984. }
  985. }
  986. }
  987. }
  988. mutex_unlock(&deviceListMutex);
  989. mutex_unlock(&kernel_lock);
  990. pr_debug("%s drdy(%d)=%d, sleepPin(%d)=%d\n", __func__,
  991. vfsSpiDev->drdyPin, gpio_get_value(vfsSpiDev->drdyPin),
  992. vfsSpiDev->sleepPin, gpio_get_value(vfsSpiDev->sleepPin));
  993. return status;
  994. }
  995. int vfsspi_release(struct inode *inode, struct file *filp)
  996. {
  997. struct vfsspi_devData *vfsSpiDev = NULL;
  998. int status = 0;
  999. pr_info("%s\n", __func__);
  1000. mutex_lock(&deviceListMutex);
  1001. vfsSpiDev = filp->private_data;
  1002. filp->private_data = NULL;
  1003. vfsSpiDev->isOpened = 0;
  1004. if (vfsSpiDev->buffer != NULL) {
  1005. kfree(vfsSpiDev->buffer);
  1006. vfsSpiDev->buffer = NULL;
  1007. }
  1008. if (vfsSpiDev->nullBuffer != NULL) {
  1009. kfree(vfsSpiDev->nullBuffer);
  1010. vfsSpiDev->nullBuffer = NULL;
  1011. }
  1012. if (vfsSpiDev->streamBuffer != NULL) {
  1013. kfree(vfsSpiDev->streamBuffer);
  1014. vfsSpiDev->streamBuffer = NULL;
  1015. vfsSpiDev->streamBufSize = 0;
  1016. }
  1017. if (vfsSpiDev->ldo_onoff)
  1018. vfsspi_regulator_onoff(vfsSpiDev, false);
  1019. mutex_unlock(&deviceListMutex);
  1020. return status;
  1021. }
  1022. static void vfsspi_ocp_work_func(struct work_struct *work)
  1023. {
  1024. struct vfsspi_devData *vfsSpiDev =
  1025. container_of((struct work_struct *)work,
  1026. struct vfsspi_devData, ocp_work);
  1027. if (!gpio_get_value(vfsSpiDev->ocp_pin)) {
  1028. pr_info("%s ldo off!!!\n", __func__);
  1029. if (vfsSpiDev->ldo_pin2) {
  1030. gpio_set_value(vfsSpiDev->ldo_pin2, 0);
  1031. mdelay(1);
  1032. }
  1033. gpio_set_value(vfsSpiDev->ldo_pin, 0);
  1034. vfsSpiDev->ocp_state = 1;
  1035. disable_irq_wake(vfsSpiDev->ocp_irq);
  1036. disable_irq(vfsSpiDev->ocp_irq);
  1037. }
  1038. }
  1039. static irqreturn_t vfsspi_ocp_irq_thread(int irq, void *dev)
  1040. {
  1041. wake_lock_timeout(&g_data->ocp_wake_lock, 1 * HZ);
  1042. schedule_work(&g_data->ocp_work);
  1043. return IRQ_HANDLED;
  1044. }
  1045. int vfsspi_platformInit(struct vfsspi_devData *vfsSpiDev)
  1046. {
  1047. int status = 0;
  1048. pr_info("%s\n", __func__);
  1049. if (vfsSpiDev != NULL) {
  1050. if (vfsSpiDev->ocp_en) {
  1051. status = gpio_request(vfsSpiDev->ocp_en, "vfsspi_ocp_en");
  1052. if (status < 0) {
  1053. pr_err("%s gpio_request vfsspi_ocp_en failed\n",
  1054. __func__);
  1055. goto done;
  1056. }
  1057. gpio_direction_output(vfsSpiDev->ocp_en, 0);
  1058. pr_info("%s ocp off\n", __func__);
  1059. }
  1060. status = gpio_request(vfsSpiDev->ldo_pin, "vfsspi_ldo_en");
  1061. if (status < 0) {
  1062. pr_err("%s gpio_request vfsspi_ldo_en failed\n",
  1063. __func__);
  1064. goto done;
  1065. }
  1066. if (vfsSpiDev->ldo_pin2) {
  1067. status = gpio_request(vfsSpiDev->ldo_pin2, "vfsspi_ldo_en2");
  1068. if (status < 0) {
  1069. pr_err("%s gpio_request vfsspi_ldo_en2 failed\n",
  1070. __func__);
  1071. goto done;
  1072. }
  1073. }
  1074. if (vfsSpiDev->ldocontrol) {
  1075. gpio_direction_output(vfsSpiDev->ldo_pin, 0);
  1076. if (vfsSpiDev->ldo_pin2) {
  1077. mdelay(1);
  1078. gpio_direction_output(vfsSpiDev->ldo_pin2, 0);
  1079. }
  1080. pr_info("%s ldo off\n", __func__);
  1081. } else {
  1082. if (vfsSpiDev->ldo_pin2) {
  1083. gpio_direction_output(vfsSpiDev->ldo_pin2, 1);
  1084. mdelay(1);
  1085. }
  1086. gpio_direction_output(vfsSpiDev->ldo_pin, 1);
  1087. pr_info("%s ldo on\n", __func__);
  1088. }
  1089. status = gpio_request(vfsSpiDev->sleepPin, "vfsspi_sleep");
  1090. if (status < 0) {
  1091. pr_err("%s gpio_request vfsspi_sleep failed\n",
  1092. __func__);
  1093. goto done;
  1094. }
  1095. status = gpio_request(vfsSpiDev->drdyPin, "vfsspi_drdy");
  1096. if (status < 0) {
  1097. pr_err("%s gpio_request vfsspi_drdy failed\n",
  1098. __func__);
  1099. goto done;
  1100. }
  1101. status = gpio_direction_output(vfsSpiDev->sleepPin, 0);
  1102. if (status < 0) {
  1103. pr_err("%s gpio_direction_output SLEEP failed\n",
  1104. __func__);
  1105. goto done;
  1106. }
  1107. status = gpio_direction_input(vfsSpiDev->drdyPin);
  1108. if (status < 0) {
  1109. pr_err("%s gpio_direction_input DRDY failed\n",
  1110. __func__);
  1111. goto done;
  1112. }
  1113. spin_lock_init(&vfsSpiDev->irq_lock);
  1114. gpio_irq = gpio_to_irq(vfsSpiDev->drdyPin);
  1115. if (gpio_irq < 0) {
  1116. pr_err("%s gpio_to_irq failed\n", __func__);
  1117. status = gpio_irq;
  1118. goto done;
  1119. }
  1120. if (request_irq
  1121. (gpio_irq, vfsspi_irq, DRDY_IRQ_FLAG, "vfsspi_irq",
  1122. vfsSpiDev) < 0) {
  1123. vfsSpiDev->drdy_irq_flag = DRDY_IRQ_DISABLE;
  1124. pr_err("%s drdy request_irq failed\n", __func__);
  1125. status = -EBUSY;
  1126. goto done;
  1127. } else
  1128. vfsSpiDev->drdy_irq_flag = DRDY_IRQ_ENABLE;
  1129. if (vfsSpiDev->ocp_pin) {
  1130. status = gpio_request(vfsSpiDev->ocp_pin, "vfsspi_ocp");
  1131. if (status < 0) {
  1132. pr_err("%s gpio_request vfsspi_ocp failed\n",
  1133. __func__);
  1134. goto done;
  1135. }
  1136. status = gpio_direction_input(vfsSpiDev->ocp_pin);
  1137. if (status < 0) {
  1138. pr_err("%s gpio_direction_input ocp_pin failed\n",
  1139. __func__);
  1140. goto done;
  1141. }
  1142. if (!gpio_get_value(vfsSpiDev->ocp_pin)) {
  1143. pr_info("%s ocp_flag high, turn off ldo\n",
  1144. __func__);
  1145. vfsSpiDev->ocp_state = 1;
  1146. }
  1147. /* wake lock init */
  1148. wake_lock_init(&vfsSpiDev->ocp_wake_lock,
  1149. WAKE_LOCK_SUSPEND, "ocp_wake_lock");
  1150. INIT_WORK(&vfsSpiDev->ocp_work, vfsspi_ocp_work_func);
  1151. vfsSpiDev->ocp_irq = gpio_to_irq(vfsSpiDev->ocp_pin);
  1152. status = request_threaded_irq(
  1153. vfsSpiDev->ocp_irq, NULL,
  1154. vfsspi_ocp_irq_thread,
  1155. IRQF_TRIGGER_FALLING,
  1156. "vfsspi_ocp", vfsSpiDev);
  1157. if (status < 0) {
  1158. pr_err("%s ocp irq request failed error %d\n",
  1159. __func__, status);
  1160. goto done;
  1161. }
  1162. enable_irq_wake(vfsSpiDev->ocp_irq);
  1163. }
  1164. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  1165. #ifdef FEATURE_SPI_WAKELOCK
  1166. wake_lock_init(&vfsSpiDev->fp_spi_lock,
  1167. WAKE_LOCK_SUSPEND, "vfsspi_wake_lock");
  1168. #endif
  1169. vfsspi_disable_irq(vfsSpiDev);
  1170. #endif
  1171. pr_info("%s drdy value =%d\n"
  1172. "%s sleep value =%d\n"
  1173. "%s ldo en value =%d\n",
  1174. __func__, gpio_get_value(vfsSpiDev->drdyPin),
  1175. __func__, gpio_get_value(vfsSpiDev->sleepPin),
  1176. __func__, gpio_get_value(vfsSpiDev->ldo_pin));
  1177. if (vfsSpiDev->ldo_pin2) {
  1178. pr_info("%s ldo en2 value =%d\n",
  1179. __func__, gpio_get_value(vfsSpiDev->ldo_pin2));
  1180. }
  1181. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  1182. vfsSpiDev->freqTable = freqTable;
  1183. vfsSpiDev->freqTableSize = sizeof(freqTable);
  1184. #endif
  1185. } else {
  1186. status = -EFAULT;
  1187. }
  1188. done:
  1189. pr_info("%s vfsspi_platofrminit, status=%d\n", __func__, status);
  1190. return status;
  1191. }
  1192. void vfsspi_platformUninit(struct vfsspi_devData *vfsSpiDev)
  1193. {
  1194. pr_info("%s\n", __func__);
  1195. if (vfsSpiDev != NULL) {
  1196. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  1197. #ifdef FEATURE_SPI_WAKELOCK
  1198. wake_lock_destroy(&vfsSpiDev->fp_spi_lock);
  1199. #endif
  1200. vfsSpiDev->freqTable = NULL;
  1201. vfsSpiDev->freqTableSize = 0;
  1202. #endif
  1203. if (vfsSpiDev->ocp_pin)
  1204. wake_lock_destroy(&vfsSpiDev->ocp_wake_lock);
  1205. free_irq(gpio_irq, vfsSpiDev);
  1206. vfsSpiDev->drdy_irq_flag = DRDY_IRQ_DISABLE;
  1207. if (vfsSpiDev->ldo_pin)
  1208. gpio_free(vfsSpiDev->ldo_pin);
  1209. if (vfsSpiDev->ldo_pin2)
  1210. gpio_free(vfsSpiDev->ldo_pin2);
  1211. gpio_free(vfsSpiDev->sleepPin);
  1212. gpio_free(vfsSpiDev->drdyPin);
  1213. if (vfsSpiDev->ocp_en)
  1214. gpio_free(vfsSpiDev->ocp_en);
  1215. }
  1216. }
  1217. static int vfsspi_parse_dt(struct device *dev,
  1218. struct vfsspi_devData *data)
  1219. {
  1220. struct device_node *np = dev->of_node;
  1221. enum of_gpio_flags flags;
  1222. int errorno = 0;
  1223. int gpio;
  1224. gpio = of_get_named_gpio_flags(np, "vfsspi-sleepPin",
  1225. 0, &flags);
  1226. if (gpio < 0) {
  1227. errorno = gpio;
  1228. goto dt_exit;
  1229. } else {
  1230. data->sleepPin = gpio;
  1231. pr_info("%s: sleepPin=%d\n",
  1232. __func__, data->sleepPin);
  1233. }
  1234. gpio = of_get_named_gpio_flags(np, "vfsspi-drdyPin",
  1235. 0, &flags);
  1236. if (gpio < 0) {
  1237. errorno = gpio;
  1238. goto dt_exit;
  1239. } else {
  1240. data->drdyPin = gpio;
  1241. pr_info("%s: drdyPin=%d\n",
  1242. __func__, data->drdyPin);
  1243. }
  1244. gpio = of_get_named_gpio_flags(np, "vfsspi-ocpflag",
  1245. 0, &flags);
  1246. if (gpio < 0) {
  1247. data->ocp_pin = 0;
  1248. pr_err("%s: fail to get ocp_pin\n", __func__);
  1249. } else {
  1250. data->ocp_pin = gpio;
  1251. pr_info("%s: ocp_pin=%d\n",
  1252. __func__, data->ocp_pin);
  1253. }
  1254. #if defined(CONFIG_MACH_KLTE_JPN) || defined(CONFIG_MACH_CHAGALL_KDI)
  1255. gpio = of_get_named_gpio_flags(np, "vfsspi-ocpen-jpn",
  1256. 0, &flags);
  1257. #else
  1258. gpio = of_get_named_gpio_flags(np, "vfsspi-ocpen",
  1259. 0, &flags);
  1260. #endif
  1261. if (gpio < 0) {
  1262. data->ocp_en = 0;
  1263. pr_err("%s: fail to get ocp_en\n", __func__);
  1264. } else {
  1265. data->ocp_en = gpio;
  1266. pr_info("%s: ocp_en=%d\n",
  1267. __func__, data->ocp_en);
  1268. }
  1269. gpio = of_get_named_gpio_flags(np, "vfsspi-ldoPin",
  1270. 0, &flags);
  1271. if (gpio < 0) {
  1272. data->ldo_pin = 0;
  1273. pr_err("%s: fail to get ldo_pin\n", __func__);
  1274. } else {
  1275. data->ldo_pin = gpio;
  1276. pr_info("%s: ldo_pin=%d\n",
  1277. __func__, data->ldo_pin);
  1278. }
  1279. if (!of_find_property(np, "vfsspi-ldoPin2", NULL)) {
  1280. pr_err("%s: not set ldo2 in dts\n", __func__);
  1281. } else {
  1282. gpio = of_get_named_gpio(np, "vfsspi-ldoPin2", 0);
  1283. if (gpio < 0) {
  1284. data->ldo_pin2 = 0;
  1285. pr_err("%s: fail to get ldo_pin2\n", __func__);
  1286. } else {
  1287. data->ldo_pin2 = gpio;
  1288. pr_info("%s: ldo_pin2=%d\n",
  1289. __func__, data->ldo_pin2);
  1290. }
  1291. }
  1292. if (of_property_read_u32(np, "vfsspi-ldocontrol",
  1293. &data->ldocontrol))
  1294. data->ldocontrol = 0;
  1295. pr_info("%s: ldocontrol=%d\n",
  1296. __func__, data->ldocontrol);
  1297. #ifdef ENABLE_SENSORS_FPRINT_SECURE
  1298. if (of_property_read_u32(np, "vfsspi-mosipin", &data->mosipin))
  1299. data->mosipin = 0;
  1300. if (of_property_read_u32(np, "vfsspi-misopin", &data->misopin))
  1301. data->misopin = 0;
  1302. if (of_property_read_u32(np, "vfsspi-cspin", &data->cspin))
  1303. data->cspin = 0;
  1304. if (of_property_read_u32(np, "vfsspi-clkpin", &data->clkpin))
  1305. data->clkpin = 0;
  1306. if (of_property_read_u32(np, "vfsspi-set_altfunc", &data->set_altfunc))
  1307. data->set_altfunc = 0;
  1308. if (of_property_read_u32(np, "vfsspi-mosi_alt", &data->mosi_alt))
  1309. data->mosi_alt = 0;
  1310. if (of_property_read_u32(np, "vfsspi-miso_alt", &data->miso_alt))
  1311. data->miso_alt = 0;
  1312. if (of_property_read_u32(np, "vfsspi-cs_alt", &data->cs_alt))
  1313. data->cs_alt = 0;
  1314. if (of_property_read_u32(np, "vfsspi-clk_alt", &data->clk_alt))
  1315. data->clk_alt = 0;
  1316. data->tz_mode = true;
  1317. #endif
  1318. if (of_property_read_u32(np, "vfsspi-orient",
  1319. &data->orient))
  1320. data->orient = 0;
  1321. pr_info("%s: orient=%d\n",
  1322. __func__, data->orient);
  1323. dt_exit:
  1324. return errorno;
  1325. }
  1326. #ifdef CONFIG_SENSORS_FINGERPRINT_SYSFS
  1327. static ssize_t vfsspi_type_check_show(struct device *dev,
  1328. struct device_attribute *attr, char *buf)
  1329. {
  1330. struct vfsspi_devData *data = dev_get_drvdata(dev);
  1331. return snprintf(buf, PAGE_SIZE, "%d\n", data->sensortype);
  1332. }
  1333. static ssize_t vfsspi_vendor_show(struct device *dev,
  1334. struct device_attribute *attr, char *buf)
  1335. {
  1336. return sprintf(buf, "%s\n", VENDOR);
  1337. }
  1338. static ssize_t vfsspi_name_show(struct device *dev,
  1339. struct device_attribute *attr, char *buf)
  1340. {
  1341. return sprintf(buf, "%s\n", CHIP_ID);
  1342. }
  1343. static DEVICE_ATTR(type_check, S_IRUGO,
  1344. vfsspi_type_check_show, NULL);
  1345. static DEVICE_ATTR(vendor, S_IRUGO,
  1346. vfsspi_vendor_show, NULL);
  1347. static DEVICE_ATTR(name, S_IRUGO,
  1348. vfsspi_name_show, NULL);
  1349. static struct device_attribute *fp_attrs[] = {
  1350. &dev_attr_type_check,
  1351. &dev_attr_vendor,
  1352. &dev_attr_name,
  1353. NULL,
  1354. };
  1355. #endif
  1356. static void vfsspi_work_func_debug(struct work_struct *work)
  1357. {
  1358. u8 ldo_value = 0;
  1359. if (g_data->ldo_pin2 == 0) {
  1360. ldo_value = gpio_get_value(g_data->ldo_pin);
  1361. } else {
  1362. ldo_value = (gpio_get_value(g_data->ldo_pin2) << 1 )
  1363. | gpio_get_value(g_data->ldo_pin);
  1364. }
  1365. if (g_data->ocp_en)
  1366. pr_info("%s r ocpen: %d, ldo: %d,"
  1367. " sleep: %d, tz: %d, type: %s\n",
  1368. __func__, gpio_get_value(g_data->ocp_en),
  1369. ldo_value, gpio_get_value(g_data->sleepPin),
  1370. g_data->tz_mode,
  1371. sensor_status[g_data->sensortype+1]);
  1372. else
  1373. pr_info("%s r ldo: %d,"
  1374. " sleep: %d, tz: %d, type: %s\n",
  1375. __func__, ldo_value,
  1376. gpio_get_value(g_data->sleepPin),
  1377. g_data->tz_mode,
  1378. sensor_status[g_data->sensortype+1]);
  1379. }
  1380. static void vfsspi_enable_debug_timer(void)
  1381. {
  1382. mod_timer(&g_data->dbg_timer,
  1383. round_jiffies_up(jiffies + FPSENSOR_DEBUG_TIMER_SEC));
  1384. }
  1385. static void vfsspi_disable_debug_timer(void)
  1386. {
  1387. del_timer_sync(&g_data->dbg_timer);
  1388. cancel_work_sync(&g_data->work_debug);
  1389. }
  1390. static void vfsspi_timer_func(unsigned long ptr)
  1391. {
  1392. queue_work(g_data->wq_dbg, &g_data->work_debug);
  1393. mod_timer(&g_data->dbg_timer,
  1394. round_jiffies_up(jiffies + FPSENSOR_DEBUG_TIMER_SEC));
  1395. }
  1396. #define TEST_DEBUG
  1397. int vfsspi_probe(struct spi_device *spi)
  1398. {
  1399. int status = 0;
  1400. struct vfsspi_devData *vfsSpiDev = NULL;
  1401. struct device *dev = NULL;
  1402. #ifdef TEST_DEBUG
  1403. char tx_buf[64] = {5};
  1404. char rx_buf[64] = {0};
  1405. struct spi_transfer t;
  1406. struct spi_message m;
  1407. #endif
  1408. pr_info("%s\n", __func__);
  1409. vfsSpiDev = kzalloc(sizeof(*vfsSpiDev), GFP_KERNEL);
  1410. if (vfsSpiDev == NULL)
  1411. return -ENOMEM;
  1412. if (spi->dev.of_node) {
  1413. status = vfsspi_parse_dt(&spi->dev, vfsSpiDev);
  1414. if (status) {
  1415. pr_err("%s - Failed to parse DT\n", __func__);
  1416. goto parse_dt_failed;
  1417. }
  1418. }
  1419. /* Initialize driver data. */
  1420. vfsSpiDev->curSpiSpeed = SLOW_BAUD_RATE;
  1421. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  1422. vfsSpiDev->userInfoData.buffer = NULL;
  1423. #endif
  1424. vfsSpiDev->spi = spi;
  1425. g_data = vfsSpiDev;
  1426. spin_lock_init(&vfsSpiDev->vfsSpiLock);
  1427. mutex_init(&vfsSpiDev->bufferMutex);
  1428. INIT_LIST_HEAD(&vfsSpiDev->deviceEntry);
  1429. status = vfsspi_platformInit(vfsSpiDev);
  1430. if (status == 0) {
  1431. spi->bits_per_word = BITS_PER_WORD;
  1432. spi->max_speed_hz = SLOW_BAUD_RATE;
  1433. spi->mode = SPI_MODE_0;
  1434. status = spi_setup(spi);
  1435. if (status == 0) {
  1436. mutex_lock(&deviceListMutex);
  1437. /* Create device node */
  1438. vfsSpiDev->devt = MKDEV(vfsspi_majorno, 0);
  1439. dev =
  1440. device_create(vfsSpiDevClass, &spi->dev,
  1441. vfsSpiDev->devt, vfsSpiDev, "vfsspi");
  1442. status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
  1443. if (status == 0)
  1444. list_add(&vfsSpiDev->deviceEntry, &deviceList);
  1445. mutex_unlock(&deviceListMutex);
  1446. if (status == 0) {
  1447. spi_set_drvdata(spi, vfsSpiDev);
  1448. } else {
  1449. pr_err("%s device_create failed %d\n",
  1450. __func__, status);
  1451. goto parse_dt_failed;
  1452. }
  1453. } else {
  1454. gDevSpi = spi;
  1455. pr_err("%s spi_setup() is failed! status= %d\n",
  1456. __func__, status);
  1457. vfsspi_platformUninit(vfsSpiDev);
  1458. goto parse_dt_failed;
  1459. }
  1460. } else {
  1461. vfsspi_platformUninit(vfsSpiDev);
  1462. goto parse_dt_failed;
  1463. }
  1464. #ifdef TEST_DEBUG
  1465. vfsspi_regulator_onoff(vfsSpiDev, true);
  1466. /* check sensor if it is raptor */
  1467. vfsspi_hardReset(vfsSpiDev);
  1468. msleep(20);
  1469. tx_buf[0] = 5;
  1470. tx_buf[1] = 0;
  1471. spi->bits_per_word = 16;
  1472. spi->mode = SPI_MODE_0;
  1473. memset(&t, 0, sizeof(t));
  1474. t.tx_buf = tx_buf;
  1475. t.rx_buf = rx_buf;
  1476. t.len = 64;
  1477. spi_setup(spi);
  1478. spi_message_init(&m);
  1479. spi_message_add_tail(&t, &m);
  1480. pr_info("ValiditySensor: spi_sync returned %d\n",
  1481. spi_sync(spi, &m));
  1482. if (((rx_buf[0] == 0x98) || (rx_buf[0] == 0xBA))
  1483. && ((rx_buf[1] == 0x98) || (rx_buf[1] == 0xBA))) {
  1484. vfsSpiDev->sensortype = SENSOR_RAPTOR;
  1485. pr_info("%s sensor type is RAPTOR\n", __func__);
  1486. goto spi_setup;
  1487. }
  1488. /* check sensor if it is viper */
  1489. gpio_set_value(vfsSpiDev->sleepPin, 1);
  1490. msleep(20);
  1491. tx_buf[0] = 1; /* EP0 Read */
  1492. tx_buf[1] = 0;
  1493. spi->bits_per_word = 8;
  1494. spi->mode = SPI_MODE_0;
  1495. memset(&t, 0, sizeof(t));
  1496. t.tx_buf = tx_buf;
  1497. t.rx_buf = rx_buf;
  1498. t.len = 6;
  1499. spi_setup(spi);
  1500. spi_message_init(&m);
  1501. spi_message_add_tail(&t, &m);
  1502. pr_info("%s ValiditySensor: spi_sync returned %d\n",
  1503. __func__, spi_sync(spi, &m));
  1504. if (((rx_buf[2] == 0x01) || (rx_buf[2] == 0x41))
  1505. && (rx_buf[5] == 0x68)) {
  1506. vfsSpiDev->sensortype = SENSOR_VIPER;
  1507. pr_info("%s sensor type is VIPER\n", __func__);
  1508. } else {
  1509. vfsSpiDev->sensortype = SENSOR_FAILED;
  1510. pr_info("%s sensor type is FAILED\n", __func__);
  1511. }
  1512. spi_setup:
  1513. spi->bits_per_word = BITS_PER_WORD;
  1514. spi->max_speed_hz = SLOW_BAUD_RATE;
  1515. spi->mode = SPI_MODE_0;
  1516. status = spi_setup(spi);
  1517. vfsspi_regulator_onoff(vfsSpiDev, false);
  1518. gpio_set_value(vfsSpiDev->sleepPin, 0);
  1519. #endif
  1520. #ifdef CONFIG_SENSORS_FINGERPRINT_SYSFS
  1521. status = fingerprint_register(vfsSpiDev->fp_device,
  1522. vfsSpiDev, fp_attrs, "fingerprint");
  1523. if (status) {
  1524. pr_err("%s sysfs register failed\n", __func__);
  1525. goto parse_dt_failed;
  1526. }
  1527. #endif
  1528. /* debug polling function */
  1529. setup_timer(&vfsSpiDev->dbg_timer,
  1530. vfsspi_timer_func, (unsigned long)vfsSpiDev);
  1531. vfsSpiDev->wq_dbg =
  1532. create_singlethread_workqueue("vfsspi_debug_wq");
  1533. if (!vfsSpiDev->wq_dbg) {
  1534. status = -ENOMEM;
  1535. pr_err("%s: could not create workqueue\n", __func__);
  1536. goto err_create_workqueue;
  1537. }
  1538. INIT_WORK(&vfsSpiDev->work_debug, vfsspi_work_func_debug);
  1539. vfsspi_enable_debug_timer();
  1540. pr_info("%s success...\n", __func__);
  1541. return 0;
  1542. err_create_workqueue:
  1543. parse_dt_failed:
  1544. kfree(vfsSpiDev);
  1545. return status;
  1546. }
  1547. int vfsspi_remove(struct spi_device *spi)
  1548. {
  1549. int status = 0;
  1550. struct vfsspi_devData *vfsSpiDev = NULL;
  1551. pr_info("%s\n", __func__);
  1552. vfsSpiDev = spi_get_drvdata(spi);
  1553. if (vfsSpiDev != NULL) {
  1554. vfsspi_disable_debug_timer();
  1555. #ifdef CONFIG_SENSORS_FINGERPRINT_SYSFS
  1556. fingerprint_unregister(vfsSpiDev->fp_device, fp_attrs);
  1557. #endif
  1558. gDevSpi = spi;
  1559. spin_lock_irq(&vfsSpiDev->vfsSpiLock);
  1560. vfsSpiDev->spi = NULL;
  1561. spi_set_drvdata(spi, NULL);
  1562. spin_unlock_irq(&vfsSpiDev->vfsSpiLock);
  1563. mutex_lock(&deviceListMutex);
  1564. vfsspi_platformUninit(vfsSpiDev);
  1565. #ifndef ENABLE_SENSORS_FPRINT_SECURE
  1566. if (vfsSpiDev->userInfoData.buffer != NULL)
  1567. kfree(vfsSpiDev->userInfoData.buffer);
  1568. #endif
  1569. /* Remove device entry. */
  1570. list_del(&vfsSpiDev->deviceEntry);
  1571. device_destroy(vfsSpiDevClass, vfsSpiDev->devt);
  1572. kfree(vfsSpiDev);
  1573. mutex_unlock(&deviceListMutex);
  1574. }
  1575. return status;
  1576. }
  1577. static void vfsspi_shutdown(struct spi_device *spi)
  1578. {
  1579. if (g_data != NULL)
  1580. vfsspi_disable_debug_timer();
  1581. pr_info("%s\n", __func__);
  1582. }
  1583. static int vfsspi_pm_suspend(struct device *dev)
  1584. {
  1585. if (g_data != NULL)
  1586. vfsspi_disable_debug_timer();
  1587. pr_info("%s\n", __func__);
  1588. return 0;
  1589. }
  1590. static int vfsspi_pm_resume(struct device *dev)
  1591. {
  1592. if (g_data != NULL)
  1593. vfsspi_enable_debug_timer();
  1594. pr_info("%s\n", __func__);
  1595. return 0;
  1596. }
  1597. static const struct dev_pm_ops vfsspi_pm_ops = {
  1598. .suspend = vfsspi_pm_suspend,
  1599. .resume = vfsspi_pm_resume
  1600. };
  1601. #ifdef CONFIG_OF
  1602. static struct of_device_id vfsspi_match_table[] = {
  1603. { .compatible = "vfsspi,vfs61xx",},
  1604. {},
  1605. };
  1606. #endif
  1607. /* SPI driver info */
  1608. struct spi_driver vfsspi_spi = {
  1609. .driver = {
  1610. .name = "validity_fingerprint",
  1611. .owner = THIS_MODULE,
  1612. .pm = &vfsspi_pm_ops,
  1613. #ifdef CONFIG_OF
  1614. .of_match_table = vfsspi_match_table
  1615. #endif
  1616. },
  1617. .probe = vfsspi_probe,
  1618. .shutdown = vfsspi_shutdown,
  1619. .remove = __devexit_p(vfsspi_remove),
  1620. };
  1621. /* file operations associated with device */
  1622. const struct file_operations vfsspi_fops = {
  1623. .owner = THIS_MODULE,
  1624. .write = vfsspi_write,
  1625. .read = vfsspi_read,
  1626. .unlocked_ioctl = vfsspi_ioctl,
  1627. .open = vfsspi_open,
  1628. .release = vfsspi_release,
  1629. };
  1630. static int __init vfsspi_init(void)
  1631. {
  1632. int status = 0;
  1633. pr_info("%s\n", __func__);
  1634. /* register major number for character device */
  1635. vfsspi_majorno =
  1636. register_chrdev(0, "validity_fingerprint", &vfsspi_fops);
  1637. if (vfsspi_majorno < 0) {
  1638. pr_err("%s register_chrdev failed\n", __func__);
  1639. return vfsspi_majorno;
  1640. }
  1641. vfsSpiDevClass = class_create(THIS_MODULE, "validity_fingerprint");
  1642. if (IS_ERR(vfsSpiDevClass)) {
  1643. pr_err
  1644. ("%s vfsspi_init: class_create() is failed\n", __func__);
  1645. unregister_chrdev(vfsspi_majorno, vfsspi_spi.driver.name);
  1646. return PTR_ERR(vfsSpiDevClass);
  1647. }
  1648. status = spi_register_driver(&vfsspi_spi);
  1649. if (status < 0) {
  1650. pr_err("%s : register spi drv is failed\n", __func__);
  1651. class_destroy(vfsSpiDevClass);
  1652. unregister_chrdev(vfsspi_majorno, vfsspi_spi.driver.name);
  1653. return status;
  1654. }
  1655. pr_info("%s init is successful\n", __func__);
  1656. return status;
  1657. }
  1658. static void __exit vfsspi_exit(void)
  1659. {
  1660. pr_info("%s\n", __func__);
  1661. spi_unregister_driver(&vfsspi_spi);
  1662. class_destroy(vfsSpiDevClass);
  1663. if (vfsspi_majorno >= 0)
  1664. unregister_chrdev(vfsspi_majorno, vfsspi_spi.driver.name);
  1665. }
  1666. module_init(vfsspi_init);
  1667. module_exit(vfsspi_exit);
  1668. MODULE_LICENSE("GPL");