gsensor.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /* SPDX-License-Identifier: GPL-2.0-only
  2. *
  3. * Verifone Gsensor driver for screen orientation
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/init.h>
  8. #include <linux/i2c.h>
  9. #include <linux/delay.h>
  10. #include <linux/mutex.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/slab.h>
  13. #include <linux/workqueue.h>
  14. extern struct kobject *vfiec_kobj;
  15. /* Verifone API Definitions */
  16. enum gsensor_orientation_hex {
  17. GSENSOR_ORIENT_UNKNOWN_HEX = 0x00,
  18. GSENSOR_ORIENT_PORTRAIT_HEX = 0x14,
  19. GSENSOR_ORIENT_LANDSCAPE_HEX = 0x15,
  20. GSENSOR_ORIENT_PORTRAIT_FLIP_HEX = 0x16,
  21. GSENSOR_ORIENT_LANDSCAPE_FLIP_HEX = 0x17,
  22. };
  23. #define GSENSOR_ORIENT_PORTRAIT_STR "portrait"
  24. #define GSENSOR_ORIENT_LANDSCAPE_STR "landscape"
  25. #define GSENSOR_ORIENT_PORTRAIT_FLIP_STR "portrait_flip"
  26. #define GSENSOR_ORIENT_LANDSCAPE_FLIP_STR "landscape_flip"
  27. #define GSENSOR_MODE_SOFT_RESET BIT(2)
  28. #define GSENSOR_MODE_INITIALIZED BIT(1)
  29. /* Register Definitions */
  30. #define ST_ACCEL_WHO_AM_I_ADDR 0x0F
  31. #define ST_ACCEL_CTRL_REG1_ADDR 0x20
  32. #define ST_ACCEL_CTRL_REG4_ADDR 0x23
  33. #define ST_ACCEL_OUT_X_L_ADDR 0x28
  34. #define SC7A20_WHO_AM_I_VALUE 0x11
  35. /* Configuration */
  36. #define GSENSOR_DEBOUNCE_MS 300
  37. #define GSENSOR_POLL_INTERVAL_MS 200
  38. struct gsensor_data {
  39. struct i2c_client *client;
  40. struct mutex lock;
  41. struct delayed_work poll_work;
  42. struct kobject *gsensor_kobj;
  43. bool enabled;
  44. bool initialized;
  45. enum gsensor_orientation_hex orientation_hex;
  46. enum gsensor_orientation_hex pending_orientation;
  47. char orientation_str[32];
  48. unsigned long last_change_jiffies;
  49. u8 who_am_i;
  50. };
  51. static struct gsensor_data *g_data = NULL;
  52. /* I2C Helper Functions */
  53. static int gsensor_read_reg(struct i2c_client *client, u8 reg, u8 *data)
  54. {
  55. int ret = i2c_smbus_read_byte_data(client, reg);
  56. if (ret < 0)
  57. return ret;
  58. *data = ret;
  59. return 0;
  60. }
  61. static int gsensor_write_reg(struct i2c_client *client, u8 reg, u8 value)
  62. {
  63. return i2c_smbus_write_byte_data(client, reg, value);
  64. }
  65. /* Read the raw data */
  66. static int gsensor_read_data(struct gsensor_data *data, int *x, int *y, int *z)
  67. {
  68. u8 xl, xh, yl, yh, zl, zh;
  69. s16 raw_x, raw_y, raw_z;
  70. int ret;
  71. /* Read the X-axis byte by byte */
  72. ret = gsensor_read_reg(data->client, 0x28, &xl);
  73. if (ret < 0) return ret;
  74. ret = gsensor_read_reg(data->client, 0x29, &xh);
  75. if (ret < 0) return ret;
  76. /* Read the Y-axis byte by byte */
  77. ret = gsensor_read_reg(data->client, 0x2a, &yl);
  78. if (ret < 0) return ret;
  79. ret = gsensor_read_reg(data->client, 0x2b, &yh);
  80. if (ret < 0) return ret;
  81. /* Z-axis */
  82. ret = gsensor_read_reg(data->client, 0x2c, &zl);
  83. if (ret < 0) return ret;
  84. ret = gsensor_read_reg(data->client, 0x2d, &zh);
  85. if (ret < 0) return ret;
  86. raw_x = (s16)((xh << 8) | xl);
  87. raw_y = (s16)((yh << 8) | yl);
  88. raw_z = (s16)((zh << 8) | zl);
  89. /* ±2g Mode: 1 LSB = 0.06103515625 mg */
  90. *x = (raw_x * 61) / 1000;
  91. *y = (raw_y * 61) / 1000;
  92. *z = (raw_z * 61) / 1000;
  93. return 0;
  94. }
  95. /* Enable/Disable sensor */
  96. static int gsensor_set_enable(struct gsensor_data *data, bool enable)
  97. {
  98. int ret;
  99. if (enable) {
  100. /* CTRL_REG1: 0x87 = 100Hz (0x80) + Enable all axes (0x07) */
  101. ret = gsensor_write_reg(data->client, ST_ACCEL_CTRL_REG1_ADDR, 0x87);
  102. if (ret < 0)
  103. return ret;
  104. msleep(20);
  105. } else {
  106. /* CTRL_REG1: 0x00 = Standby mode */
  107. ret = gsensor_write_reg(data->client, ST_ACCEL_CTRL_REG1_ADDR, 0x00);
  108. if (ret < 0)
  109. return ret;
  110. }
  111. return 0;
  112. }
  113. /* Initialize sensor */
  114. static int gsensor_init(struct gsensor_data *data)
  115. {
  116. u8 who_am_i;
  117. int ret;
  118. /* 1. Verify device */
  119. ret = gsensor_read_reg(data->client, ST_ACCEL_WHO_AM_I_ADDR, &who_am_i);
  120. if (ret < 0) {
  121. dev_err(&data->client->dev, "Failed to read WHO_AM_I: %d\n", ret);
  122. return ret;
  123. }
  124. data->who_am_i = who_am_i;
  125. dev_info(&data->client->dev, "WHO_AM_I = 0x%02x\n", who_am_i);
  126. if (who_am_i != SC7A20_WHO_AM_I_VALUE) {
  127. dev_err(&data->client->dev, "Invalid WHO_AM_I: 0x%02x\n", who_am_i);
  128. return -ENODEV;
  129. }
  130. /* 2. Setting CTRL_REG4: BDU enable , ±2g */
  131. ret = gsensor_write_reg(data->client, ST_ACCEL_CTRL_REG4_ADDR, 0x80);
  132. if (ret < 0)
  133. return ret;
  134. /* 3. Disable the sensor at startup */
  135. ret = gsensor_write_reg(data->client, ST_ACCEL_CTRL_REG1_ADDR, 0x00);
  136. if (ret < 0)
  137. return ret;
  138. data->initialized = true;
  139. dev_info(&data->client->dev, "Gsensor initialized successfully\n");
  140. return 0;
  141. }
  142. /* Orientation Calculation */
  143. static enum gsensor_orientation_hex gsensor_calc_orientation(int x, int y, int z)
  144. {
  145. int abs_x = abs(x);
  146. int abs_y = abs(y);
  147. int abs_z = abs(z);
  148. /* Device flat - Z axis maximum */
  149. if (abs_z > 800 && abs_z > abs_x && abs_z > abs_y) {
  150. return GSENSOR_ORIENT_UNKNOWN_HEX;
  151. }
  152. /* Landscape mode: X axis maximum */
  153. if (abs_x > 800 && abs_x > abs_y) {
  154. if (x > 0)
  155. return GSENSOR_ORIENT_LANDSCAPE_FLIP_HEX;
  156. else
  157. return GSENSOR_ORIENT_LANDSCAPE_HEX;
  158. }
  159. /* Portrait mode: Y axis maximum. */
  160. if (abs_y > 800 && abs_y > abs_x) {
  161. if (y > 0)
  162. return GSENSOR_ORIENT_PORTRAIT_FLIP_HEX;
  163. else
  164. return GSENSOR_ORIENT_PORTRAIT_HEX;
  165. }
  166. return GSENSOR_ORIENT_UNKNOWN_HEX;
  167. }
  168. static void gsensor_update_orientation(struct gsensor_data *data)
  169. {
  170. int x, y, z;
  171. enum gsensor_orientation_hex new_orient;
  172. unsigned long now = jiffies;
  173. if (!data->enabled)
  174. return;
  175. if (gsensor_read_data(data, &x, &y, &z) < 0)
  176. return;
  177. new_orient = gsensor_calc_orientation(x, y, z);
  178. if (new_orient != GSENSOR_ORIENT_UNKNOWN_HEX && new_orient != data->orientation_hex) {
  179. if (new_orient == data->pending_orientation) {
  180. if (time_after(now, data->last_change_jiffies +
  181. msecs_to_jiffies(GSENSOR_DEBOUNCE_MS))) {
  182. data->orientation_hex = new_orient;
  183. data->pending_orientation = GSENSOR_ORIENT_UNKNOWN_HEX;
  184. switch (new_orient) {
  185. case GSENSOR_ORIENT_PORTRAIT_HEX:
  186. strcpy(data->orientation_str, GSENSOR_ORIENT_PORTRAIT_STR);
  187. break;
  188. case GSENSOR_ORIENT_LANDSCAPE_HEX:
  189. strcpy(data->orientation_str, GSENSOR_ORIENT_LANDSCAPE_STR);
  190. break;
  191. case GSENSOR_ORIENT_PORTRAIT_FLIP_HEX:
  192. strcpy(data->orientation_str, GSENSOR_ORIENT_PORTRAIT_FLIP_STR);
  193. break;
  194. case GSENSOR_ORIENT_LANDSCAPE_FLIP_HEX:
  195. strcpy(data->orientation_str, GSENSOR_ORIENT_LANDSCAPE_FLIP_STR);
  196. break;
  197. default:
  198. break;
  199. }
  200. dev_info(&data->client->dev, "Orientation: %s [X=%d, Y=%d, Z=%d]\n",
  201. data->orientation_str, x, y, z);
  202. }
  203. } else {
  204. data->pending_orientation = new_orient;
  205. data->last_change_jiffies = now;
  206. }
  207. } else if (new_orient == data->orientation_hex) {
  208. data->pending_orientation = GSENSOR_ORIENT_UNKNOWN_HEX;
  209. }
  210. }
  211. static void gsensor_poll_work(struct work_struct *work)
  212. {
  213. struct gsensor_data *data = container_of(work, struct gsensor_data,
  214. poll_work.work);
  215. mutex_lock(&data->lock);
  216. gsensor_update_orientation(data);
  217. mutex_unlock(&data->lock);
  218. if (data->enabled)
  219. schedule_delayed_work(&data->poll_work,
  220. msecs_to_jiffies(GSENSOR_POLL_INTERVAL_MS));
  221. }
  222. /* Sysfs Interface */
  223. static ssize_t enable_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  224. {
  225. return sprintf(buf, "%d\n", g_data ? g_data->enabled : 0);
  226. }
  227. static ssize_t enable_store(struct kobject *kobj, struct kobj_attribute *attr,
  228. const char *buf, size_t count)
  229. {
  230. struct gsensor_data *data = g_data;
  231. unsigned long val;
  232. int ret;
  233. if (!data)
  234. return -ENODEV;
  235. ret = kstrtoul(buf, 0, &val);
  236. if (ret)
  237. return ret;
  238. mutex_lock(&data->lock);
  239. if (val && !data->enabled) {
  240. ret = gsensor_set_enable(data, true);
  241. if (ret == 0) {
  242. data->enabled = true;
  243. schedule_delayed_work(&data->poll_work, 0);
  244. gsensor_update_orientation(data);
  245. }
  246. } else if (!val && data->enabled) {
  247. cancel_delayed_work_sync(&data->poll_work);
  248. ret = gsensor_set_enable(data, false);
  249. if (ret == 0)
  250. data->enabled = false;
  251. }
  252. mutex_unlock(&data->lock);
  253. return ret < 0 ? ret : count;
  254. }
  255. static struct kobj_attribute enable_attr = __ATTR_RW(enable);
  256. static ssize_t screen_orientation_show(struct kobject *kobj,
  257. struct kobj_attribute *attr, char *buf)
  258. {
  259. return sprintf(buf, "%s\n", g_data ? g_data->orientation_str : "unknown");
  260. }
  261. static struct kobj_attribute screen_orientation_attr = __ATTR_RO(screen_orientation);
  262. static ssize_t instantaneous_orientation_show(struct kobject *kobj,
  263. struct kobj_attribute *attr, char *buf)
  264. {
  265. return sprintf(buf, "0x%02x\n", g_data ? g_data->orientation_hex : 0);
  266. }
  267. static struct kobj_attribute instantaneous_orientation_attr = __ATTR_RO(instantaneous_orientation);
  268. static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  269. {
  270. u8 mode = 0;
  271. if (g_data && g_data->initialized)
  272. mode |= GSENSOR_MODE_INITIALIZED;
  273. return sprintf(buf, "0x%02x\n", mode);
  274. }
  275. static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
  276. const char *buf, size_t count)
  277. {
  278. struct gsensor_data *data = g_data;
  279. unsigned long val;
  280. if (!data)
  281. return -ENODEV;
  282. if (kstrtoul(buf, 0, &val))
  283. return -EINVAL;
  284. if (val & GSENSOR_MODE_SOFT_RESET) {
  285. mutex_lock(&data->lock);
  286. gsensor_init(data);
  287. data->orientation_hex = GSENSOR_ORIENT_LANDSCAPE_HEX;
  288. strcpy(data->orientation_str, GSENSOR_ORIENT_LANDSCAPE_STR);
  289. mutex_unlock(&data->lock);
  290. }
  291. return count;
  292. }
  293. static struct kobj_attribute mode_attr = __ATTR_RW(mode);
  294. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  295. {
  296. u8 state = 0;
  297. if (g_data && g_data->enabled)
  298. state |= 0x01;
  299. return sprintf(buf, "0x%02x\n", state);
  300. }
  301. static struct kobj_attribute state_attr = __ATTR_RO(state);
  302. static ssize_t raw_data_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  303. {
  304. int x, y, z;
  305. if (!g_data)
  306. return -ENODEV;
  307. if (gsensor_read_data(g_data, &x, &y, &z) < 0)
  308. return -EIO;
  309. return sprintf(buf, "%d %d %d\n", x, y, z);
  310. }
  311. static struct kobj_attribute raw_data_attr = __ATTR_RO(raw_data);
  312. static struct attribute *gsensor_attrs[] = {
  313. &enable_attr.attr,
  314. &screen_orientation_attr.attr,
  315. &instantaneous_orientation_attr.attr,
  316. &mode_attr.attr,
  317. &state_attr.attr,
  318. &raw_data_attr.attr,
  319. NULL,
  320. };
  321. static const struct attribute_group gsensor_attr_group = {
  322. .attrs = gsensor_attrs,
  323. };
  324. /* I2C Probe/Remove */
  325. static int gsensor_probe(struct i2c_client *client)
  326. {
  327. struct gsensor_data *data;
  328. int ret;
  329. dev_info(&client->dev, "Gsensor probe\n");
  330. data = kzalloc(sizeof(*data), GFP_KERNEL);
  331. if (!data)
  332. return -ENOMEM;
  333. data->client = client;
  334. mutex_init(&data->lock);
  335. INIT_DELAYED_WORK(&data->poll_work, gsensor_poll_work);
  336. data->orientation_hex = GSENSOR_ORIENT_LANDSCAPE_HEX;
  337. strcpy(data->orientation_str, GSENSOR_ORIENT_LANDSCAPE_STR);
  338. data->pending_orientation = GSENSOR_ORIENT_UNKNOWN_HEX;
  339. data->last_change_jiffies = jiffies;
  340. i2c_set_clientdata(client, data);
  341. ret = gsensor_init(data);
  342. if (ret < 0) {
  343. dev_err(&client->dev, "Init failed: %d\n", ret);
  344. goto err_free;
  345. }
  346. g_data = data;
  347. if (vfiec_kobj) {
  348. data->gsensor_kobj = kobject_create_and_add("gsensor", vfiec_kobj);
  349. if (data->gsensor_kobj) {
  350. ret = sysfs_create_group(data->gsensor_kobj, &gsensor_attr_group);
  351. if (ret < 0) {
  352. dev_err(&client->dev, "Failed to create sysfs group\n");
  353. kobject_put(data->gsensor_kobj);
  354. goto err_free;
  355. }
  356. } else {
  357. dev_err(&client->dev, "Failed to create gsensor kobject\n");
  358. goto err_free;
  359. }
  360. } else {
  361. dev_err(&client->dev, "vfiec_kobj not available\n");
  362. goto err_free;
  363. }
  364. dev_info(&client->dev, "Gsensor driver loaded\n");
  365. return 0;
  366. err_free:
  367. kfree(data);
  368. g_data = NULL;
  369. return ret;
  370. }
  371. static int gsensor_remove(struct i2c_client *client)
  372. {
  373. struct gsensor_data *data = i2c_get_clientdata(client);
  374. if (data) {
  375. cancel_delayed_work_sync(&data->poll_work);
  376. if (data->enabled)
  377. gsensor_set_enable(data, false);
  378. if (data->gsensor_kobj) {
  379. sysfs_remove_group(data->gsensor_kobj, &gsensor_attr_group);
  380. kobject_put(data->gsensor_kobj);
  381. }
  382. mutex_destroy(&data->lock);
  383. kfree(data);
  384. g_data = NULL;
  385. }
  386. return 0;
  387. }
  388. /* I2C Driver */
  389. static const struct i2c_device_id gsensor_id_table[] = {
  390. { "sc7a20", 0 },
  391. { "gsensor", 0 },
  392. {}
  393. };
  394. MODULE_DEVICE_TABLE(i2c, gsensor_id_table);
  395. static struct i2c_driver gsensor_driver = {
  396. .driver = { .name = "verifone-gsensor" },
  397. .probe_new = gsensor_probe,
  398. .remove = gsensor_remove,
  399. .id_table = gsensor_id_table,
  400. };
  401. /* Init/Exit */
  402. int gsensor_init_main(void)
  403. {
  404. printk(KERN_INFO "gsensor: Registering driver\n");
  405. return i2c_add_driver(&gsensor_driver);
  406. }
  407. EXPORT_SYMBOL(gsensor_init_main);
  408. void gsensor_exit_main(void)
  409. {
  410. printk(KERN_INFO "gsensor: Unregistering driver\n");
  411. i2c_del_driver(&gsensor_driver);
  412. }
  413. EXPORT_SYMBOL(gsensor_exit_main);
  414. MODULE_LICENSE("GPL v2");
  415. MODULE_AUTHOR("Verifone, Inc.");
  416. MODULE_DESCRIPTION("Verifone Gsensor driver for screen orientation");