ak8963c.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. * Copyright (C) 2013 Samsung Electronics. 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. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/errno.h>
  23. #include <linux/input.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/completion.h>
  28. #include <linux/gpio.h>
  29. #include <linux/of_gpio.h>
  30. #include "sensors_core.h"
  31. #include "ak8963c_reg.h"
  32. /* Rx buffer size. i.e ST,TMPS,H1X,H1Y,H1Z*/
  33. #define SENSOR_DATA_SIZE 8
  34. #define AK8963C_DEFAULT_DELAY 200000000LL
  35. #define I2C_M_WR 0 /* for i2c Write */
  36. #define I2c_M_RD 1 /* for i2c Read */
  37. #define VENDOR_NAME "AKM"
  38. #define MODEL_NAME "AK8963C"
  39. #define MODULE_NAME "magnetic_sensor"
  40. #define AK8963C_TOP_LOWER_RIGHT 0
  41. #define AK8963C_TOP_LOWER_LEFT 1
  42. #define AK8963C_TOP_UPPER_LEFT 2
  43. #define AK8963C_TOP_UPPER_RIGHT 3
  44. #define AK8963C_BOTTOM_LOWER_RIGHT 4
  45. #define AK8963C_BOTTOM_LOWER_LEFT 5
  46. #define AK8963C_BOTTOM_UPPER_LEFT 6
  47. #define AK8963C_BOTTOM_UPPER_RIGHT 7
  48. struct ak8963c_v {
  49. union {
  50. s16 v[3];
  51. struct {
  52. s16 x;
  53. s16 y;
  54. s16 z;
  55. };
  56. };
  57. };
  58. struct ak8963c_p {
  59. struct i2c_client *client;
  60. struct input_dev *input;
  61. struct device *factory_device;
  62. struct ak8963c_v magdata;
  63. struct mutex lock;
  64. struct completion data_ready;
  65. struct delayed_work work;
  66. atomic_t delay;
  67. atomic_t enable;
  68. wait_queue_head_t state_wq;
  69. u8 asa[3];
  70. u32 chip_pos;
  71. int irq;
  72. int m_rst_n;
  73. int m_sensor_int;
  74. };
  75. static int ak8963c_i2c_read(struct i2c_client *client,
  76. unsigned char reg_addr, unsigned char *buf)
  77. {
  78. int ret;
  79. struct i2c_msg msg[2];
  80. msg[0].addr = client->addr;
  81. msg[0].flags = I2C_M_WR;
  82. msg[0].len = 1;
  83. msg[0].buf = &reg_addr;
  84. msg[1].addr = client->addr;
  85. msg[1].flags = I2C_M_RD;
  86. msg[1].len = 1;
  87. msg[1].buf = buf;
  88. ret = i2c_transfer(client->adapter, msg, 2);
  89. if (ret < 0) {
  90. pr_err("[SENSOR]: %s - i2c bus read error %d\n",
  91. __func__, ret);
  92. return ret;
  93. }
  94. return 0;
  95. }
  96. static int ak8963c_i2c_write(struct i2c_client *client,
  97. unsigned char reg_addr, unsigned char buf)
  98. {
  99. int ret;
  100. struct i2c_msg msg;
  101. unsigned char w_buf[2];
  102. w_buf[0] = reg_addr;
  103. w_buf[1] = buf;
  104. msg.addr = client->addr;
  105. msg.flags = I2C_M_WR;
  106. msg.len = 2;
  107. msg.buf = (char *)w_buf;
  108. ret = i2c_transfer(client->adapter, &msg, 1);
  109. if (ret < 0) {
  110. pr_err("[SENSOR]: %s - i2c bus write error %d\n",
  111. __func__, ret);
  112. return ret;
  113. }
  114. return 0;
  115. }
  116. static int ak8963c_i2c_read_block(struct i2c_client *client,
  117. unsigned char reg_addr, unsigned char *buf, unsigned char len)
  118. {
  119. int i, ret = 0;
  120. for (i = 0; i < len; i++)
  121. ret += ak8963c_i2c_read(client, reg_addr + i, &buf[i]);
  122. return ret;
  123. }
  124. static void ak8963c_disable_irq(struct ak8963c_p *data)
  125. {
  126. disable_irq(data->irq);
  127. if (try_wait_for_completion(&data->data_ready)) {
  128. /* we actually got the interrupt before we could disable it
  129. * so we need to enable again to undo our disable since the
  130. * irq_handler already disabled it
  131. */
  132. enable_irq(data->irq);
  133. }
  134. }
  135. static int ak8963c_ecs_set_mode_power_down(struct ak8963c_p *data)
  136. {
  137. unsigned char reg;
  138. int ret;
  139. reg = AK8963C_CNTL1_POWER_DOWN;
  140. ret = ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  141. return ret;
  142. }
  143. static int ak8963c_ecs_set_mode(struct ak8963c_p *data, char mode)
  144. {
  145. u8 reg;
  146. int ret;
  147. switch (mode) {
  148. case AK8963C_CNTL1_SNG_MEASURE:
  149. reg = AK8963C_CNTL1_SNG_MEASURE;
  150. ret = ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  151. break;
  152. case AK8963C_CNTL1_FUSE_ACCESS:
  153. reg = AK8963C_CNTL1_FUSE_ACCESS;
  154. ret = ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  155. break;
  156. case AK8963C_CNTL1_POWER_DOWN:
  157. reg = AK8963C_CNTL1_SNG_MEASURE;
  158. ret = ak8963c_ecs_set_mode_power_down(data);
  159. break;
  160. case AK8963C_CNTL1_SELF_TEST:
  161. reg = AK8963C_CNTL1_SELF_TEST;
  162. ret = ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. if (ret < 0)
  168. return ret;
  169. /* Wait at least 300us after changing mode. */
  170. udelay(300);
  171. return 0;
  172. }
  173. static irqreturn_t ak8963c_irq_handler(int irq, void *pdata)
  174. {
  175. struct ak8963c_p *data = pdata;
  176. disable_irq_nosync(irq);
  177. complete(&data->data_ready);
  178. return IRQ_HANDLED;
  179. }
  180. static int ak8963c_wait_for_data_ready(struct ak8963c_p *data)
  181. {
  182. int ret;
  183. int data_ready = gpio_get_value(data->m_sensor_int);
  184. if (data_ready)
  185. return 0;
  186. enable_irq(data->irq);
  187. ret = wait_for_completion_timeout(&data->data_ready, 2 * HZ);
  188. if (ret > 0)
  189. return 0;
  190. ak8963c_disable_irq(data);
  191. if (ret == 0) {
  192. pr_err("[SENSOR]: %s - wait timed out\n", __func__);
  193. return -ETIMEDOUT;
  194. }
  195. pr_err("[SENSOR]: %s - wait restart\n", __func__);
  196. return ret;
  197. }
  198. static int ak8963c_read_mag_xyz(struct ak8963c_p *data, struct ak8963c_v *mag)
  199. {
  200. u8 temp[SENSOR_DATA_SIZE];
  201. int ret;
  202. mutex_lock(&data->lock);
  203. ret = ak8963c_ecs_set_mode(data, AK8963C_CNTL1_SNG_MEASURE);
  204. if (ret) {
  205. mutex_unlock(&data->lock);
  206. return ret;
  207. }
  208. ret = ak8963c_wait_for_data_ready(data);
  209. if (ret) {
  210. mutex_unlock(&data->lock);
  211. return ret;
  212. }
  213. ret = ak8963c_i2c_read_block(data->client, AK8963C_REG_ST1,
  214. temp, SENSOR_DATA_SIZE);
  215. mutex_unlock(&data->lock);
  216. mag->x = temp[1] | (temp[2] << 8);
  217. mag->y = temp[3] | (temp[4] << 8);
  218. mag->z = temp[5] | (temp[6] << 8);
  219. remap_sensor_data(mag->v, data->chip_pos);
  220. return ret;
  221. }
  222. static void ak8963c_work_func(struct work_struct *work)
  223. {
  224. struct ak8963c_v mag;
  225. struct ak8963c_p *data = container_of((struct delayed_work *)work,
  226. struct ak8963c_p, work);
  227. unsigned long delay = nsecs_to_jiffies(atomic_read(&data->delay));
  228. ak8963c_read_mag_xyz(data, &mag);
  229. input_report_rel(data->input, REL_X, mag.x);
  230. input_report_rel(data->input, REL_Y, mag.y);
  231. input_report_rel(data->input, REL_Z, mag.z);
  232. input_sync(data->input);
  233. data->magdata = mag;
  234. schedule_delayed_work(&data->work, delay);
  235. }
  236. static void ak8963c_set_enable(struct ak8963c_p *data, int enable)
  237. {
  238. int pre_enable = atomic_read(&data->enable);
  239. if (enable) {
  240. if (pre_enable == 0) {
  241. ak8963c_ecs_set_mode(data, AK8963C_CNTL1_SNG_MEASURE);
  242. schedule_delayed_work(&data->work,
  243. nsecs_to_jiffies(atomic_read(&data->delay)));
  244. atomic_set(&data->enable, 1);
  245. }
  246. } else {
  247. if (pre_enable == 1) {
  248. ak8963c_ecs_set_mode(data, AK8963C_CNTL1_POWER_DOWN);
  249. cancel_delayed_work_sync(&data->work);
  250. atomic_set(&data->enable, 0);
  251. }
  252. }
  253. }
  254. static ssize_t ak8963c_enable_show(struct device *dev,
  255. struct device_attribute *attr, char *buf)
  256. {
  257. struct ak8963c_p *data = dev_get_drvdata(dev);
  258. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&data->enable));
  259. }
  260. static ssize_t ak8963c_enable_store(struct device *dev,
  261. struct device_attribute *attr, const char *buf, size_t size)
  262. {
  263. u8 enable;
  264. int ret;
  265. struct ak8963c_p *data = dev_get_drvdata(dev);
  266. ret = kstrtou8(buf, 2, &enable);
  267. if (ret) {
  268. pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
  269. return ret;
  270. }
  271. pr_info("[SENSOR]: %s - new_value = %u\n", __func__, enable);
  272. if ((enable == 0) || (enable == 1))
  273. ak8963c_set_enable(data, (int)enable);
  274. return size;
  275. }
  276. static ssize_t ak8963c_delay_show(struct device *dev,
  277. struct device_attribute *attr, char *buf)
  278. {
  279. struct ak8963c_p *data = dev_get_drvdata(dev);
  280. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&data->delay));
  281. }
  282. static ssize_t ak8963c_delay_store(struct device *dev,
  283. struct device_attribute *attr,
  284. const char *buf, size_t size)
  285. {
  286. int ret;
  287. int64_t delay;
  288. struct ak8963c_p *data = dev_get_drvdata(dev);
  289. ret = kstrtoll(buf, 10, &delay);
  290. if (ret) {
  291. pr_err("[SENSOR]: %s - Invalid Argument\n", __func__);
  292. return ret;
  293. }
  294. atomic_set(&data->delay, (int64_t)delay);
  295. pr_info("[SENSOR]: %s - poll_delay = %lld\n", __func__, delay);
  296. return size;
  297. }
  298. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  299. ak8963c_delay_show, ak8963c_delay_store);
  300. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  301. ak8963c_enable_show, ak8963c_enable_store);
  302. static struct attribute *ak8963c_attributes[] = {
  303. &dev_attr_poll_delay.attr,
  304. &dev_attr_enable.attr,
  305. NULL
  306. };
  307. static struct attribute_group ak8963c_attribute_group = {
  308. .attrs = ak8963c_attributes
  309. };
  310. static int ak8963c_selftest(struct ak8963c_p *data, int *sf)
  311. {
  312. u8 temp[6], reg;
  313. s16 x, y, z;
  314. int retry_count = 0;
  315. int ready_count = 0;
  316. int ret;
  317. retry:
  318. mutex_lock(&data->lock);
  319. /* power down */
  320. reg = AK8963C_CNTL1_POWER_DOWN;
  321. ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  322. /* read device info */
  323. ak8963c_i2c_read_block(data->client, AK8963C_REG_WIA, temp, 2);
  324. pr_info("[SENSOR]: %s - device id = 0x%x, info = 0x%x\n",
  325. __func__, temp[0], temp[1]);
  326. /* set ATSC self test bit to 1 */
  327. reg = 0x40;
  328. ak8963c_i2c_write(data->client, AK8963C_REG_ASTC, reg);
  329. /* start self test */
  330. reg = AK8963C_CNTL1_SELF_TEST;
  331. ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  332. /* wait for data ready */
  333. while (ready_count < 10) {
  334. msleep(20);
  335. ret = ak8963c_i2c_read(data->client, AK8963C_REG_ST1, &reg);
  336. if ((reg == 1) && (ret == 0))
  337. break;
  338. ready_count++;
  339. }
  340. ak8963c_i2c_read_block(data->client, AK8963C_REG_HXL,
  341. temp, sizeof(temp));
  342. /* set ATSC self test bit to 0 */
  343. reg = 0x00;
  344. ak8963c_i2c_write(data->client, AK8963C_REG_ASTC, reg);
  345. mutex_unlock(&data->lock);
  346. x = temp[0] | (temp[1] << 8);
  347. y = temp[2] | (temp[3] << 8);
  348. z = temp[4] | (temp[5] << 8);
  349. /* Hadj = (H*(Asa+128))/256 */
  350. x = (x * (data->asa[0] + 128)) >> 8;
  351. y = (y * (data->asa[1] + 128)) >> 8;
  352. z = (z * (data->asa[2] + 128)) >> 8;
  353. pr_info("[SENSOR]: %s - self test x = %d, y = %d, z = %d\n",
  354. __func__, x, y, z);
  355. if ((x >= -200) && (x <= 200))
  356. pr_info("[SENSOR]: %s - x passed self test, -200<=x<=200\n",
  357. __func__);
  358. else
  359. pr_info("[SENSOR]: %s - x failed self test, -200<=x<=200\n",
  360. __func__);
  361. if ((y >= -200) && (y <= 200))
  362. pr_info("[SENSOR]: %s - y passed self test, -200<=y<=200\n",
  363. __func__);
  364. else
  365. pr_info("[SENSOR]: %s - y failed self test, -200<=y<=200\n",
  366. __func__);
  367. if ((z >= -3200) && (z <= -800))
  368. pr_info("[SENSOR]: %s - z passed self test, -3200<=z<=-800\n",
  369. __func__);
  370. else
  371. pr_info("[SENSOR]: %s - z failed self test, -3200<=z<=-800\n",
  372. __func__);
  373. sf[0] = x;
  374. sf[1] = y;
  375. sf[2] = z;
  376. if (((x >= -200) && (x <= 200)) &&
  377. ((y >= -200) && (y <= 200)) &&
  378. ((z >= -3200) && (z <= -800))) {
  379. pr_info("%s, Selftest is successful.\n", __func__);
  380. return 1;
  381. } else {
  382. if (retry_count < 5) {
  383. retry_count++;
  384. pr_warn("############################################");
  385. pr_warn("%s, retry_count=%d\n", __func__, retry_count);
  386. pr_warn("############################################");
  387. goto retry;
  388. } else {
  389. pr_err("[SENSOR]: %s - Selftest is failed.\n",
  390. __func__);
  391. return 0;
  392. }
  393. }
  394. }
  395. static ssize_t ak8963c_vendor_show(struct device *dev,
  396. struct device_attribute *attr, char *buf)
  397. {
  398. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
  399. }
  400. static ssize_t ak8963c_name_show(struct device *dev,
  401. struct device_attribute *attr, char *buf)
  402. {
  403. return snprintf(buf, PAGE_SIZE, "%s\n", MODEL_NAME);
  404. }
  405. static ssize_t ak8963c_get_asa(struct device *dev,
  406. struct device_attribute *attr, char *buf)
  407. {
  408. struct ak8963c_p *data = dev_get_drvdata(dev);
  409. return snprintf(buf, PAGE_SIZE, "%u,%u,%u\n",
  410. data->asa[0], data->asa[1], data->asa[2]);
  411. }
  412. static ssize_t ak8963c_get_selftest(struct device *dev,
  413. struct device_attribute *attr, char *buf)
  414. {
  415. int ret = 0;
  416. int sf[3] = {0,};
  417. ret = ak8963c_selftest(dev_get_drvdata(dev), sf);
  418. return snprintf(buf, PAGE_SIZE, "%d,%d,%d,%d\n",
  419. ret, sf[0], sf[1], sf[2]);
  420. }
  421. static ssize_t ak8963c_check_registers(struct device *dev,
  422. struct device_attribute *attr, char *buf)
  423. {
  424. u8 temp[13], reg;
  425. struct ak8963c_p *data = dev_get_drvdata(dev);
  426. mutex_lock(&data->lock);
  427. /* power down */
  428. reg = AK8963C_CNTL1_POWER_DOWN;
  429. ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  430. /* get the value */
  431. ak8963c_i2c_read_block(data->client, AK8963C_REG_WIA, temp, 11);
  432. ak8963c_i2c_read(data->client, AK8963C_REG_ASTC, &temp[11]);
  433. ak8963c_i2c_read(data->client, AK8963C_REG_I2CDIS, &temp[12]);
  434. mutex_unlock(&data->lock);
  435. return snprintf(buf, PAGE_SIZE,
  436. "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
  437. temp[0], temp[1], temp[2], temp[3], temp[4], temp[5],
  438. temp[6], temp[7], temp[8], temp[9], temp[10], temp[11],
  439. temp[12]);
  440. }
  441. static ssize_t ak8963c_check_cntl(struct device *dev,
  442. struct device_attribute *attr, char *buf)
  443. {
  444. u8 reg;
  445. int ret = 0;
  446. struct ak8963c_p *data = dev_get_drvdata(dev);
  447. mutex_lock(&data->lock);
  448. /* power down */
  449. reg = AK8963C_CNTL1_POWER_DOWN;
  450. ret = ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  451. ret += ak8963c_i2c_read(data->client, AK8963C_REG_CNTL1, &reg);
  452. mutex_unlock(&data->lock);
  453. return snprintf(buf, PAGE_SIZE, "%s\n",
  454. (((reg == AK8963C_CNTL1_POWER_DOWN) &&
  455. (ret == 0)) ? "OK" : "NG"));
  456. }
  457. static ssize_t ak8963c_get_status(struct device *dev,
  458. struct device_attribute *attr, char *buf)
  459. {
  460. bool success;
  461. struct ak8963c_p *data = dev_get_drvdata(dev);
  462. if ((data->asa[0] == 0) | (data->asa[0] == 0xff)
  463. | (data->asa[1] == 0) | (data->asa[1] == 0xff)
  464. | (data->asa[2] == 0) | (data->asa[2] == 0xff))
  465. success = false;
  466. else
  467. success = true;
  468. return snprintf(buf, PAGE_SIZE, "%s\n", (success ? "OK" : "NG"));
  469. }
  470. static ssize_t ak8963c_adc(struct device *dev,
  471. struct device_attribute *attr, char *buf)
  472. {
  473. bool success = false;
  474. u8 temp[SENSOR_DATA_SIZE] = {0,};
  475. int ret, retry = 0;
  476. struct ak8963c_p *data = dev_get_drvdata(dev);
  477. struct ak8963c_v mag = data->magdata;
  478. if (atomic_read(&data->enable) == 1) {
  479. success = true;
  480. goto exit;
  481. }
  482. msleep(20);
  483. retry_adc:
  484. mutex_lock(&data->lock);
  485. ret = ak8963c_ecs_set_mode(data, AK8963C_CNTL1_SNG_MEASURE);
  486. if (ret) {
  487. mutex_unlock(&data->lock);
  488. goto exit;
  489. }
  490. ret = ak8963c_wait_for_data_ready(data);
  491. if (ret) {
  492. mutex_unlock(&data->lock);
  493. goto exit;
  494. }
  495. ret = ak8963c_i2c_read_block(data->client, AK8963C_REG_ST1,
  496. temp, SENSOR_DATA_SIZE);
  497. if (ret < 0) {
  498. pr_err("[SENSOR] %s: failed to read mag data\n", __func__);
  499. mutex_unlock(&data->lock);
  500. goto exit;
  501. }
  502. ak8963c_ecs_set_mode(data, AK8963C_CNTL1_POWER_DOWN);
  503. mutex_unlock(&data->lock);
  504. /* buf[0] is status1, buf[7] is status2 */
  505. if ((temp[0] == 0) | (temp[7] == 1)) {
  506. if (retry++ < 3)
  507. goto retry_adc;
  508. success = false;
  509. }
  510. else
  511. success = true;
  512. mag.x = (temp[2] << 8) + temp[1];
  513. mag.y = (temp[4] << 8) + temp[3];
  514. mag.z = (temp[6] << 8) + temp[5];
  515. pr_info("[SENSOR]: %s - ST1=%d, x=%d, y=%d, z=%d, ST2=%d\n",
  516. __func__, temp[0], mag.x, mag.y, mag.z, temp[7]);
  517. exit:
  518. return snprintf(buf, PAGE_SIZE, "%s,%d,%d,%d\n",
  519. (success ? "OK" : "NG"), mag.x, mag.y, mag.z);
  520. }
  521. static ssize_t ak8963c_raw_data_read(struct device *dev,
  522. struct device_attribute *attr, char *buf)
  523. {
  524. struct ak8963c_p *data = dev_get_drvdata(dev);
  525. struct ak8963c_v mag = data->magdata;
  526. int ret, retry = 0;
  527. u8 temp[SENSOR_DATA_SIZE] = {0,};
  528. if (atomic_read(&data->enable) == 1)
  529. goto exit;
  530. msleep(20);
  531. retry_rawdata:
  532. mutex_lock(&data->lock);
  533. ret = ak8963c_ecs_set_mode(data, AK8963C_CNTL1_SNG_MEASURE);
  534. if (ret) {
  535. mutex_unlock(&data->lock);
  536. goto exit;
  537. }
  538. ret = ak8963c_wait_for_data_ready(data);
  539. if (ret) {
  540. mutex_unlock(&data->lock);
  541. goto exit;
  542. }
  543. ret = ak8963c_i2c_read_block(data->client, AK8963C_REG_ST1,
  544. temp, SENSOR_DATA_SIZE);
  545. if (ret < 0) {
  546. pr_err("[SENSOR] %s: failed to read mag data\n", __func__);
  547. mutex_unlock(&data->lock);
  548. goto exit;
  549. }
  550. ak8963c_ecs_set_mode(data, AK8963C_CNTL1_POWER_DOWN);
  551. mutex_unlock(&data->lock);
  552. if (temp[0] & 0x01) {
  553. mag.x = (temp[2] << 8) + temp[1];
  554. mag.y = (temp[4] << 8) + temp[3];
  555. mag.z = (temp[6] << 8) + temp[5];
  556. } else {
  557. pr_err("[SENSOR]: %s - invalid raw data(st1 = %d)\n",
  558. __func__, temp[0] & 0x01);
  559. pr_info("[SENSOR]: %s - ST1=%d, x=%d, y=%d, z=%d, ST2=%d\n",
  560. __func__, temp[0], mag.x, mag.y, mag.z, temp[7]);
  561. if (retry++ < 3)
  562. goto retry_rawdata;
  563. }
  564. exit:
  565. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", mag.x, mag.y, mag.z);
  566. }
  567. static DEVICE_ATTR(name, S_IRUGO, ak8963c_name_show, NULL);
  568. static DEVICE_ATTR(vendor, S_IRUGO, ak8963c_vendor_show, NULL);
  569. static DEVICE_ATTR(raw_data, S_IRUGO, ak8963c_raw_data_read, NULL);
  570. static DEVICE_ATTR(adc, S_IRUGO, ak8963c_adc, NULL);
  571. static DEVICE_ATTR(dac, S_IRUGO, ak8963c_check_cntl, NULL);
  572. static DEVICE_ATTR(chk_registers, S_IRUGO, ak8963c_check_registers, NULL);
  573. static DEVICE_ATTR(selftest, S_IRUGO, ak8963c_get_selftest, NULL);
  574. static DEVICE_ATTR(asa, S_IRUGO, ak8963c_get_asa, NULL);
  575. static DEVICE_ATTR(status, S_IRUGO, ak8963c_get_status, NULL);
  576. static struct device_attribute *sensor_attrs[] = {
  577. &dev_attr_name,
  578. &dev_attr_vendor,
  579. &dev_attr_raw_data,
  580. &dev_attr_adc,
  581. &dev_attr_dac,
  582. &dev_attr_chk_registers,
  583. &dev_attr_selftest,
  584. &dev_attr_asa,
  585. &dev_attr_status,
  586. NULL,
  587. };
  588. static int ak8963c_read_fuserom(struct ak8963c_p *data)
  589. {
  590. unsigned char reg;
  591. int ret;
  592. /* put into fuse access mode to read asa data */
  593. reg = AK8963C_CNTL1_FUSE_ACCESS;
  594. ret = ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  595. if (ret < 0) {
  596. pr_err("[SENSOR]: %s - unable to enter fuse rom mode\n",
  597. __func__);
  598. goto exit_default_value;
  599. }
  600. ret = ak8963c_i2c_read_block(data->client, AK8963C_FUSE_ASAX,
  601. data->asa, sizeof(data->asa));
  602. if (ret < 0) {
  603. pr_err("[SENSOR]: %s - unable to load factory sensitivity "\
  604. "adjust values\n", __func__);
  605. goto exit_default_value;
  606. } else
  607. pr_info("[SENSOR]: %s - asa_x = %u, asa_y = %u, asa_z = %u\n",
  608. __func__, data->asa[0], data->asa[1], data->asa[2]);
  609. reg = AK8963C_CNTL1_POWER_DOWN;
  610. ret = ak8963c_i2c_write(data->client, AK8963C_REG_CNTL1, reg);
  611. if (ret < 0)
  612. pr_err("[SENSOR] Error in setting power down mode\n");
  613. return 0;
  614. exit_default_value:
  615. data->asa[0] = 0;
  616. data->asa[1] = 0;
  617. data->asa[2] = 0;
  618. return ret;
  619. }
  620. static int ak8963c_setup_pin(struct ak8963c_p *data)
  621. {
  622. int ret, irq;
  623. ret = gpio_request(data->m_rst_n, "M_RST_N");
  624. if (ret < 0) {
  625. pr_err("[SENSOR] %s - gpio %d request failed (%d)\n",
  626. __func__, data->m_rst_n, ret);
  627. goto exit;
  628. }
  629. ret = gpio_direction_output(data->m_rst_n, 1);
  630. if (ret < 0) {
  631. pr_err("[SENSOR]: %s - failed to set gpio %d as input (%d)\n",
  632. __func__, data->m_rst_n, ret);
  633. goto exit_reset_gpio;
  634. }
  635. gpio_set_value(data->m_rst_n, 0);
  636. udelay(10);
  637. gpio_set_value(data->m_rst_n, 1);
  638. udelay(100);
  639. ret = gpio_request(data->m_sensor_int, "M_SENSOR_INT");
  640. if (ret < 0) {
  641. pr_err("[SENSOR]: %s - gpio %d request failed (%d)\n",
  642. __func__, data->m_sensor_int, ret);
  643. goto exit_reset_gpio;
  644. }
  645. ret = gpio_direction_input(data->m_sensor_int);
  646. if (ret < 0) {
  647. pr_err("[SENSOR]: %s - failed to set gpio %d as input (%d)\n",
  648. __func__, data->m_sensor_int, ret);
  649. goto exit_int_gpio;
  650. }
  651. irq = gpio_to_irq(data->m_sensor_int);
  652. /* trigger high so we don't miss initial interrupt if it
  653. * is already pending
  654. */
  655. ret = request_irq(irq, ak8963c_irq_handler,
  656. IRQF_TRIGGER_RISING | IRQF_ONESHOT, "ak8963c_int", data);
  657. if (ret < 0) {
  658. pr_err("[SENSOR]: %s - request_irq(%d) fail for gpio %d (%d)\n",
  659. __func__, irq,
  660. data->m_sensor_int, ret);
  661. goto exit_int_gpio;
  662. }
  663. /* start with interrupt disabled until the driver is enabled */
  664. data->irq = irq;
  665. ak8963c_disable_irq(data);
  666. goto exit;
  667. exit_int_gpio:
  668. gpio_free(data->m_sensor_int);
  669. exit_reset_gpio:
  670. gpio_free(data->m_rst_n);
  671. exit:
  672. return ret;
  673. }
  674. static int ak8963c_input_init(struct ak8963c_p *data)
  675. {
  676. int ret = 0;
  677. struct input_dev *dev;
  678. dev = input_allocate_device();
  679. if (!dev)
  680. return -ENOMEM;
  681. dev->name = MODULE_NAME;
  682. dev->id.bustype = BUS_I2C;
  683. input_set_capability(dev, EV_REL, REL_X);
  684. input_set_capability(dev, EV_REL, REL_Y);
  685. input_set_capability(dev, EV_REL, REL_Z);
  686. input_set_drvdata(dev, data);
  687. ret = input_register_device(dev);
  688. if (ret < 0) {
  689. input_free_device(dev);
  690. return ret;
  691. }
  692. ret = sensors_create_symlink(&dev->dev.kobj, dev->name);
  693. if (ret < 0) {
  694. input_unregister_device(dev);
  695. return ret;
  696. }
  697. /* sysfs node creation */
  698. ret = sysfs_create_group(&dev->dev.kobj, &ak8963c_attribute_group);
  699. if (ret < 0) {
  700. sensors_remove_symlink(&data->input->dev.kobj,
  701. data->input->name);
  702. input_unregister_device(dev);
  703. return ret;
  704. }
  705. data->input = dev;
  706. return 0;
  707. }
  708. static int ak8963c_parse_dt(struct ak8963c_p *data, struct device *dev)
  709. {
  710. struct device_node *dNode = dev->of_node;
  711. enum of_gpio_flags flags;
  712. if (dNode == NULL)
  713. return -ENODEV;
  714. data->m_sensor_int = of_get_named_gpio_flags(dNode,
  715. "ak8963c-i2c,m_sensor_int-gpio", 0, &flags);
  716. if (data->m_sensor_int < 0) {
  717. pr_err("[SENSOR]: %s - get m_sensor_int error\n", __func__);
  718. return -ENODEV;
  719. }
  720. data->m_rst_n = of_get_named_gpio_flags(dNode,
  721. "ak8963c-i2c,m_rst_n-gpio", 0, &flags);
  722. if (data->m_rst_n < 0) {
  723. pr_err("[SENSOR]: %s - m_rst_n error\n", __func__);
  724. return -ENODEV;
  725. }
  726. if (of_property_read_u32(dNode,
  727. "ak8963c-i2c,chip_pos", &data->chip_pos) < 0)
  728. data->chip_pos = AK8963C_TOP_LOWER_RIGHT;
  729. return 0;
  730. }
  731. static int ak8963c_probe(struct i2c_client *client,
  732. const struct i2c_device_id *id)
  733. {
  734. int ret = -ENODEV;
  735. struct ak8963c_p *data = NULL;
  736. pr_info("[SENSOR]: %s - Probe Start!\n", __func__);
  737. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  738. pr_err("[SENSOR]: %s - i2c_check_functionality error\n",
  739. __func__);
  740. goto exit;
  741. }
  742. data = kzalloc(sizeof(struct ak8963c_p), GFP_KERNEL);
  743. if (data == NULL) {
  744. pr_err("[SENSOR]: %s - kzalloc error\n", __func__);
  745. ret = -ENOMEM;
  746. goto exit_kzalloc;
  747. }
  748. ret = ak8963c_parse_dt(data, &client->dev);
  749. if (ret < 0) {
  750. pr_err("[SENSOR]: %s - of_node error\n", __func__);
  751. ret = -ENODEV;
  752. goto exit_of_node;
  753. }
  754. init_completion(&data->data_ready);
  755. ret = ak8963c_setup_pin(data);
  756. if (ret) {
  757. pr_err("[SENSOR]: %s - could not setup pin\n", __func__);
  758. goto exit_setup_pin;
  759. }
  760. i2c_set_clientdata(client, data);
  761. data->client = client;
  762. ret = ak8963c_ecs_set_mode_power_down(data);
  763. if (ret < 0) {
  764. pr_err("[SENSOR]: %s - ak8963c_ecs_set_mode_power_down fail"\
  765. "(err=%d)\n", __func__, ret);
  766. goto exit_set_mode_power_down;
  767. }
  768. /* input device init */
  769. ret = ak8963c_input_init(data);
  770. if (ret < 0)
  771. goto exit_input_init;
  772. sensors_register(data->factory_device, data, sensor_attrs, MODULE_NAME);
  773. /* workqueue init */
  774. INIT_DELAYED_WORK(&data->work, ak8963c_work_func);
  775. mutex_init(&data->lock);
  776. init_waitqueue_head(&data->state_wq);
  777. atomic_set(&data->delay, AK8963C_DEFAULT_DELAY);
  778. atomic_set(&data->enable, 0);
  779. ak8963c_read_fuserom(data);
  780. pr_info("[SENSOR]: %s - Probe done!(chip pos : %d)\n",
  781. __func__, data->chip_pos);
  782. return 0;
  783. exit_input_init:
  784. exit_set_mode_power_down:
  785. free_irq(data->irq, data);
  786. gpio_free(data->m_rst_n);
  787. gpio_free(data->m_sensor_int);
  788. exit_setup_pin:
  789. exit_of_node:
  790. kfree(data);
  791. exit_kzalloc:
  792. exit:
  793. pr_err("[SENSOR]: %s - Probe fail!\n", __func__);
  794. return ret;
  795. }
  796. static void ak8963c_shutdown(struct i2c_client *client)
  797. {
  798. struct ak8963c_p *data = (struct ak8963c_p *)i2c_get_clientdata(client);
  799. pr_info("[SENSOR]: %s\n", __func__);
  800. if (atomic_read(&data->enable) == 1) {
  801. ak8963c_ecs_set_mode(data, AK8963C_CNTL1_POWER_DOWN);
  802. cancel_delayed_work_sync(&data->work);
  803. }
  804. }
  805. static int __devexit ak8963c_remove(struct i2c_client *client)
  806. {
  807. struct ak8963c_p *data = (struct ak8963c_p *)i2c_get_clientdata(client);
  808. if (atomic_read(&data->enable) == 1)
  809. ak8963c_set_enable(data, 0);
  810. free_irq(data->irq, data);
  811. gpio_free(data->m_rst_n);
  812. gpio_free(data->m_sensor_int);
  813. mutex_destroy(&data->lock);
  814. sensors_unregister(data->factory_device, sensor_attrs);
  815. sensors_remove_symlink(&data->input->dev.kobj, data->input->name);
  816. sysfs_remove_group(&data->input->dev.kobj, &ak8963c_attribute_group);
  817. input_unregister_device(data->input);
  818. kfree(data);
  819. return 0;
  820. }
  821. static int ak8963c_suspend(struct device *dev)
  822. {
  823. struct ak8963c_p *data = dev_get_drvdata(dev);
  824. if (atomic_read(&data->enable) == 1) {
  825. ak8963c_ecs_set_mode(data, AK8963C_CNTL1_POWER_DOWN);
  826. cancel_delayed_work_sync(&data->work);
  827. }
  828. return 0;
  829. }
  830. static int ak8963c_resume(struct device *dev)
  831. {
  832. struct ak8963c_p *data = dev_get_drvdata(dev);
  833. if (atomic_read(&data->enable) == 1) {
  834. ak8963c_ecs_set_mode(data, AK8963C_CNTL1_SNG_MEASURE);
  835. schedule_delayed_work(&data->work,
  836. nsecs_to_jiffies(atomic_read(&data->delay)));
  837. }
  838. return 0;
  839. }
  840. static struct of_device_id ak8963c_match_table[] = {
  841. { .compatible = "ak8963c-i2c",},
  842. {},
  843. };
  844. static const struct i2c_device_id ak8963c_id[] = {
  845. { "ak8963c_match_table", 0 },
  846. { }
  847. };
  848. static const struct dev_pm_ops ak8963c_pm_ops = {
  849. .suspend = ak8963c_suspend,
  850. .resume = ak8963c_resume
  851. };
  852. static struct i2c_driver ak8963c_driver = {
  853. .driver = {
  854. .name = MODEL_NAME,
  855. .owner = THIS_MODULE,
  856. .of_match_table = ak8963c_match_table,
  857. .pm = &ak8963c_pm_ops
  858. },
  859. .probe = ak8963c_probe,
  860. .shutdown = ak8963c_shutdown,
  861. .remove = __devexit_p(ak8963c_remove),
  862. .id_table = ak8963c_id,
  863. };
  864. static int __init ak8963c_init(void)
  865. {
  866. return i2c_add_driver(&ak8963c_driver);
  867. }
  868. static void __exit ak8963c_exit(void)
  869. {
  870. i2c_del_driver(&ak8963c_driver);
  871. }
  872. module_init(ak8963c_init);
  873. module_exit(ak8963c_exit);
  874. MODULE_DESCRIPTION("AKM8963 compass driver");
  875. MODULE_AUTHOR("Samsung Electronics");
  876. MODULE_LICENSE("GPL");