hscdtd008a_i2c.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /* hscdtd008a_i2c.c
  2. *
  3. * GeoMagneticField device driver for I2C (HSCDTD008A)
  4. *
  5. * Copyright (C) 2012 ALPS ELECTRIC CO., LTD. All Rights Reserved.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/i2c.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/regulator/consumer.h>
  23. #undef CONFIG_HAS_EARLYSUSPEND
  24. #ifdef CONFIG_HAS_EARLYSUSPEND
  25. #include <linux/earlysuspend.h>
  26. #endif
  27. #include <linux/err.h>
  28. #include <linux/of_gpio.h>
  29. #include "sensors_core.h"
  30. #define I2C_RETRY_DELAY 5
  31. #define I2C_RETRIES 5
  32. #define I2C_HSCD_ADDR (0x0c) /* 000 1100 */
  33. #define HSCD_DRIVER_NAME "HSCDTD008"
  34. #define CHIP_DEV_NAME "HSCDTD008A"
  35. #define CHIP_DEV_VENDOR "ALPS"
  36. #undef ALPS_DEBUG
  37. #define alps_dbgmsg(str, args...) pr_debug("%s: " str, __func__, ##args)
  38. #define alps_errmsg(str, args...) pr_err("%s: " str, __func__, ##args)
  39. #define alps_info(str, args...) pr_info("%s: " str, __func__, ##args)
  40. #define HSCD_STB 0x0C
  41. #define HSCD_XOUT 0x10
  42. #define HSCD_YOUT 0x12
  43. #define HSCD_ZOUT 0x14
  44. #define HSCD_XOUT_H 0x11
  45. #define HSCD_XOUT_L 0x10
  46. #define HSCD_YOUT_H 0x13
  47. #define HSCD_YOUT_L 0x12
  48. #define HSCD_ZOUT_H 0x15
  49. #define HSCD_ZOUT_L 0x14
  50. #define HSCD_STATUS 0x18
  51. #define HSCD_CTRL1 0x1b
  52. #define HSCD_CTRL2 0x1c
  53. #define HSCD_CTRL3 0x1d
  54. #define HSCD_CTRL4 0x1e
  55. #define HSCD_TCS_TIME 10000 /* Measure temp. of every 10 sec */
  56. /* hscdtd008a chip id */
  57. #define DEVICE_ID 0x49
  58. /* hscd magnetic sensor chip identification register */
  59. #define WHO_AM_I 0x0F
  60. #define RETRY_COUNT 10
  61. static struct i2c_driver hscd_driver;
  62. static struct i2c_client *this_client;
  63. #ifdef CONFIG_HAS_EARLYSUSPEND
  64. static struct early_suspend hscd_early_suspend_handler;
  65. #endif
  66. struct hscd_power_data {
  67. struct regulator *regulator_vdd;
  68. struct regulator *regulator_vio;
  69. };
  70. struct hscd_platform_data{
  71. unsigned int irq_gpio;
  72. u32 irq_gpio_flags;
  73. const u8 *reg_vdd;
  74. const u8 *reg_vio;
  75. };
  76. static struct hscd_power_data hscd_power;
  77. static atomic_t flgEna;
  78. static atomic_t delay;
  79. static atomic_t flgSuspend;
  80. static int tcs_thr;
  81. static int tcs_cnt;
  82. static int hscd_i2c_readm(char *rxData, int length)
  83. {
  84. int err;
  85. int tries = 0;
  86. struct i2c_msg msgs[] = {
  87. {
  88. .addr = this_client->addr,
  89. .flags = 0,
  90. .len = 1,
  91. .buf = rxData,
  92. },
  93. {
  94. .addr = this_client->addr,
  95. .flags = I2C_M_RD,
  96. .len = length,
  97. .buf = rxData,
  98. },
  99. };
  100. do {
  101. err = i2c_transfer(this_client->adapter, msgs, 2);
  102. } while ((err != 2) && (++tries < I2C_RETRIES));
  103. if (err != 2) {
  104. dev_err(&this_client->adapter->dev, "read transfer error\n");
  105. err = -EIO;
  106. } else {
  107. err = 0;
  108. }
  109. return err;
  110. }
  111. static int hscd_i2c_writem(char *txData, int length)
  112. {
  113. int err;
  114. int tries = 0;
  115. #ifdef ALPS_DEBUG
  116. int i;
  117. #endif
  118. struct i2c_msg msg[] = {
  119. {
  120. .addr = this_client->addr,
  121. .flags = 0,
  122. .len = length,
  123. .buf = txData,
  124. },
  125. };
  126. #ifdef ALPS_DEBUG
  127. pr_debug("%s : ", __func__);
  128. for (i = 0; i < length; i++)
  129. pr_debug("0X%02X, ", txData[i]);
  130. pr_debug("\n");
  131. #endif
  132. do {
  133. err = i2c_transfer(this_client->adapter, msg, 1);
  134. } while ((err != 1) && (++tries < I2C_RETRIES));
  135. if (err != 1) {
  136. dev_err(&this_client->adapter->dev, "write transfer error\n");
  137. err = -EIO;
  138. } else {
  139. err = 0;
  140. }
  141. return err;
  142. }
  143. static int hscd_power_on(int onoff)
  144. {
  145. int err = 0;
  146. alps_info("is called\n");
  147. if (onoff == 1)
  148. {
  149. if (hscd_power.regulator_vdd) {
  150. err = regulator_enable(hscd_power.regulator_vdd);
  151. if (err) {
  152. alps_errmsg("Couldn't enable VDD %d\n", err);
  153. return err;
  154. }
  155. }
  156. if (hscd_power.regulator_vio) {
  157. err = regulator_enable(hscd_power.regulator_vio);
  158. if (err) {
  159. alps_errmsg("Couldn't enable VIO %d\n", err);
  160. return err;
  161. }
  162. }
  163. }
  164. else if (onoff == 0) {
  165. if (hscd_power.regulator_vdd) {
  166. regulator_disable(hscd_power.regulator_vdd);
  167. /*regulator_put(hscd_power.regulator_vdd);*/
  168. }
  169. if (hscd_power.regulator_vio) {
  170. regulator_disable(hscd_power.regulator_vio);
  171. /*regulator_put(hscd_power.regulator_vio);*/
  172. }
  173. }
  174. msleep(60);
  175. return err;
  176. }
  177. int hscd_self_test_A(void)
  178. {
  179. u8 sx[2], cr1[1];
  180. if ( (this_client == NULL) || (atomic_read(&flgSuspend) == 1) )
  181. {
  182. alps_errmsg("ALPS_ERR hscd_self_test_A validation fail\n");
  183. return -1;
  184. }
  185. alps_info("is called\n");
  186. /* Control register1 backup */
  187. cr1[0] = HSCD_CTRL1;
  188. if (hscd_i2c_readm(cr1, 1))
  189. return 1;
  190. #ifdef ALPS_DEBUG
  191. else
  192. alps_dbgmsg("Control resister1 value, %02X\n", cr1[0]);
  193. #endif
  194. mdelay(1);
  195. /* Move active Mode (force state) */
  196. sx[0] = HSCD_CTRL1;
  197. sx[1] = 0x8A;
  198. if (hscd_i2c_writem(sx, 2))
  199. return 1;
  200. /* Get inital value of self-test-A register */
  201. sx[0] = HSCD_STB;
  202. hscd_i2c_readm(sx, 1);
  203. mdelay(1);
  204. sx[0] = HSCD_STB;
  205. if (hscd_i2c_readm(sx, 1))
  206. return 1;
  207. #ifdef ALPS_DEBUG
  208. else
  209. alps_dbgmsg("Self test A register value, %02X\n", sx[0]);
  210. #endif
  211. if (sx[0] != 0x55) {
  212. alps_errmsg("Err! self-test-A, initial value is %02X\n", sx[0]);
  213. return 2;
  214. }
  215. /* do self-test*/
  216. sx[0] = HSCD_CTRL3;
  217. sx[1] = 0x10;
  218. if (hscd_i2c_writem(sx, 2))
  219. return 1;
  220. mdelay(3);
  221. /* Get 1st value of self-test-A register */
  222. sx[0] = HSCD_STB;
  223. if (hscd_i2c_readm(sx, 1))
  224. return 1;
  225. #ifdef ALPS_DEBUG
  226. else
  227. alps_dbgmsg("Self test register value, %02X\n", sx[0]);
  228. #endif
  229. if (sx[0] != 0xAA) {
  230. alps_errmsg("Err! self-test, 1st value is %02X\n", sx[0]);
  231. return 3;
  232. }
  233. mdelay(3);
  234. /* Get 2nd value of self-test register */
  235. sx[0] = HSCD_STB;
  236. if (hscd_i2c_readm(sx, 1))
  237. return 1;
  238. #ifdef ALPS_DEBUG
  239. else
  240. alps_dbgmsg("Self test register value, %02X\n", sx[0]);
  241. #endif
  242. if (sx[0] != 0x55) {
  243. alps_errmsg("Err! self-test, 2nd value is %02X\n", sx[0]);
  244. return 4;
  245. }
  246. /* Resume */
  247. sx[0] = HSCD_CTRL1;
  248. sx[1] = cr1[0];
  249. if (hscd_i2c_writem(sx, 2))
  250. return 1;
  251. return 0;
  252. }
  253. EXPORT_SYMBOL(hscd_self_test_A);
  254. int hscd_self_test_B(void)
  255. {
  256. if ( (this_client == NULL) || atomic_read(&flgSuspend) == 1)
  257. {
  258. alps_errmsg("ALPS_ERR hscd_self_test_B validation fail\n");
  259. return -1;
  260. }
  261. alps_info("is called\n");
  262. return 0;
  263. }
  264. EXPORT_SYMBOL(hscd_self_test_B);
  265. static int hscd_soft_reset(void)
  266. {
  267. int rc;
  268. u8 buf[2];
  269. #ifdef ALPS_DEBUG
  270. alps_dbgmsg("Software Reset\n");
  271. #endif
  272. buf[0] = HSCD_CTRL3;
  273. buf[1] = 0x80;
  274. rc = hscd_i2c_writem(buf, 2);
  275. msleep(20);
  276. return rc;
  277. }
  278. static int hscd_tcs_setup(void)
  279. {
  280. int rc;
  281. u8 buf[2];
  282. buf[0] = HSCD_CTRL3;
  283. buf[1] = 0x02;
  284. rc = hscd_i2c_writem(buf, 2);
  285. msleep(20);
  286. tcs_thr = HSCD_TCS_TIME / atomic_read(&delay);
  287. tcs_cnt = 0;
  288. return rc;
  289. }
  290. static int hscd_force_setup(void)
  291. {
  292. u8 buf[2];
  293. buf[0] = HSCD_CTRL3;
  294. buf[1] = 0x40;
  295. return hscd_i2c_writem(buf, 2);
  296. }
  297. int hscd_get_magnetic_field_data(int *xyz)
  298. {
  299. int err = -1;
  300. int i;
  301. u8 sx[6] = {0, };
  302. if ( (this_client == NULL) || ( atomic_read(&flgSuspend) == 1 ) )
  303. {
  304. alps_errmsg("ALPS_ERR hscd_get_magnetic_field_data validation fail\n");
  305. return err;
  306. }
  307. sx[0] = HSCD_XOUT;
  308. err = hscd_i2c_readm(sx, 6);
  309. if (err < 0) {
  310. alps_errmsg("Fail to read data from i2c\n");
  311. return err;
  312. }
  313. for (i = 0; i < 3; i++)
  314. xyz[i] = (int) ((short)((sx[2*i + 1] << 8) | (sx[2*i])));
  315. #ifdef ALPS_DEBUG
  316. /*** DEBUG OUTPUT - REMOVE ***/
  317. alps_dbgmsg("x = %d, y = %d, z = %d\n", xyz[0], xyz[1], xyz[2]);
  318. /*** <end> DEBUG OUTPUT - REMOVE ***/
  319. #endif
  320. if (++tcs_cnt > tcs_thr)
  321. hscd_tcs_setup();
  322. hscd_force_setup();
  323. return err;
  324. }
  325. EXPORT_SYMBOL(hscd_get_magnetic_field_data);
  326. void hscd_activate(int flgatm, int flg, int dtime)
  327. {
  328. u8 buf[2];
  329. int Ena = atomic_read(&flgEna);
  330. if (this_client == NULL)
  331. return;
  332. else if ((atomic_read(&delay) == dtime)
  333. && (atomic_read(&flgEna) == flg)
  334. && (flgatm == 1))
  335. return;
  336. alps_info("is called\n");
  337. if (flg != 0) {
  338. buf[0] = HSCD_CTRL4; /* 15 bit signed value */
  339. buf[1] = 0x90;
  340. hscd_i2c_writem(buf, 2);
  341. mdelay(1);
  342. tcs_cnt = tcs_cnt * atomic_read(&delay) / dtime;
  343. tcs_thr = HSCD_TCS_TIME / dtime;
  344. }
  345. if ((!flg) && (Ena)) {
  346. hscd_soft_reset();
  347. } else if (!flg) {
  348. buf[0] = HSCD_CTRL1;
  349. buf[1] = 0x0A;
  350. hscd_i2c_writem(buf, 2);
  351. } else if ((flg) && (!Ena)) {
  352. buf[0] = HSCD_CTRL1;
  353. buf[1] = 0x8A;
  354. hscd_i2c_writem(buf, 2);
  355. mdelay(1);
  356. hscd_tcs_setup();
  357. hscd_force_setup();
  358. }
  359. if (flgatm) {
  360. atomic_set(&flgEna, flg);
  361. atomic_set(&delay, dtime);
  362. }
  363. }
  364. EXPORT_SYMBOL(hscd_activate);
  365. /*
  366. static void hscd_register_init(void)
  367. {
  368. u8 buf[2];
  369. alps_info("is called\n");
  370. buf[0] = HSCD_CTRL3;
  371. buf[1] = 0x80;
  372. hscd_i2c_writem(buf, 2);
  373. mdelay(5);
  374. atomic_set(&delay, 100);
  375. hscd_activate(0, 1, atomic_read(&delay));
  376. hscd_get_magnetic_field_data(v);
  377. alps_dbgmsg("x = %d, y = %d, z = %d\n", v[0], v[1], v[2]);
  378. hscd_activate(0, 0, atomic_read(&delay));
  379. }
  380. */
  381. static ssize_t selftest_show(struct device *dev,
  382. struct device_attribute *attr, char *buf)
  383. {
  384. int result1, result2;
  385. if (!atomic_read(&flgEna))
  386. hscd_power_on(1);
  387. result1 = hscd_self_test_A();
  388. result2 = hscd_self_test_B();
  389. /*if (!atomic_read(&flgEna))
  390. hscd_power_off();*/
  391. if (result1 == 0)
  392. result1 = 1;
  393. else
  394. result1 = 0;
  395. if (result2 == 0)
  396. result2 = 1;
  397. else
  398. result2 = 0;
  399. alps_info("result, A = %d, B = %d\n", result1, result2);
  400. return snprintf(buf, PAGE_SIZE, "%d, %d\n", result1, result2);
  401. }
  402. static ssize_t status_show(struct device *dev,
  403. struct device_attribute *attr, char *buf)
  404. {
  405. int result;
  406. if (!atomic_read(&flgEna))
  407. hscd_power_on(1);
  408. result = hscd_self_test_B();
  409. /*if (!atomic_read(&flgEna))
  410. hscd_power_off();*/
  411. if (result == 0)
  412. result = 1;
  413. else
  414. result = 0;
  415. return snprintf(buf, PAGE_SIZE, "%d,%d\n", result, 0);
  416. }
  417. static ssize_t adc_show(struct device *dev,
  418. struct device_attribute *attr, char *buf)
  419. {
  420. int data[3];
  421. if (!atomic_read(&flgEna))
  422. hscd_activate(0, 1, 100);
  423. msleep(20);
  424. hscd_get_magnetic_field_data(data);
  425. alps_dbgmsg("x = %d, y = %d, z = %d\n", data[0], data[1], data[2]);
  426. if (!atomic_read(&flgEna))
  427. hscd_activate(0, 0, 100);
  428. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
  429. data[0], data[1], data[2]);
  430. }
  431. static ssize_t name_show(struct device *dev,
  432. struct device_attribute *attr, char *buf)
  433. {
  434. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_NAME);
  435. }
  436. static ssize_t vendor_show(struct device *dev,
  437. struct device_attribute *attr, char *buf)
  438. {
  439. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_VENDOR);
  440. }
  441. static ssize_t mag_raw_data_read(struct device *dev,
  442. struct device_attribute *attr, char *buf)
  443. {
  444. int xyz[3] = {0, };
  445. alps_dbgmsg("is called\n");
  446. if (!atomic_read(&flgEna)) {
  447. hscd_activate(0, 1, 100);
  448. msleep(20);
  449. }
  450. hscd_get_magnetic_field_data(xyz);
  451. if (!atomic_read(&flgEna))
  452. hscd_activate(0, 0, 100);
  453. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
  454. xyz[0], xyz[1], xyz[2]);
  455. }
  456. static DEVICE_ATTR(selftest, S_IRUGO | S_IWUSR | S_IWGRP,
  457. selftest_show, NULL);
  458. static DEVICE_ATTR(status, S_IRUGO | S_IWUSR | S_IWGRP,
  459. status_show, NULL);
  460. static DEVICE_ATTR(adc, S_IRUGO | S_IWUSR | S_IWGRP,
  461. adc_show, NULL);
  462. static DEVICE_ATTR(raw_data, S_IRUGO | S_IWUSR | S_IWGRP,
  463. mag_raw_data_read, NULL);
  464. static DEVICE_ATTR(name, S_IRUGO | S_IWUSR | S_IWGRP, name_show, NULL);
  465. static DEVICE_ATTR(vendor, S_IRUGO | S_IWUSR | S_IWGRP, vendor_show, NULL);
  466. static struct device_attribute *magnetic_attrs[] = {
  467. &dev_attr_selftest,
  468. &dev_attr_status,
  469. &dev_attr_adc,
  470. &dev_attr_name,
  471. &dev_attr_vendor,
  472. &dev_attr_raw_data,
  473. NULL,
  474. };
  475. #ifdef CONFIG_OF
  476. /* device tree parsing */
  477. static int hscd_parse_dt(struct device *dev,struct hscd_platform_data *pdata)
  478. {
  479. struct device_node *np = dev->of_node;
  480. pdata->irq_gpio = of_get_named_gpio_flags(np, "HSCDTD008-i2c,irq_gpio",
  481. 0, &pdata->irq_gpio_flags);
  482. pdata->reg_vdd = of_get_property(np,"HSCDTD008-i2c,reg_vdd", NULL);
  483. pdata->reg_vio = of_get_property(np,"HSCDTD008-i2c,reg_vio", NULL);
  484. return 0;
  485. }
  486. #else
  487. static int hscd_parse_dt(struct device *dev,
  488. struct hscd_platform_data *pdata)
  489. {
  490. return -ENODEV;
  491. }
  492. #endif
  493. static int hscd_probe(struct i2c_client *client,
  494. const struct i2c_device_id *id)
  495. {
  496. int ret = 0;
  497. struct device *magnetic_device = NULL;
  498. #ifdef CONFIG_OF
  499. int err;
  500. struct hscd_platform_data *platform_data;
  501. #endif
  502. alps_info("is called\n");
  503. this_client = client;
  504. pr_info("\n hscd_probe start");
  505. platform_data = devm_kzalloc (&client->dev,
  506. sizeof(struct hscd_platform_data), GFP_KERNEL);
  507. if(!platform_data) {
  508. dev_err(&client->dev, "Failed to allocate memory\n");
  509. return -ENOMEM;
  510. }
  511. err = hscd_parse_dt(&client->dev, platform_data);
  512. if(err)
  513. return err;
  514. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  515. dev_err(&client->adapter->dev, "client not i2c capable (hscd_probe)\n");
  516. ret = -ENOMEM;
  517. goto exit;
  518. }
  519. hscd_power.regulator_vdd = NULL;
  520. hscd_power.regulator_vio = NULL;
  521. hscd_power.regulator_vdd = regulator_get(&client->dev,platform_data->reg_vdd);
  522. if (IS_ERR(hscd_power.regulator_vdd)) {
  523. ret = PTR_ERR(hscd_power.regulator_vdd);
  524. hscd_power.regulator_vdd = NULL;
  525. alps_errmsg("Failed to get hscd_i2c_vdd %d\n", ret);
  526. goto err_setup_regulator;
  527. }
  528. hscd_power.regulator_vio = regulator_get(&client->dev,platform_data->reg_vio);
  529. if (IS_ERR(hscd_power.regulator_vio)) {
  530. ret = PTR_ERR(hscd_power.regulator_vio);
  531. hscd_power.regulator_vio = NULL;
  532. alps_errmsg("Failed to get hscd_i2c_vio %d\n", ret);
  533. goto err_setup_regulator;
  534. }
  535. /* turn on the power */
  536. hscd_power_on(1);
  537. hscd_soft_reset();
  538. /* read chip id */
  539. ret = i2c_smbus_read_byte_data(this_client, WHO_AM_I);
  540. alps_info("Device ID = 0x%x, Reading ID = 0x%x\n", DEVICE_ID, ret);
  541. if (ret == DEVICE_ID) /* Normal Operation */
  542. ret = 0;
  543. else {
  544. if (ret < 0)
  545. alps_errmsg("i2c for reading chip id failed\n");
  546. else {
  547. alps_errmsg("Device identification failed\n");
  548. ret = -ENODEV;
  549. }
  550. goto err_setup_regulator;
  551. }
  552. err=sensors_register(magnetic_device, NULL, magnetic_attrs, "magnetic_sensor");
  553. //sensors_register(magnetic_device, NULL, magnetic_attrs,
  554. // "magnetic_sensor");
  555. if (err < 0)
  556. return err;
  557. atomic_set(&flgEna, 0);
  558. atomic_set(&delay, 100);
  559. atomic_set(&flgSuspend, 0);
  560. tcs_cnt = 0;
  561. tcs_thr = HSCD_TCS_TIME / atomic_read(&delay);
  562. alps_info("is Successful\n");
  563. pr_info("\n hscd probe success");
  564. return 0;
  565. err_setup_regulator:
  566. if (hscd_power.regulator_vdd) {
  567. regulator_disable(hscd_power.regulator_vdd);
  568. regulator_put(hscd_power.regulator_vdd);
  569. }
  570. if (hscd_power.regulator_vio) {
  571. regulator_disable(hscd_power.regulator_vio);
  572. regulator_put(hscd_power.regulator_vio);
  573. }
  574. exit:
  575. this_client = NULL;
  576. alps_errmsg("Failed (errno = %d)\n", ret);
  577. return ret;
  578. }
  579. static int __devexit hscd_remove(struct i2c_client *client)
  580. {
  581. alps_info("is called\n");
  582. hscd_activate(0, 0, atomic_read(&delay));
  583. #ifdef CONFIG_HAS_EARLYSUSPEND
  584. unregister_early_suspend(&hscd_early_suspend_handler);
  585. #endif
  586. if (hscd_power.regulator_vdd) {
  587. regulator_disable(hscd_power.regulator_vdd);
  588. regulator_put(hscd_power.regulator_vdd);
  589. }
  590. if (hscd_power.regulator_vio) {
  591. regulator_disable(hscd_power.regulator_vio);
  592. regulator_put(hscd_power.regulator_vio);
  593. }
  594. this_client = NULL;
  595. return 0;
  596. }
  597. static int hscd_suspend(struct i2c_client *client, pm_message_t mesg)
  598. {
  599. alps_info("is called\n");
  600. atomic_set(&flgSuspend, 1);
  601. if (atomic_read(&flgEna))
  602. hscd_activate(0, 0, atomic_read(&delay));
  603. #if !(defined(CONFIG_MACH_CRATERQ_CHN_OPEN)||defined(CONFIG_MACH_MS01_CHN_CTC)||defined(CONFIG_MACH_BAFFIN2_CHN_CMCC) || defined(CONFIG_SEC_GNOTE_PROJECT) )
  604. hscd_power_on(0);
  605. #endif
  606. return 0;
  607. }
  608. static int hscd_resume(struct i2c_client *client)
  609. {
  610. alps_info("is called\n");
  611. atomic_set(&flgSuspend, 0);
  612. if (atomic_read(&flgEna)) {
  613. atomic_set(&flgEna, 0);
  614. hscd_activate(1, 1 , atomic_read(&delay));
  615. }
  616. #if !(defined(CONFIG_MACH_CRATERQ_CHN_OPEN)||defined(CONFIG_MACH_MS01_CHN_CTC)||defined(CONFIG_MACH_BAFFIN2_CHN_CMCC) || defined(CONFIG_SEC_GNOTE_PROJECT) )
  617. hscd_power_on(1);
  618. #endif
  619. return 0;
  620. }
  621. #ifdef CONFIG_HAS_EARLYSUSPEND
  622. static void hscd_early_suspend(struct early_suspend *handler)
  623. {
  624. alps_info("is called\n");
  625. hscd_suspend(this_client, PMSG_SUSPEND);
  626. }
  627. static void hscd_early_resume(struct early_suspend *handler)
  628. {
  629. alps_info("is called\n");
  630. hscd_resume(this_client);
  631. }
  632. #endif
  633. static const struct i2c_device_id ALPS_id[] = {
  634. { HSCD_DRIVER_NAME, 0 },
  635. { }
  636. };
  637. #ifdef CONFIG_OF
  638. static struct of_device_id magnetic_match_table[] = {
  639. {.compatible = "magnetic,HSCDTD008",},
  640. {},
  641. };
  642. #else
  643. #define magnetic_match_table NULL
  644. #endif
  645. static struct i2c_driver hscd_driver = {
  646. .probe = hscd_probe,
  647. .remove = hscd_remove,
  648. .id_table = ALPS_id,
  649. .driver = {
  650. .name = HSCD_DRIVER_NAME,
  651. .of_match_table = magnetic_match_table,
  652. },
  653. .suspend = hscd_suspend,
  654. .resume = hscd_resume,
  655. };
  656. #ifdef CONFIG_HAS_EARLYSUSPEND
  657. static struct early_suspend hscd_early_suspend_handler = {
  658. .suspend = hscd_early_suspend,
  659. .resume = hscd_early_resume,
  660. };
  661. #endif
  662. static int __init hscd_init(void)
  663. {
  664. alps_info("is called\n");
  665. return i2c_add_driver(&hscd_driver);
  666. }
  667. static void __exit hscd_exit(void)
  668. {
  669. alps_info("is called\n");
  670. i2c_del_driver(&hscd_driver);
  671. }
  672. module_init(hscd_init);
  673. module_exit(hscd_exit);
  674. MODULE_DESCRIPTION("Alps HSCDTD008A Compass Device");
  675. MODULE_AUTHOR("ALPS ELECTRIC CO., LTD.");
  676. MODULE_LICENSE("GPL v2");