yas53x_kernel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * Copyright (c) 2013 Yamaha Corporation
  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/types.h>
  20. #include <linux/i2c.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/input.h>
  27. #include <linux/mutex.h>
  28. #include <linux/delay.h>
  29. #include <linux/poll.h>
  30. #include <linux/slab.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/kernel.h>
  34. #include <linux/of_gpio.h>
  35. #include "yas53x_drv.c"
  36. #include "../sensors_core.h"
  37. #define MODULE_NAME "magnetic_sensor"
  38. #define VENDOR_NAME "YAMAHA"
  39. #define CHIP_NAME "yas532"
  40. #define YAS53X_MIN_DELAY (200) /* msec */
  41. #define YAS53X_LOG_TIME 10000
  42. struct yas53x_data {
  43. struct i2c_client *client;
  44. struct input_dev *input;
  45. struct device *factory_device;
  46. struct delayed_work work;
  47. struct mutex mutex, enable_mutex;
  48. int8_t hard_offset[3];
  49. int32_t delay;
  50. atomic_t enable;
  51. int count_logtime;
  52. };
  53. static struct i2c_client *this_client;
  54. static int yas53x_i2c_open(void)
  55. {
  56. pr_info("[SENSOR] %s\n", __func__);
  57. return 0;
  58. }
  59. static int yas53x_i2c_close(void)
  60. {
  61. pr_info("[SENSOR] %s\n", __func__);
  62. return 0;
  63. }
  64. static int yas53x_i2c_write(uint8_t addr, const uint8_t *buf, int len)
  65. {
  66. uint8_t tmp[16];
  67. if (sizeof(tmp) - 1 < len)
  68. return -1;
  69. tmp[0] = addr;
  70. memcpy(&tmp[1], buf, len);
  71. if (i2c_master_send(this_client, tmp, len + 1) < 0)
  72. return -1;
  73. return 0;
  74. }
  75. static int yas53x_i2c_read(uint8_t addr, uint8_t *buf, int len)
  76. {
  77. struct i2c_msg msg[2];
  78. int err;
  79. msg[0].addr = this_client->addr;
  80. msg[0].flags = 0;
  81. msg[0].len = 1;
  82. msg[0].buf = &addr;
  83. msg[1].addr = this_client->addr;
  84. msg[1].flags = I2C_M_RD;
  85. msg[1].len = len;
  86. msg[1].buf = buf;
  87. err = i2c_transfer(this_client->adapter, msg, 2);
  88. if (err != 2) {
  89. dev_err(&this_client->dev,
  90. "i2c_transfer() read error: "
  91. "slave_addr=%02x, reg_addr=%02x, err=%d\n",
  92. this_client->addr, addr, err);
  93. return err;
  94. }
  95. return 0;
  96. }
  97. static void yas53x_msleep(int ms)
  98. {
  99. usleep_range(ms * 1000, (ms + 1) * 1000);
  100. }
  101. static void yas53x_current_time(uint32_t *msec)
  102. {
  103. *msec = jiffies_to_msecs(jiffies);
  104. }
  105. static struct yas_mag_driver this_drv = {
  106. .callback = {
  107. .device_open = yas53x_i2c_open,
  108. .device_close = yas53x_i2c_close,
  109. .device_write = yas53x_i2c_write,
  110. .device_read = yas53x_i2c_read,
  111. .msleep = yas53x_msleep,
  112. .current_time = yas53x_current_time,
  113. },
  114. };
  115. static void yas53x_input_work_func(struct work_struct *work)
  116. {
  117. struct yas53x_data *data
  118. = container_of((struct delayed_work *)work,
  119. struct yas53x_data, work);
  120. struct yas_mag_data mag;
  121. int32_t delay;
  122. uint32_t time_before, time_after;
  123. int rt = 0;
  124. mutex_lock(&data->mutex);
  125. yas53x_current_time(&time_before);
  126. rt = this_drv.measure(&mag);
  127. if (rt & YAS_REPORT_HARD_OFFSET_CHANGED)
  128. this_drv.get_offset(data->hard_offset);
  129. yas53x_current_time(&time_after);
  130. delay = data->delay - (time_after - time_before);
  131. mutex_unlock(&data->mutex);
  132. /* report magnetic data in [nT] */
  133. input_report_rel(data->input, REL_X, mag.xyz.v[0]);
  134. input_report_rel(data->input, REL_Y, mag.xyz.v[1]);
  135. input_report_rel(data->input, REL_Z, mag.xyz.v[2]);
  136. input_sync(data->input);
  137. if (data->delay * data->count_logtime > YAS53X_LOG_TIME) {
  138. pr_info("[SENSOR] %s, %d, %d, %d\n",
  139. __func__, mag.xyz.v[0], mag.xyz.v[1], mag.xyz.v[2]);
  140. data->count_logtime = 0;
  141. } else
  142. data->count_logtime++;
  143. if (delay <= 0)
  144. delay = 1;
  145. schedule_delayed_work(&data->work, msecs_to_jiffies(delay));
  146. }
  147. static int yas53x_enable(struct yas53x_data *data)
  148. {
  149. pr_info("[SENSOR] %s\n", __func__);
  150. if (!atomic_cmpxchg(&data->enable, 0, 1))
  151. schedule_delayed_work(&data->work, 0);
  152. return 0;
  153. }
  154. static int yas53x_disable(struct yas53x_data *data)
  155. {
  156. pr_info("[SENSOR] %s\n", __func__);
  157. if (atomic_cmpxchg(&data->enable, 1, 0))
  158. cancel_delayed_work_sync(&data->work);
  159. return 0;
  160. }
  161. /* Input sysfs interface */
  162. static ssize_t yas53x_delay_show(struct device *dev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. struct yas53x_data *data = dev_get_drvdata(dev);
  166. int32_t delay;
  167. mutex_lock(&data->mutex);
  168. delay = data->delay;
  169. mutex_unlock(&data->mutex);
  170. pr_info("[SENSOR] %s, delay = %d\n", __func__, delay);
  171. return sprintf(buf, "%d\n", delay);
  172. }
  173. static ssize_t yas53x_delay_store(struct device *dev,
  174. struct device_attribute *attr, const char *buf, size_t count)
  175. {
  176. struct yas53x_data *data = dev_get_drvdata(dev);
  177. long value;
  178. if (kstrtol(buf, 10, &value) < 0)
  179. return -EINVAL;
  180. value /= 1000000;
  181. if (value < 0)
  182. value = 0;
  183. if (YAS53X_MIN_DELAY < value)
  184. value = YAS53X_MIN_DELAY;
  185. mutex_lock(&data->mutex);
  186. data->delay = value;
  187. mutex_unlock(&data->mutex);
  188. pr_info("[SENSOR] %s, delay = %d\n", __func__, data->delay);
  189. return count;
  190. }
  191. static ssize_t yas53x_enable_show(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. struct yas53x_data *data = dev_get_drvdata(dev);
  195. return sprintf(buf, "%d\n", atomic_read(&data->enable));
  196. }
  197. static ssize_t yas53x_enable_store(struct device *dev,
  198. struct device_attribute *attr, const char *buf, size_t count)
  199. {
  200. struct yas53x_data *data = dev_get_drvdata(dev);
  201. int value;
  202. if (kstrtoint(buf, 10, &value) < 0)
  203. return -EINVAL;
  204. pr_info("[SENSOR] %s: enable = %d\n", __func__, value);
  205. if (value)
  206. yas53x_enable(data);
  207. else
  208. yas53x_disable(data);
  209. return count;
  210. }
  211. static DEVICE_ATTR(poll_delay, S_IRUGO|S_IWUSR|S_IWGRP,
  212. yas53x_delay_show, yas53x_delay_store);
  213. static DEVICE_ATTR(enable, S_IRUGO|S_IWUSR|S_IWGRP,
  214. yas53x_enable_show, yas53x_enable_store);
  215. static struct attribute *yas53x_attributes[] = {
  216. &dev_attr_poll_delay.attr,
  217. &dev_attr_enable.attr,
  218. NULL
  219. };
  220. static struct attribute_group yas53x_attribute_group = {
  221. .attrs = yas53x_attributes
  222. };
  223. /* sensors sysfs interface */
  224. static ssize_t yas53x_self_test_show(struct device *dev,
  225. struct device_attribute *attr, char *buf)
  226. {
  227. struct yas53x_data *data = dev_get_drvdata(dev);
  228. struct yas_self_test_result r;
  229. int rt;
  230. s8 err[7] = { 0, };
  231. mutex_lock(&data->mutex);
  232. rt = this_drv.self_test(&r);
  233. mutex_unlock(&data->mutex);
  234. if (unlikely(r.id != 0x2))
  235. err[0] = -1;
  236. if (unlikely(r.xy1y2[0] < -30 || r.xy1y2[0] > 30))
  237. err[3] = -1;
  238. if (unlikely(r.xy1y2[1] < -30 || r.xy1y2[1] > 30))
  239. err[3] = -1;
  240. if (unlikely(r.xy1y2[2] < -30 || r.xy1y2[2] > 30))
  241. err[3] = -1;
  242. if (unlikely(r.sx < 17 || r.sy < 22))
  243. err[5] = -1;
  244. if (unlikely(r.xyz[0] < -600 || r.xyz[0] > 600))
  245. err[6] = -1;
  246. if (unlikely(r.xyz[1] < -600 || r.xyz[1] > 600))
  247. err[6] = -1;
  248. if (unlikely(r.xyz[2] < -600 || r.xyz[2] > 600))
  249. err[6] = -1;
  250. pr_info("[SENSOR] %s\n"
  251. "[SENSOR] Test1 - err = %d, id = %d\n"
  252. "[SENSOR] Test3 - err = %d\n"
  253. "[SENSOR] Test4 - err = %d, offset = %d,%d,%d\n"
  254. "[SENSOR] Test5 - err = %d, direction = %d\n"
  255. "[SENSOR] Test6 - err = %d, sensitivity = %d,%d\n"
  256. "[SENSOR] Test7 - err = %d, offset = %d,%d,%d\n"
  257. "[SENSOR] Test2 - err = %d\n", __func__,
  258. err[0], r.id, err[2], err[3], r.xy1y2[0], r.xy1y2[1],
  259. r.xy1y2[2], err[4], r.dir, err[5], r.sx, r.sy,
  260. err[6], r.xyz[0], r.xyz[1], r.xyz[2], err[1]);
  261. return sprintf(buf,
  262. "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
  263. err[0], r.id, err[2], err[3], r.xy1y2[0],
  264. r.xy1y2[1], r.xy1y2[2], err[4], r.dir,
  265. err[5], r.sx, r.sy, err[6],
  266. r.xyz[0], r.xyz[1], r.xyz[2], err[1]);
  267. }
  268. static ssize_t yas53x_self_test_noise_show(struct device *dev,
  269. struct device_attribute *attr, char *buf)
  270. {
  271. struct yas53x_data *data = dev_get_drvdata(dev);
  272. struct yas_vector raw_xyz;
  273. int rt;
  274. mutex_lock(&data->mutex);
  275. rt = this_drv.self_test_noise(&raw_xyz);
  276. mutex_unlock(&data->mutex);
  277. return sprintf(buf, "%d,%d,%d\n", raw_xyz.v[0], raw_xyz.v[1],
  278. raw_xyz.v[2]);
  279. }
  280. static ssize_t yas53x_self_test_noise_store(struct device *dev,
  281. struct device_attribute *attr, const char *buf, size_t size)
  282. {
  283. pr_info("[SENSOR] %s\n", __func__);
  284. return size;
  285. }
  286. static ssize_t yas53x_vendor_show(struct device *dev,
  287. struct device_attribute *attr, char *buf)
  288. {
  289. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
  290. }
  291. static ssize_t yas53x_name_show(struct device *dev,
  292. struct device_attribute *attr, char *buf)
  293. {
  294. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_NAME);
  295. }
  296. static DEVICE_ATTR(name, S_IRUGO, yas53x_name_show, NULL);
  297. static DEVICE_ATTR(vendor, S_IRUGO, yas53x_vendor_show, NULL);
  298. static DEVICE_ATTR(raw_data, S_IRUGO | S_IWUSR | S_IWGRP,
  299. yas53x_self_test_noise_show, yas53x_self_test_noise_store);
  300. static DEVICE_ATTR(selftest, S_IRUGO, yas53x_self_test_show, NULL);
  301. static struct device_attribute *sensor_attrs[] = {
  302. &dev_attr_name,
  303. &dev_attr_vendor,
  304. &dev_attr_raw_data,
  305. &dev_attr_selftest,
  306. NULL,
  307. };
  308. static int yas53x_input_init(struct yas53x_data *data)
  309. {
  310. int ret = 0;
  311. struct input_dev *dev;
  312. dev = input_allocate_device();
  313. if (!dev)
  314. return -ENOMEM;
  315. dev->name = MODULE_NAME;
  316. dev->id.bustype = BUS_I2C;
  317. input_set_capability(dev, EV_REL, REL_X);
  318. input_set_capability(dev, EV_REL, REL_Y);
  319. input_set_capability(dev, EV_REL, REL_Z);
  320. input_set_drvdata(dev, data);
  321. ret = input_register_device(dev);
  322. if (ret < 0) {
  323. input_free_device(dev);
  324. return ret;
  325. }
  326. ret = sensors_create_symlink(&dev->dev.kobj, dev->name);
  327. if (ret < 0) {
  328. input_unregister_device(dev);
  329. return ret;
  330. }
  331. /* sysfs node creation */
  332. ret = sysfs_create_group(&dev->dev.kobj, &yas53x_attribute_group);
  333. if (ret < 0) {
  334. sensors_remove_symlink(&data->input->dev.kobj,
  335. data->input->name);
  336. input_unregister_device(dev);
  337. return ret;
  338. }
  339. data->input = dev;
  340. pr_info("[SENSOR] %s\n", __func__);
  341. return 0;
  342. }
  343. static int yas53x_probe(struct i2c_client *client, const struct i2c_device_id *id)
  344. {
  345. struct yas53x_data *data = NULL;
  346. int ret = -ENODEV;
  347. pr_info("[SENSOR]: %s - Probe Start!\n", __func__);
  348. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  349. pr_err("[SENSOR]: %s - i2c_check_functionality error\n",
  350. __func__);
  351. goto exit;
  352. }
  353. data = kzalloc(sizeof(struct yas53x_data), GFP_KERNEL);
  354. if (data == NULL) {
  355. pr_err("[SENSOR]: %s - kzalloc error\n", __func__);
  356. ret = -ENOMEM;
  357. goto exit_kzalloc;
  358. }
  359. i2c_set_clientdata(client, data);
  360. data->client = client;
  361. ret = yas53x_input_init(data);
  362. if (ret < 0)
  363. goto exit_input_init;
  364. sensors_register(data->factory_device, data, sensor_attrs, MODULE_NAME);
  365. /* workqueue init */
  366. INIT_DELAYED_WORK(&data->work, yas53x_input_work_func);
  367. mutex_init(&data->mutex);
  368. mutex_init(&data->enable_mutex);
  369. atomic_set(&data->enable, 0);
  370. data->delay = YAS53X_MIN_DELAY;
  371. this_client = client;
  372. ret = yas_mag_driver_init(&this_drv);
  373. if (ret < 0) {
  374. printk(KERN_ERR "yas_mag_driver_init failed[%d]\n", ret);
  375. goto err;
  376. }
  377. ret = this_drv.init();
  378. if (ret < 0) {
  379. printk(KERN_ERR "this_drv.init() failed[%d]\n", ret);
  380. goto err;
  381. }
  382. this_drv.set_position(CONFIG_INPUT_YAS_MAGNETOMETER_POSITION);
  383. this_drv.get_offset(data->hard_offset);
  384. pr_info("[SENSOR]: %s - Probe done!\n", __func__);
  385. return 0;
  386. err:
  387. if (data != NULL) {
  388. if (data->input != NULL) {
  389. mutex_destroy(&data->mutex);
  390. mutex_destroy(&data->enable_mutex);
  391. sensors_unregister(data->factory_device, sensor_attrs);
  392. sysfs_remove_group(&data->input->dev.kobj,
  393. &yas53x_attribute_group);
  394. sensors_remove_symlink(&data->input->dev.kobj,
  395. data->input->name);
  396. input_unregister_device(data->input);
  397. }
  398. kfree(data);
  399. }
  400. return ret;
  401. exit_input_init:
  402. kfree(data);
  403. exit_kzalloc:
  404. exit:
  405. pr_err("[SENSOR]: %s - Probe fail!\n", __func__);
  406. return ret;
  407. }
  408. static int yas53x_remove(struct i2c_client *client)
  409. {
  410. struct yas53x_data *data = i2c_get_clientdata(client);
  411. if (data != NULL) {
  412. yas53x_disable(data);
  413. this_drv.term();
  414. sysfs_remove_group(&data->input->dev.kobj,
  415. &yas53x_attribute_group);
  416. input_unregister_device(data->input);
  417. kfree(data);
  418. }
  419. pr_info("[SENSOR] %s\n", __func__);
  420. return 0;
  421. }
  422. static int yas53x_suspend(struct device *dev)
  423. {
  424. struct yas53x_data *data = dev_get_drvdata(dev);
  425. if (atomic_read(&data->enable))
  426. cancel_delayed_work_sync(&data->work);
  427. pr_info("[SENSOR] %s\n", __func__);
  428. return 0;
  429. }
  430. static int yas53x_resume(struct device *dev)
  431. {
  432. struct yas53x_data *data = dev_get_drvdata(dev);
  433. if (atomic_read(&data->enable))
  434. schedule_delayed_work(&data->work, 0);
  435. pr_info("[SENSOR] %s\n", __func__);
  436. return 0;
  437. }
  438. static struct of_device_id yas53x_match_table[] = {
  439. { .compatible = "yamaha,yas532",},
  440. {},
  441. };
  442. /*
  443. static struct i2c_device_id yas53x_id[] = {
  444. { "yas53x_match_table", 0},
  445. {}
  446. };
  447. */
  448. static struct i2c_device_id yas53x_id[] = {
  449. {"yas532", 0},
  450. {}
  451. };
  452. MODULE_DEVICE_TABLE(i2c, yas53x_id);
  453. static const struct dev_pm_ops yas53x_pm_ops = {
  454. .suspend = yas53x_suspend,
  455. .resume = yas53x_resume
  456. };
  457. static struct i2c_driver yas53x_i2c_driver = {
  458. .driver = {
  459. .name = CHIP_NAME,
  460. .owner = THIS_MODULE,
  461. .of_match_table = yas53x_match_table,
  462. .pm = &yas53x_pm_ops
  463. },
  464. .id_table = yas53x_id,
  465. .probe = yas53x_probe,
  466. .remove = yas53x_remove,
  467. };
  468. static int __init yas53x_init(void)
  469. {
  470. pr_info("[SENSOR] %s\n", __func__);
  471. return i2c_add_driver(&yas53x_i2c_driver);
  472. }
  473. static void __exit yas53x_term(void)
  474. {
  475. i2c_del_driver(&yas53x_i2c_driver);
  476. }
  477. module_init(yas53x_init);
  478. module_exit(yas53x_term);
  479. MODULE_AUTHOR("Yamaha Corporation");
  480. MODULE_DESCRIPTION("YAS53x Geomagnetic Sensor Driver");
  481. MODULE_LICENSE("GPL");
  482. MODULE_VERSION(YAS_VERSION);