PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/sensor/ak8963.c

https://github.com/Team-Hydra/android_kernel_samsung_smdk4210
C | 894 lines | 732 code | 124 blank | 38 comment | 96 complexity | 3c10098dd9d922a5cd5be17990a16cb8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright (C) 2010, Samsung Electronics Co. Ltd. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (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. */
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/slab.h>
  19. #include <linux/gpio.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/delay.h>
  23. #include <linux/fs.h>
  24. #include <linux/sensor/ak8963.h>
  25. #include <linux/completion.h>
  26. #include "ak8963-reg.h"
  27. #include <linux/sensor/sensors_core.h>
  28. #if defined(CONFIG_SLP) || defined(CONFIG_MACH_GC1)\
  29. || defined(CONFIG_MACH_M3_USA_TMO)
  30. #define FACTORY_TEST
  31. #else
  32. #undef FACTORY_TEST
  33. #endif
  34. #undef MAGNETIC_LOGGING
  35. #define VENDOR "AKM"
  36. #define CHIP_ID "AK8963C"
  37. struct akm8963_data {
  38. struct i2c_client *this_client;
  39. struct akm8963_platform_data *pdata;
  40. struct mutex lock;
  41. struct miscdevice akmd_device;
  42. struct completion data_ready;
  43. struct device *dev;
  44. wait_queue_head_t state_wq;
  45. u8 asa[3];
  46. int irq;
  47. };
  48. static s32 akm8963_ecs_set_mode_power_down(struct akm8963_data *akm)
  49. {
  50. s32 ret;
  51. ret = i2c_smbus_write_byte_data(akm->this_client,
  52. AK8963_REG_CNTL1, AK8963_CNTL1_POWER_DOWN);
  53. return ret;
  54. }
  55. static int akm8963_ecs_set_mode(struct akm8963_data *akm, char mode)
  56. {
  57. s32 ret;
  58. switch (mode) {
  59. case AK8963_CNTL1_SNG_MEASURE:
  60. ret = i2c_smbus_write_byte_data(akm->this_client,
  61. AK8963_REG_CNTL1, AK8963_CNTL1_SNG_MEASURE);
  62. break;
  63. case AK8963_CNTL1_FUSE_ACCESS:
  64. ret = i2c_smbus_write_byte_data(akm->this_client,
  65. AK8963_REG_CNTL1, AK8963_CNTL1_FUSE_ACCESS);
  66. break;
  67. case AK8963_CNTL1_POWER_DOWN:
  68. ret = akm8963_ecs_set_mode_power_down(akm);
  69. break;
  70. case AK8963_CNTL1_SELF_TEST:
  71. ret = i2c_smbus_write_byte_data(akm->this_client,
  72. AK8963_REG_CNTL1, AK8963_CNTL1_SELF_TEST);
  73. break;
  74. default:
  75. return -EINVAL;
  76. }
  77. if (ret < 0)
  78. return ret;
  79. /* Wait at least 300us after changing mode. */
  80. udelay(300);
  81. return 0;
  82. }
  83. static int akm8963_Reset(struct akm8963_data *akm)
  84. {
  85. int err = 0;
  86. gpio_set_value(GPIO_MSENSE_RST_N, 0);
  87. udelay(5);
  88. gpio_set_value(GPIO_MSENSE_RST_N, 1);
  89. /* Device will be accessible 100 us after */
  90. udelay(100);
  91. return err;
  92. }
  93. static int akmd_copy_in(unsigned int cmd, void __user *argp,
  94. void *buf, size_t buf_size)
  95. {
  96. if (!(cmd & IOC_IN))
  97. return 0;
  98. if (_IOC_SIZE(cmd) > buf_size)
  99. return -EINVAL;
  100. if (copy_from_user(buf, argp, _IOC_SIZE(cmd)))
  101. return -EFAULT;
  102. return 0;
  103. }
  104. static int akmd_copy_out(unsigned int cmd, void __user *argp,
  105. void *buf, size_t buf_size)
  106. {
  107. if (!(cmd & IOC_OUT))
  108. return 0;
  109. if (_IOC_SIZE(cmd) > buf_size)
  110. return -EINVAL;
  111. if (copy_to_user(argp, buf, _IOC_SIZE(cmd)))
  112. return -EFAULT;
  113. return 0;
  114. }
  115. static void akm8963_disable_irq(struct akm8963_data *akm)
  116. {
  117. disable_irq(akm->irq);
  118. if (try_wait_for_completion(&akm->data_ready)) {
  119. /* we actually got the interrupt before we could disable it
  120. * so we need to enable again to undo our disable since the
  121. * irq_handler already disabled it
  122. */
  123. enable_irq(akm->irq);
  124. }
  125. }
  126. static irqreturn_t akm8963_irq_handler(int irq, void *data)
  127. {
  128. struct akm8963_data *akm = data;
  129. disable_irq_nosync(irq);
  130. complete(&akm->data_ready);
  131. return IRQ_HANDLED;
  132. }
  133. static int akm8963_wait_for_data_ready(struct akm8963_data *akm)
  134. {
  135. int data_ready = gpio_get_value(akm->pdata->gpio_data_ready_int);
  136. int err;
  137. if (data_ready)
  138. return 0;
  139. enable_irq(akm->irq);
  140. err = wait_for_completion_timeout(&akm->data_ready, 2*HZ);
  141. if (err > 0)
  142. return 0;
  143. akm8963_disable_irq(akm);
  144. if (err == 0) {
  145. pr_err("akm: wait timed out\n");
  146. return -ETIMEDOUT;
  147. }
  148. pr_err("akm: wait restart\n");
  149. return err;
  150. }
  151. static ssize_t akmd_read(struct file *file, char __user *buf,
  152. size_t count, loff_t *pos)
  153. {
  154. struct akm8963_data *akm = container_of(file->private_data,
  155. struct akm8963_data, akmd_device);
  156. short x = 0, y = 0, z = 0;
  157. int ret;
  158. u8 data[8];
  159. mutex_lock(&akm->lock);
  160. ret = akm8963_ecs_set_mode(akm, AK8963_CNTL1_SNG_MEASURE);
  161. if (ret) {
  162. mutex_unlock(&akm->lock);
  163. goto done;
  164. }
  165. ret = akm8963_wait_for_data_ready(akm);
  166. if (ret) {
  167. mutex_unlock(&akm->lock);
  168. goto done;
  169. }
  170. ret = i2c_smbus_read_i2c_block_data(akm->this_client, AK8963_REG_ST1,
  171. sizeof(data), data);
  172. mutex_unlock(&akm->lock);
  173. if (ret != sizeof(data)) {
  174. pr_err("%s: failed to read %d bytes of mag data\n",
  175. __func__, sizeof(data));
  176. goto done;
  177. }
  178. if (data[0] & 0x01) {
  179. x = (data[2] << 8) + data[1];
  180. y = (data[4] << 8) + data[3];
  181. z = (data[6] << 8) + data[5];
  182. } else
  183. pr_err("%s: invalid raw data(st1 = %d)\n",
  184. __func__, data[0] & 0x01);
  185. done:
  186. return sprintf(buf, "%d,%d,%d\n", x, y, z);
  187. }
  188. static long akmd_ioctl(struct file *file, unsigned int cmd,
  189. unsigned long arg)
  190. {
  191. void __user *argp = (void __user *)arg;
  192. struct akm8963_data *akm = container_of(file->private_data,
  193. struct akm8963_data, akmd_device);
  194. int ret;
  195. #ifdef MAGNETIC_LOGGING
  196. short x, y, z;
  197. #endif
  198. union {
  199. char raw[RWBUF_SIZE];
  200. int status;
  201. char mode;
  202. u8 data[8];
  203. } rwbuf;
  204. ret = akmd_copy_in(cmd, argp, rwbuf.raw, sizeof(rwbuf));
  205. if (ret)
  206. return ret;
  207. switch (cmd) {
  208. case ECS_IOCTL_WRITE:
  209. if ((rwbuf.raw[0] < 2) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
  210. return -EINVAL;
  211. if (copy_from_user(&rwbuf.raw[2], argp+2, rwbuf.raw[0]-1))
  212. return -EFAULT;
  213. ret = i2c_smbus_write_i2c_block_data(akm->this_client,
  214. rwbuf.raw[1],
  215. rwbuf.raw[0] - 1,
  216. &rwbuf.raw[2]);
  217. break;
  218. case ECS_IOCTL_READ:
  219. if ((rwbuf.raw[0] < 1) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
  220. return -EINVAL;
  221. ret = i2c_smbus_read_i2c_block_data(akm->this_client,
  222. rwbuf.raw[1],
  223. rwbuf.raw[0],
  224. &rwbuf.raw[1]);
  225. if (ret < 0)
  226. return ret;
  227. if (copy_to_user(argp+1, rwbuf.raw+1, rwbuf.raw[0]))
  228. return -EFAULT;
  229. return 0;
  230. case ECS_IOCTL_SET_MODE:
  231. mutex_lock(&akm->lock);
  232. ret = akm8963_ecs_set_mode(akm, rwbuf.mode);
  233. mutex_unlock(&akm->lock);
  234. break;
  235. case ECS_IOCTL_RESET:
  236. akm8963_Reset(akm);
  237. break;
  238. case ECS_IOCTL_GETDATA:
  239. mutex_lock(&akm->lock);
  240. ret = akm8963_wait_for_data_ready(akm);
  241. if (ret) {
  242. mutex_unlock(&akm->lock);
  243. return ret;
  244. }
  245. ret = i2c_smbus_read_i2c_block_data(akm->this_client,
  246. AK8963_REG_ST1,
  247. sizeof(rwbuf.data),
  248. rwbuf.data);
  249. #ifdef MAGNETIC_LOGGING
  250. x = (rwbuf.data[2] << 8) + rwbuf.data[1];
  251. y = (rwbuf.data[4] << 8) + rwbuf.data[3];
  252. z = (rwbuf.data[6] << 8) + rwbuf.data[5];
  253. pr_info("%s:ST1=%d, x=%d, y=%d, z=%d, ST2=%d\n",
  254. __func__, rwbuf.data[0], x, y, z, rwbuf.data[7]);
  255. #endif
  256. mutex_unlock(&akm->lock);
  257. if (ret != sizeof(rwbuf.data)) {
  258. pr_err("%s : failed to read %d bytes of mag data\n",
  259. __func__, sizeof(rwbuf.data));
  260. return -EIO;
  261. }
  262. break;
  263. default:
  264. return -ENOTTY;
  265. }
  266. if (ret < 0)
  267. return ret;
  268. return akmd_copy_out(cmd, argp, rwbuf.raw, sizeof(rwbuf));
  269. }
  270. static const struct file_operations akmd_fops = {
  271. .owner = THIS_MODULE,
  272. .open = nonseekable_open,
  273. .read = akmd_read,
  274. .unlocked_ioctl = akmd_ioctl,
  275. };
  276. static int akm8963_setup_irq(struct akm8963_data *akm)
  277. {
  278. int rc = -EIO;
  279. struct akm8963_platform_data *pdata = akm->pdata;
  280. int irq;
  281. if (akm->this_client->irq)
  282. irq = akm->this_client->irq;
  283. else {
  284. rc = gpio_request(pdata->gpio_data_ready_int, "gpio_akm_int");
  285. if (rc < 0) {
  286. pr_err("%s: gpio %d request failed (%d)\n",
  287. __func__, pdata->gpio_data_ready_int, rc);
  288. return rc;
  289. }
  290. rc = gpio_direction_input(pdata->gpio_data_ready_int);
  291. if (rc < 0) {
  292. pr_err("%s: failed to set gpio %d as input (%d)\n",
  293. __func__, pdata->gpio_data_ready_int, rc);
  294. goto err_request_irq;
  295. }
  296. irq = gpio_to_irq(pdata->gpio_data_ready_int);
  297. }
  298. /* trigger high so we don't miss initial interrupt if it
  299. * is already pending
  300. */
  301. rc = request_irq(irq, akm8963_irq_handler,
  302. IRQF_TRIGGER_RISING | IRQF_ONESHOT, "akm_int", akm);
  303. if (rc < 0) {
  304. pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
  305. __func__, irq,
  306. pdata->gpio_data_ready_int, rc);
  307. goto err_request_irq;
  308. }
  309. /* start with interrupt disabled until the driver is enabled */
  310. akm->irq = irq;
  311. akm8963_disable_irq(akm);
  312. goto done;
  313. err_request_irq:
  314. gpio_free(pdata->gpio_data_ready_int);
  315. done:
  316. return rc;
  317. }
  318. #ifdef FACTORY_TEST
  319. static int ak8963c_selftest(struct akm8963_data *ak_data, int *sf)
  320. {
  321. u8 buf[6];
  322. s16 x, y, z;
  323. int retry_count = 0;
  324. retry:
  325. /* read device info */
  326. i2c_smbus_read_i2c_block_data(ak_data->this_client,
  327. AK8963_REG_WIA, 2, buf);
  328. pr_info("%s: device id = 0x%x, info = 0x%x\n",
  329. __func__, buf[0], buf[1]);
  330. /* set ATSC self test bit to 1 */
  331. i2c_smbus_write_byte_data(ak_data->this_client,
  332. AK8963_REG_ASTC, 0x40);
  333. /* start self test */
  334. i2c_smbus_write_byte_data(ak_data->this_client,
  335. AK8963_REG_CNTL1,
  336. AK8963_CNTL1_SELF_TEST);
  337. /* wait for data ready */
  338. while (1) {
  339. msleep(20);
  340. if (i2c_smbus_read_byte_data(ak_data->this_client,
  341. AK8963_REG_ST1) == 1) {
  342. break;
  343. }
  344. }
  345. i2c_smbus_read_i2c_block_data(ak_data->this_client,
  346. AK8963_REG_HXL, sizeof(buf), buf);
  347. /* set ATSC self test bit to 0 */
  348. i2c_smbus_write_byte_data(ak_data->this_client,
  349. AK8963_REG_ASTC, 0x00);
  350. x = buf[0] | (buf[1] << 8);
  351. y = buf[2] | (buf[3] << 8);
  352. z = buf[4] | (buf[5] << 8);
  353. /* Hadj = (H*(Asa+128))/256 */
  354. x = (x*(ak_data->asa[0] + 128)) >> 8;
  355. y = (y*(ak_data->asa[1] + 128)) >> 8;
  356. z = (z*(ak_data->asa[2] + 128)) >> 8;
  357. pr_info("%s: self test x = %d, y = %d, z = %d\n",
  358. __func__, x, y, z);
  359. if ((x >= -200) && (x <= 200))
  360. pr_info("%s: x passed self test, expect -200<=x<=200\n",
  361. __func__);
  362. else
  363. pr_info("%s: x failed self test, expect -200<=x<=200\n",
  364. __func__);
  365. if ((y >= -200) && (y <= 200))
  366. pr_info("%s: y passed self test, expect -200<=y<=200\n",
  367. __func__);
  368. else
  369. pr_info("%s: y failed self test, expect -200<=y<=200\n",
  370. __func__);
  371. if ((z >= -3200) && (z <= -800))
  372. pr_info("%s: z passed self test, expect -3200<=z<=-800\n",
  373. __func__);
  374. else
  375. pr_info("%s: z failed self test, expect -3200<=z<=-800\n",
  376. __func__);
  377. sf[0] = x;
  378. sf[1] = y;
  379. sf[2] = z;
  380. if (((x >= -200) && (x <= 200)) &&
  381. ((y >= -200) && (y <= 200)) &&
  382. ((z >= -3200) && (z <= -800))) {
  383. pr_info("%s, Selftest is successful.\n", __func__);
  384. return 1;
  385. } else {
  386. if (retry_count < 5) {
  387. retry_count++;
  388. pr_warn("############################################");
  389. pr_warn("%s, retry_count=%d\n", __func__, retry_count);
  390. pr_warn("############################################");
  391. goto retry;
  392. } else {
  393. pr_err("%s, Selftest is failed.\n", __func__);
  394. return 0;
  395. }
  396. }
  397. }
  398. static ssize_t ak8963c_get_asa(struct device *dev,
  399. struct device_attribute *attr, char *buf)
  400. {
  401. struct akm8963_data *ak_data = dev_get_drvdata(dev);
  402. return sprintf(buf, "%d, %d, %d\n", ak_data->asa[0],
  403. ak_data->asa[1], ak_data->asa[2]);
  404. }
  405. static ssize_t ak8963c_get_selftest(struct device *dev,
  406. struct device_attribute *attr, char *buf)
  407. {
  408. int ret = 0;
  409. int sf[3] = {0,};
  410. ret = ak8963c_selftest(dev_get_drvdata(dev), sf);
  411. return sprintf(buf, "%d, %d, %d, %d\n", ret, sf[0], sf[1], sf[2]);
  412. }
  413. static ssize_t ak8963c_check_registers(struct device *dev,
  414. struct device_attribute *attr, char *strbuf)
  415. {
  416. struct akm8963_data *ak_data = dev_get_drvdata(dev);
  417. u8 buf[13];
  418. /* power down */
  419. i2c_smbus_write_byte_data(ak_data->this_client,
  420. AK8963_REG_CNTL1, AK8963_CNTL1_POWER_DOWN);
  421. /* get the value */
  422. i2c_smbus_read_i2c_block_data(ak_data->this_client,
  423. AK8963_REG_WIA, 11, buf);
  424. buf[11] = i2c_smbus_read_byte_data(ak_data->this_client,
  425. AK8963_REG_ASTC);
  426. buf[12] = i2c_smbus_read_byte_data(ak_data->this_client,
  427. AK8963_REG_I2CDIS);
  428. return sprintf(strbuf, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
  429. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
  430. buf[6], buf[7], buf[8], buf[9], buf[10], buf[11],
  431. buf[12]);
  432. }
  433. static ssize_t ak8963c_check_cntl(struct device *dev,
  434. struct device_attribute *attr, char *strbuf)
  435. {
  436. struct akm8963_data *ak_data = dev_get_drvdata(dev);
  437. u8 buf;
  438. int err;
  439. /* power down */
  440. err = i2c_smbus_write_byte_data(ak_data->this_client,
  441. AK8963_REG_CNTL1, AK8963_CNTL1_POWER_DOWN);
  442. buf = i2c_smbus_read_byte_data(ak_data->this_client,
  443. AK8963_REG_CNTL1);
  444. return sprintf(strbuf, "%s\n",
  445. ((buf == AK8963_CNTL1_POWER_DOWN) ? "OK" : "NG"));
  446. }
  447. static ssize_t ak8963c_get_status(struct device *dev,
  448. struct device_attribute *attr, char *buf)
  449. {
  450. struct akm8963_data *ak_data = dev_get_drvdata(dev);
  451. int success;
  452. if ((ak_data->asa[0] == 0) | (ak_data->asa[0] == 0xff)
  453. | (ak_data->asa[1] == 0) | (ak_data->asa[1] == 0xff)
  454. | (ak_data->asa[2] == 0) | (ak_data->asa[2] == 0xff))
  455. success = 0;
  456. else
  457. success = 1;
  458. return sprintf(buf, "%s\n", (success ? "OK" : "NG"));
  459. }
  460. static ssize_t ak8963_adc(struct device *dev,
  461. struct device_attribute *attr, char *strbuf)
  462. {
  463. struct akm8963_data *ak_data = dev_get_drvdata(dev);
  464. u8 buf[8];
  465. s16 x, y, z;
  466. int err, success;
  467. /* start ADC conversion */
  468. err = i2c_smbus_write_byte_data(ak_data->this_client,
  469. AK8963_REG_CNTL1, AK8963_CNTL1_SNG_MEASURE);
  470. /* wait for ADC conversion to complete */
  471. err = akm8963_wait_for_data_ready(ak_data);
  472. if (err) {
  473. pr_err("%s: wait for data ready failed\n", __func__);
  474. return err;
  475. }
  476. msleep(20);
  477. /* get the value and report it */
  478. err = i2c_smbus_read_i2c_block_data(ak_data->this_client,
  479. AK8963_REG_ST1, sizeof(buf), buf);
  480. if (err != sizeof(buf)) {
  481. pr_err("%s: read data over i2c failed\n", __func__);
  482. return err;
  483. }
  484. /* buf[0] is status1, buf[7] is status2 */
  485. if ((buf[0] == 0) | (buf[7] == 1))
  486. success = 0;
  487. else
  488. success = 1;
  489. x = buf[1] | (buf[2] << 8);
  490. y = buf[3] | (buf[4] << 8);
  491. z = buf[5] | (buf[6] << 8);
  492. pr_info("raw x = %d, y = %d, z = %d\n", x, y, z);
  493. return sprintf(strbuf,
  494. "%s, %d, %d, %d\n", (success ? "OK" : "NG"), x, y, z);
  495. }
  496. #endif
  497. static ssize_t ak8963_show_raw_data(struct device *dev,
  498. struct device_attribute *attr, char *buf)
  499. {
  500. struct akm8963_data *akm = dev_get_drvdata(dev);
  501. short x = 0, y = 0, z = 0;
  502. int ret;
  503. u8 data[8] = {0,};
  504. mutex_lock(&akm->lock);
  505. ret = akm8963_ecs_set_mode(akm, AK8963_CNTL1_SNG_MEASURE);
  506. if (ret) {
  507. mutex_unlock(&akm->lock);
  508. goto done;
  509. }
  510. ret = akm8963_wait_for_data_ready(akm);
  511. if (ret) {
  512. mutex_unlock(&akm->lock);
  513. goto done;
  514. }
  515. ret = i2c_smbus_read_i2c_block_data(akm->this_client, AK8963_REG_ST1,
  516. sizeof(data), data);
  517. mutex_unlock(&akm->lock);
  518. if (ret != sizeof(data)) {
  519. pr_err("%s: failed to read %d bytes of mag data\n",
  520. __func__, sizeof(data));
  521. goto done;
  522. }
  523. if (data[0] & 0x01) {
  524. x = (data[2] << 8) + data[1];
  525. y = (data[4] << 8) + data[3];
  526. z = (data[6] << 8) + data[5];
  527. } else
  528. pr_err("%s: invalid raw data(st1 = %d)\n",
  529. __func__, data[0] & 0x01);
  530. done:
  531. return sprintf(buf, "%d,%d,%d\n", x, y, z);
  532. }
  533. static ssize_t ak8963_show_vendor(struct device *dev,
  534. struct device_attribute *attr, char *buf)
  535. {
  536. return sprintf(buf, "%s\n", VENDOR);
  537. }
  538. static ssize_t ak8963_show_name(struct device *dev,
  539. struct device_attribute *attr, char *buf)
  540. {
  541. return sprintf(buf, "%s\n", CHIP_ID);
  542. }
  543. static DEVICE_ATTR(raw_data, 0664,
  544. ak8963_show_raw_data, NULL);
  545. static DEVICE_ATTR(vendor, 0664,
  546. ak8963_show_vendor, NULL);
  547. static DEVICE_ATTR(name, 0664,
  548. ak8963_show_name, NULL);
  549. #ifdef FACTORY_TEST
  550. static DEVICE_ATTR(asa, 0664,
  551. ak8963c_get_asa, NULL);
  552. static DEVICE_ATTR(selftest, 0664,
  553. ak8963c_get_selftest, NULL);
  554. static DEVICE_ATTR(chk_registers, 0664,
  555. ak8963c_check_registers, NULL);
  556. static DEVICE_ATTR(dac, 0664,
  557. ak8963c_check_cntl, NULL);
  558. static DEVICE_ATTR(status, 0664,
  559. ak8963c_get_status, NULL);
  560. static DEVICE_ATTR(adc, 0664,
  561. ak8963_adc, NULL);
  562. #endif
  563. int akm8963_probe(struct i2c_client *client,
  564. const struct i2c_device_id *devid)
  565. {
  566. struct akm8963_data *akm;
  567. int err;
  568. pr_info("%s is called.\n", __func__);
  569. if (client->dev.platform_data == NULL && client->irq == 0) {
  570. dev_err(&client->dev, "platform data & irq are NULL.\n");
  571. err = -ENODEV;
  572. goto exit_platform_data_null;
  573. }
  574. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  575. dev_err(&client->dev, "I2C check failed, exiting.\n");
  576. err = -ENODEV;
  577. goto exit_check_functionality_failed;
  578. }
  579. akm = kzalloc(sizeof(struct akm8963_data), GFP_KERNEL);
  580. if (!akm) {
  581. dev_err(&client->dev,
  582. "failed to allocate memory for module data\n");
  583. err = -ENOMEM;
  584. goto exit_alloc_data_failed;
  585. }
  586. akm->pdata = client->dev.platform_data;
  587. mutex_init(&akm->lock);
  588. init_completion(&akm->data_ready);
  589. i2c_set_clientdata(client, akm);
  590. akm->this_client = client;
  591. err = akm8963_ecs_set_mode_power_down(akm);
  592. if (err < 0) {
  593. pr_err("%s: akm8963_ecs_set_mode_power_down fail(err=%d)\n",
  594. __func__, err);
  595. goto exit_set_mode_power_down_failed;
  596. }
  597. err = akm8963_setup_irq(akm);
  598. if (err) {
  599. pr_err("%s: could not setup irq\n", __func__);
  600. goto exit_setup_irq;
  601. }
  602. akm->akmd_device.minor = MISC_DYNAMIC_MINOR;
  603. akm->akmd_device.name = "akm8963";
  604. akm->akmd_device.fops = &akmd_fops;
  605. err = misc_register(&akm->akmd_device);
  606. if (err) {
  607. pr_err("%s, misc_register failed.\n", __func__);
  608. goto exit_akmd_device_register_failed;
  609. }
  610. init_waitqueue_head(&akm->state_wq);
  611. /* put into fuse access mode to read asa data */
  612. err = i2c_smbus_write_byte_data(client, AK8963_REG_CNTL1,
  613. AK8963_CNTL1_FUSE_ACCESS);
  614. if (err) {
  615. pr_err("%s: unable to enter fuse rom mode\n", __func__);
  616. goto exit_i2c_failed;
  617. }
  618. err = i2c_smbus_read_i2c_block_data(client, AK8963_FUSE_ASAX,
  619. sizeof(akm->asa), akm->asa);
  620. if (err != sizeof(akm->asa)) {
  621. pr_err("%s: unable to load factory sensitivity adjust values\n",
  622. __func__);
  623. goto exit_i2c_failed;
  624. } else
  625. pr_info("%s: asa_x = %d, asa_y = %d, asa_z = %d\n", __func__,
  626. akm->asa[0], akm->asa[1], akm->asa[2]);
  627. err = i2c_smbus_write_byte_data(client, AK8963_REG_CNTL1,
  628. AK8963_CNTL1_POWER_DOWN);
  629. if (err) {
  630. dev_err(&client->dev, "Error in setting power down mode\n");
  631. goto exit_i2c_failed;
  632. }
  633. akm->dev = sensors_classdev_register("magnetic_sensor");
  634. if (IS_ERR(akm->dev)) {
  635. pr_err("Failed to create device!");
  636. goto exit_class_create_failed;
  637. }
  638. if (device_create_file(akm->dev, &dev_attr_raw_data) < 0) {
  639. pr_err("Failed to create device file(%s)!\n",
  640. dev_attr_raw_data.attr.name);
  641. goto exit_device_create_raw_data;
  642. }
  643. if (device_create_file(akm->dev, &dev_attr_vendor) < 0) {
  644. pr_err("Failed to create device file(%s)!\n",
  645. dev_attr_name.attr.name);
  646. goto exit_device_create_vendor;
  647. }
  648. if (device_create_file(akm->dev, &dev_attr_name) < 0) {
  649. pr_err("Failed to create device file(%s)!\n",
  650. dev_attr_raw_data.attr.name);
  651. goto exit_device_create_name;
  652. }
  653. #ifdef FACTORY_TEST
  654. if (device_create_file(akm->dev, &dev_attr_adc) < 0) {
  655. pr_err("Failed to create device file(%s)!\n",
  656. dev_attr_adc.attr.name);
  657. goto exit_device_create_file1;
  658. }
  659. if (device_create_file(akm->dev, &dev_attr_status) < 0) {
  660. pr_err("Failed to create device file(%s)!\n",
  661. dev_attr_status.attr.name);
  662. goto exit_device_create_file2;
  663. }
  664. if (device_create_file(akm->dev, &dev_attr_asa) < 0) {
  665. pr_err("Failed to create device file(%s)!\n",
  666. dev_attr_asa.attr.name);
  667. goto exit_device_create_file3;
  668. }
  669. if (device_create_file(akm->dev, &dev_attr_selftest) < 0) {
  670. pr_err("Failed to create device file(%s)!\n",
  671. dev_attr_selftest.attr.name);
  672. goto exit_device_create_file4;
  673. }
  674. if (device_create_file(akm->dev,
  675. &dev_attr_chk_registers) < 0) {
  676. pr_err("Failed to create device file(%s)!\n",
  677. dev_attr_chk_registers.attr.name);
  678. goto exit_device_create_file5;
  679. }
  680. if (device_create_file(akm->dev, &dev_attr_dac) < 0) {
  681. pr_err("Failed to create device file(%s)!\n",
  682. dev_attr_dac.attr.name);
  683. goto exit_device_create_file6;
  684. }
  685. #endif
  686. dev_set_drvdata(akm->dev, akm);
  687. pr_info("%s is successful.\n", __func__);
  688. return 0;
  689. #ifdef FACTORY_TEST
  690. exit_device_create_file6:
  691. device_remove_file(akm->dev, &dev_attr_chk_registers);
  692. exit_device_create_file5:
  693. device_remove_file(akm->dev, &dev_attr_selftest);
  694. exit_device_create_file4:
  695. device_remove_file(akm->dev, &dev_attr_asa);
  696. exit_device_create_file3:
  697. device_remove_file(akm->dev, &dev_attr_status);
  698. exit_device_create_file2:
  699. device_remove_file(akm->dev, &dev_attr_adc);
  700. exit_device_create_file1:
  701. device_remove_file(akm->dev, &dev_attr_name);
  702. #endif
  703. exit_device_create_name:
  704. device_remove_file(akm->dev, &dev_attr_vendor);
  705. exit_device_create_vendor:
  706. device_remove_file(akm->dev, &dev_attr_raw_data);
  707. exit_device_create_raw_data:
  708. sensors_classdev_unregister(akm->dev);
  709. exit_class_create_failed:
  710. exit_i2c_failed:
  711. misc_deregister(&akm->akmd_device);
  712. exit_akmd_device_register_failed:
  713. free_irq(akm->irq, akm);
  714. gpio_free(akm->pdata->gpio_data_ready_int);
  715. exit_setup_irq:
  716. exit_set_mode_power_down_failed:
  717. mutex_destroy(&akm->lock);
  718. kfree(akm);
  719. exit_alloc_data_failed:
  720. exit_check_functionality_failed:
  721. exit_platform_data_null:
  722. return err;
  723. }
  724. static int __devexit akm8963_remove(struct i2c_client *client)
  725. {
  726. struct akm8963_data *akm = i2c_get_clientdata(client);
  727. #ifdef FACTORY_TEST
  728. device_remove_file(akm->dev, &dev_attr_adc);
  729. device_remove_file(akm->dev, &dev_attr_status);
  730. device_remove_file(akm->dev, &dev_attr_asa);
  731. device_remove_file(akm->dev, &dev_attr_selftest);
  732. device_remove_file(akm->dev, &dev_attr_chk_registers);
  733. device_remove_file(akm->dev, &dev_attr_dac);
  734. #endif
  735. device_remove_file(akm->dev, &dev_attr_name);
  736. device_remove_file(akm->dev, &dev_attr_vendor);
  737. device_remove_file(akm->dev, &dev_attr_raw_data);
  738. sensors_classdev_unregister(akm->dev);
  739. misc_deregister(&akm->akmd_device);
  740. free_irq(akm->irq, akm);
  741. gpio_free(akm->pdata->gpio_data_ready_int);
  742. mutex_destroy(&akm->lock);
  743. kfree(akm);
  744. return 0;
  745. }
  746. static const struct i2c_device_id akm8963_id[] = {
  747. {AKM8963_I2C_NAME, 0 },
  748. { }
  749. };
  750. static struct i2c_driver akm8963_driver = {
  751. .probe = akm8963_probe,
  752. .remove = akm8963_remove,
  753. .id_table = akm8963_id,
  754. .driver = {
  755. .name = AKM8963_I2C_NAME,
  756. },
  757. };
  758. static int __init akm8963_init(void)
  759. {
  760. return i2c_add_driver(&akm8963_driver);
  761. }
  762. static void __exit akm8963_exit(void)
  763. {
  764. i2c_del_driver(&akm8963_driver);
  765. }
  766. module_init(akm8963_init);
  767. module_exit(akm8963_exit);
  768. MODULE_DESCRIPTION("AKM8963 compass driver");
  769. MODULE_AUTHOR("Samsung Electronics");
  770. MODULE_LICENSE("GPL");