mlx90615.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /* driver/sensor/mlx90615.c
  2. * Copyright (c) 2011 SAMSUNG
  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/i2c.h>
  20. #include <linux/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/device.h>
  23. #include <linux/input.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/slab.h>
  28. #include <linux/delay.h>
  29. #include <linux/fs.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/gpio.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/module.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/sensors_core.h>
  36. #define VENDOR "MELEXIS"
  37. #define CHIP_ID "MLX90615"
  38. #define I2C_M_WR 0 /* for i2c Write */
  39. #define I2c_M_RD 1 /* for i2c Read */
  40. #define RAM_REG_RAW_IR 0x25
  41. #define RAM_REG_T_AMBIENT 0x26
  42. #define RAM_REG_T_OUT 0x27
  43. #define ROM_REG_I2C_ADDR 0x10
  44. #define ROM_REG_CONFIG 0x12
  45. #define ROM_REG_EMISSIVITY 0x13
  46. #define ROM_REG_ID_NUMBER 0x1E
  47. #define REG_SLEEP 0xff
  48. #define DEFAULT_DELAY 210
  49. #define I2C_READ 0x01
  50. #define I2C_WRITE 0x00
  51. #define EVENT_TYPE_AMBIENT_TEMP REL_HWHEEL
  52. #define EVENT_TYPE_OBJECT_TEMP REL_DIAL
  53. #define MLX90615_CMD_LENGTH 2
  54. #define TEMPERATURE_OFFSET 0
  55. #define ARRAY_DEPTH 7
  56. struct mlx90615_data {
  57. struct i2c_client *i2c_client;
  58. struct input_dev *input;
  59. struct mutex power_lock;
  60. struct mutex read_lock;
  61. struct hrtimer timer;
  62. struct workqueue_struct *mlx90615_wq;
  63. struct work_struct work_mlx90615;
  64. struct device *mlx90615_dev;
  65. ktime_t poll_delay;
  66. u8 enabled;
  67. int ambient_temp;
  68. int object_temp;
  69. u32 scl;
  70. u32 sda;
  71. u32 power;
  72. int always_on;
  73. int amb[ARRAY_DEPTH], obj[ARRAY_DEPTH];
  74. int index;
  75. };
  76. static inline void setsda(struct mlx90615_data *data, int state)
  77. {
  78. if (state)
  79. gpio_direction_input(data->sda);
  80. else
  81. gpio_direction_output(data->sda, 0);
  82. }
  83. static inline void setscl(struct mlx90615_data *data, int state)
  84. {
  85. if (state)
  86. gpio_direction_output(data->scl, 1);
  87. else
  88. gpio_direction_output(data->scl, 0);
  89. }
  90. static int mlx90615_power_parse_dt(struct device *dev,
  91. struct mlx90615_data *data)
  92. {
  93. struct device_node *np = dev->of_node;
  94. enum of_gpio_flags flags;
  95. int errorno = 0;
  96. data->power = of_get_named_gpio_flags(np, "mlx90615,power",
  97. 0, &flags);
  98. if (data->power < 0) {
  99. errorno = data->power;
  100. pr_err("[BODYTEMP] %s: mlx90615,power\n", __func__);
  101. goto dt_exit;
  102. }
  103. errorno = gpio_request(data->power, "MLX9061_POWER");
  104. if (errorno) {
  105. pr_err("[BODYTEMP] %s: failed to request EN PIN for MLX90615\n",
  106. __func__);
  107. goto dt_exit;
  108. }
  109. errorno = gpio_direction_output(data->power, 0);
  110. if (errorno) {
  111. pr_err("[BODYTEMP] %s: failed to set ENABLE PIN as input\n",
  112. __func__);
  113. goto dt_exit;
  114. }
  115. return 0;
  116. dt_exit:
  117. return errorno;
  118. }
  119. static int mlx90615_i2c_read_word(struct mlx90615_data *mlx90615, u8 command,
  120. s32 *val)
  121. {
  122. int err = 0;
  123. struct i2c_client *client = mlx90615->i2c_client;
  124. struct i2c_msg msg[2];
  125. unsigned char data[3] = {0,};
  126. s32 value = 0;
  127. if ((client == NULL) || (!client->adapter))
  128. return -ENODEV;
  129. /* send slave address & command */
  130. msg[0].addr = client->addr;
  131. msg[0].flags = I2C_M_WR;
  132. msg[0].len = 1;
  133. msg[0].buf = &command;
  134. /* read word data */
  135. msg[1].addr = client->addr;
  136. msg[1].flags = I2C_M_RD;
  137. msg[1].len = 3;
  138. msg[1].buf = data;
  139. err = i2c_transfer(client->adapter, msg, 2);
  140. if (err >= 0) {
  141. if(data[2] == 0xff) {
  142. pr_err("[BODYTEMP] %s : crc error detect", __func__);
  143. return -EINVAL;
  144. } else {
  145. value = (s32)data[1];
  146. *val= (value << 8) | (s32)data[0];
  147. }
  148. } else
  149. pr_err("[BODYTEMP] %s, i2c transfer error ret=%d\n",
  150. __func__, err);
  151. return err;
  152. }
  153. static void mlx90615_set_power(struct mlx90615_data *mlx90615, int onoff)
  154. {
  155. pr_info("[BODYTEMP] %s : %s\n", __func__, (onoff)?"ON":"OFF");
  156. if (onoff) {
  157. setscl(mlx90615, 1);
  158. if (mlx90615->power > 0)
  159. gpio_set_value(mlx90615->power, 1);
  160. else {
  161. mlx90615_power_parse_dt(&mlx90615->i2c_client->dev,
  162. mlx90615);
  163. }
  164. udelay(100);
  165. setscl(mlx90615, 0);
  166. msleep(50);
  167. setscl(mlx90615, 1);
  168. msleep(20);
  169. } else
  170. if (mlx90615->power > 0)
  171. gpio_set_value(mlx90615->power, 0);
  172. }
  173. static void mlx90615_convert(s32 kalvin, int *real, int* frac)
  174. {
  175. int temperature;
  176. temperature = (kalvin * 2) - 27315;
  177. *real = temperature / 100;
  178. *frac = temperature % 100;
  179. }
  180. static int mlx90615_read_data(struct mlx90615_data *mlx90615,
  181. int *tar, int *taf, int *tor, int *tof)
  182. {
  183. static s32 object;
  184. static s32 ambient;
  185. s32 ret;
  186. u8 cmd;
  187. if (mlx90615->always_on == 0)
  188. mlx90615_set_power(mlx90615, 1);
  189. cmd = RAM_REG_T_AMBIENT;
  190. ret = mlx90615_i2c_read_word(mlx90615, cmd, &ambient);
  191. // ret = i2c_smbus_read_word_data(mlx90615->i2c_client, cmd);
  192. if (ret < 0)
  193. pr_info("[BODYTEMP] %s : ambient temp read fail %d\n",
  194. __func__, ret);
  195. mlx90615_convert(ambient, tar, taf);
  196. cmd = RAM_REG_T_OUT;
  197. ret = mlx90615_i2c_read_word(mlx90615, cmd, &object);
  198. // ret = i2c_smbus_read_word_data(mlx90615->i2c_client, cmd);
  199. if (ret < 0)
  200. pr_info("[BODYTEMP] %s : object temp read fail %d\n",
  201. __func__, ret);
  202. mlx90615_convert(object, tor, tof);
  203. if (mlx90615->always_on == 0)
  204. mlx90615_set_power(mlx90615, 0);
  205. return 0;
  206. }
  207. static void mlx90615_enable(struct mlx90615_data *mlx90615)
  208. {
  209. int i;
  210. /* enable setting */
  211. pr_info("[BODYTEMP] %s: starting poll timer, delay %lldns\n",
  212. __func__, ktime_to_ns(mlx90615->poll_delay));
  213. /*
  214. if (ktime_to_ms(mlx90615->poll_delay) < 200) {
  215. pr_info("%s : power always on mode\n", __func__);
  216. mlx90615->always_on = 1;
  217. mlx90615_set_power(mlx90615, 1);
  218. } else {
  219. pr_info("%s : power on/off mode\n", __func__);
  220. mlx90615->always_on = 0;
  221. mlx90615_set_power(mlx90615, 0);
  222. }
  223. */
  224. mlx90615->always_on = 1;
  225. mlx90615_set_power(mlx90615, 1);
  226. mlx90615->index = 0;
  227. for (i = 0 ; i < 5 ; i++) {
  228. mlx90615->amb[i] = 0;
  229. mlx90615->obj[i] = 0;
  230. }
  231. hrtimer_start(&mlx90615->timer, mlx90615->poll_delay,
  232. HRTIMER_MODE_REL);
  233. }
  234. static void mlx90615_disable(struct mlx90615_data *mlx90615)
  235. {
  236. /* disable setting */
  237. pr_info("[BODYTEMP] %s: cancelling poll timer\n", __func__);
  238. mlx90615->always_on = 0;
  239. mlx90615_set_power(mlx90615, 0);
  240. hrtimer_cancel(&mlx90615->timer);
  241. cancel_work_sync(&mlx90615->work_mlx90615);
  242. }
  243. /* sysfs */
  244. static ssize_t poll_delay_show(struct device *dev,
  245. struct device_attribute *attr, char *buf)
  246. {
  247. struct mlx90615_data *mlx90615 = dev_get_drvdata(dev);
  248. return sprintf(buf, "%lld\n", ktime_to_ns(mlx90615->poll_delay));
  249. }
  250. static ssize_t poll_delay_store(struct device *dev,
  251. struct device_attribute *attr,
  252. const char *buf, size_t size)
  253. {
  254. struct mlx90615_data *mlx90615 = dev_get_drvdata(dev);
  255. int64_t new_delay;
  256. int err;
  257. pr_info("[BODYTEMP] %s : %s\n", __func__, buf);
  258. err = strict_strtoll(buf, 10, &new_delay);
  259. if (err < 0)
  260. return err;
  261. mutex_lock(&mlx90615->power_lock);
  262. if (new_delay != ktime_to_ns(mlx90615->poll_delay)) {
  263. #if 0
  264. if (new_delay < ktime_to_ns(mlx90615->poll_delay))
  265. mlx90615->poll_delay =
  266. ns_to_ktime(DEFAULT_DELAY * NSEC_PER_MSEC);
  267. else
  268. #endif
  269. mlx90615->poll_delay = ns_to_ktime(new_delay);
  270. if (mlx90615->enabled) {
  271. mlx90615_disable(mlx90615);
  272. mlx90615_enable(mlx90615);
  273. }
  274. pr_info("[BODYTEMP] %s, poll_delay = %lld\n",
  275. __func__, new_delay);
  276. }
  277. mutex_unlock(&mlx90615->power_lock);
  278. return size;
  279. }
  280. static ssize_t mlx90615_enable_store(struct device *dev,
  281. struct device_attribute *attr,
  282. const char *buf, size_t size)
  283. {
  284. struct mlx90615_data *mlx90615 = dev_get_drvdata(dev);
  285. bool new_value;
  286. if (sysfs_streq(buf, "1"))
  287. new_value = true;
  288. else if (sysfs_streq(buf, "0"))
  289. new_value = false;
  290. else {
  291. pr_err("[BODYTEMP] %s: invalid value %d\n", __func__, *buf);
  292. return -EINVAL;
  293. }
  294. mutex_lock(&mlx90615->power_lock);
  295. pr_info("[BODYTEMP] %s: new_value = %d\n", __func__, new_value);
  296. if (new_value && !mlx90615->enabled) {
  297. mlx90615->enabled = 1;
  298. mlx90615_enable(mlx90615);
  299. } else if (!new_value && mlx90615->enabled) {
  300. mlx90615_disable(mlx90615);
  301. mlx90615->enabled = 0;
  302. }
  303. mutex_unlock(&mlx90615->power_lock);
  304. return size;
  305. }
  306. static ssize_t mlx90615_enable_show(struct device *dev,
  307. struct device_attribute *attr, char *buf)
  308. {
  309. struct mlx90615_data *mlx90615 = dev_get_drvdata(dev);
  310. return sprintf(buf, "%d\n", mlx90615->enabled);
  311. }
  312. static ssize_t mlx90615_raw_data_show(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. struct mlx90615_data *mlx90615 = dev_get_drvdata(dev);
  316. int tar = 0, taf = 0, tor = 0, tof = 0;
  317. mutex_lock(&mlx90615->read_lock);
  318. mlx90615_read_data(mlx90615, &tar, &taf, &tor, &tof);
  319. mutex_unlock(&mlx90615->read_lock);
  320. return sprintf(buf, "%d.%02d,%d.%02d\n", tar,taf,tor,tof);
  321. }
  322. static DEVICE_ATTR(raw_data, S_IRUGO, mlx90615_raw_data_show, NULL);
  323. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  324. poll_delay_show, poll_delay_store);
  325. /* sysfs for vendor & name */
  326. static ssize_t mlx90615_vendor_show(struct device *dev,
  327. struct device_attribute *attr, char *buf)
  328. {
  329. return sprintf(buf, "%s\n", VENDOR);
  330. }
  331. static ssize_t mlx90615_name_show(struct device *dev,
  332. struct device_attribute *attr, char *buf)
  333. {
  334. return sprintf(buf, "%s\n", CHIP_ID);
  335. }
  336. static DEVICE_ATTR(vendor, 0644, mlx90615_vendor_show, NULL);
  337. static DEVICE_ATTR(name, 0644, mlx90615_name_show, NULL);
  338. static struct device_attribute dev_attr_enable =
  339. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  340. mlx90615_enable_show, mlx90615_enable_store);
  341. static struct attribute *mlx90615_sysfs_attrs[] = {
  342. &dev_attr_enable.attr,
  343. &dev_attr_poll_delay.attr,
  344. NULL
  345. };
  346. static struct attribute_group mlx90615_attribute_group = {
  347. .attrs = mlx90615_sysfs_attrs,
  348. };
  349. static struct device_attribute *bodytemp_sensor_attrs[] = {
  350. &dev_attr_raw_data,
  351. &dev_attr_vendor,
  352. &dev_attr_name,
  353. &dev_attr_enable,
  354. &dev_attr_poll_delay,
  355. NULL,
  356. };
  357. /* This function is for mlx90615 sensor. It operates every a few seconds.
  358. * It asks for work to be done on a thread because i2c needs a thread
  359. * context (slow and blocking) and then reschedules the timer to run again.
  360. */
  361. static enum hrtimer_restart timer_func(struct hrtimer *timer)
  362. {
  363. struct mlx90615_data *mlx90615
  364. = container_of(timer, struct mlx90615_data, timer);
  365. queue_work(mlx90615->mlx90615_wq, &mlx90615->work_mlx90615);
  366. hrtimer_forward_now(&mlx90615->timer, mlx90615->poll_delay);
  367. return HRTIMER_RESTART;
  368. }
  369. static void mlx90615_work_func(struct work_struct *work)
  370. {
  371. struct mlx90615_data *mlx90615 = container_of(work, struct mlx90615_data,
  372. work_mlx90615);
  373. int tar = 0, taf = 0, tor = 0, tof = 0;
  374. int ambient, object;
  375. int amb_min = 80000, obj_min = 80000;
  376. int amb_max = -1, obj_max = -1;
  377. int index;
  378. int i, total_a = 0, total_o = 0;
  379. mutex_lock(&mlx90615->read_lock);
  380. mlx90615_read_data(mlx90615, &tar, &taf, &tor, &tof);
  381. mutex_unlock(&mlx90615->read_lock);
  382. ambient = tar * 100 + taf + TEMPERATURE_OFFSET;
  383. object = tor * 100 + tof + TEMPERATURE_OFFSET;
  384. index = mlx90615->index;
  385. mlx90615->amb[index] = ambient;
  386. mlx90615->obj[index] = object;
  387. index++;
  388. mlx90615->index = index % ARRAY_DEPTH;
  389. for ( i = 0 ; i < ARRAY_DEPTH ; i++) {
  390. total_a += mlx90615->amb[i];
  391. total_o += mlx90615->obj[i];
  392. if (mlx90615->amb[i] <= amb_min)
  393. amb_min = mlx90615->amb[i];
  394. if (mlx90615->amb[i] >= amb_max)
  395. amb_max = mlx90615->amb[i];
  396. if (mlx90615->obj[i] <= obj_min)
  397. obj_min = mlx90615->obj[i];
  398. if (mlx90615->obj[i] >= obj_max)
  399. obj_max = mlx90615->obj[i];
  400. }
  401. total_a = total_a - amb_min - amb_max;
  402. total_o = total_o - obj_min - obj_max;
  403. mlx90615->ambient_temp = total_a / (ARRAY_DEPTH - 2);
  404. mlx90615->object_temp = total_o / (ARRAY_DEPTH - 2);
  405. input_report_rel(mlx90615->input, EVENT_TYPE_AMBIENT_TEMP,
  406. mlx90615->ambient_temp + 27315);
  407. input_report_rel(mlx90615->input, EVENT_TYPE_OBJECT_TEMP,
  408. mlx90615->object_temp + 27315);
  409. input_sync(mlx90615->input);
  410. pr_info("[BODYTEMP] %s: ambient = %d.%02d C object = %d.%02d C\n",
  411. __func__, tar, taf, tor, tof);
  412. }
  413. static int mlx90615_parse_dt(struct device *dev, struct mlx90615_data *data)
  414. {
  415. struct device_node *np = dev->of_node;
  416. enum of_gpio_flags flags;
  417. int errorno = 0;
  418. pr_info("[BODYTEMP] %s : mlx90615,scl\n", __func__);
  419. data->scl = of_get_named_gpio_flags(np, "mlx90615,scl",
  420. 0, &flags);
  421. if (data->scl < 0) {
  422. errorno = data->scl;
  423. pr_err("[BODYTEMP] %s: mlx90615,scl\n", __func__);
  424. goto dt_exit;
  425. }
  426. pr_info("[BODYTEMP] %s : mlx90615,sda\n", __func__);
  427. data->sda = of_get_named_gpio_flags(np, "mlx90615,sda",
  428. 0, &flags);
  429. if (data->sda < 0) {
  430. errorno = data->sda;
  431. pr_err("[BODYTEMP] %s: mlx90615,sda\n", __func__);
  432. goto dt_exit;
  433. }
  434. pr_info("[BODYTEMP] %s : mlx90615,power\n", __func__);
  435. data->power = of_get_named_gpio_flags(np, "mlx90615,power",
  436. 0, &flags);
  437. if (data->power < 0) {
  438. errorno = data->power;
  439. pr_err("[BODYTEMP] %s: mlx90615,power\n", __func__);
  440. goto dt_exit;
  441. }
  442. errorno = gpio_request(data->power, "MLX9061_POWER");
  443. if (errorno) {
  444. pr_err("[BODYTEMP] %s: failed to request ENABLE PIN\n",
  445. __func__);
  446. goto dt_exit;
  447. }
  448. errorno = gpio_direction_output(data->power, 0);
  449. if (errorno) {
  450. pr_err("[BODYTEMP] %s: failed to set ENABLE PIN as input\n",
  451. __func__);
  452. goto dt_exit;
  453. }
  454. return 0;
  455. dt_exit:
  456. return errorno;
  457. }
  458. static int mlx90615_i2c_probe(struct i2c_client *client,
  459. const struct i2c_device_id *id)
  460. {
  461. int ret = -ENODEV;
  462. struct mlx90615_data *mlx90615 = NULL;
  463. pr_info("[BODYTEMP] %s is called.\n", __func__);
  464. /* Check i2c functionality */
  465. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  466. pr_err("[BODYTEMP] %s: i2c functionality check failed!\n",
  467. __func__);
  468. return ret;
  469. }
  470. mlx90615 = kzalloc(sizeof(struct mlx90615_data), GFP_KERNEL);
  471. if (!mlx90615) {
  472. pr_err("[BODYTEMP] %s: failed to alloc memory mlx90615_data\n",
  473. __func__);
  474. return -ENOMEM;
  475. }
  476. if (client-> dev.of_node)
  477. pr_info("[BODYTEMP] %s : of node\n", __func__);
  478. else {
  479. pr_err("[BODYTEMP] %s : no of node\n", __func__);
  480. goto err_setup_reg;
  481. }
  482. ret = mlx90615_parse_dt(&client->dev, mlx90615);
  483. if (ret) {
  484. pr_err("[BODYTEMP] %s : parse dt error\n", __func__);
  485. //goto err_parse_dt;
  486. }
  487. mlx90615->i2c_client = client;
  488. mlx90615->always_on = 0;
  489. i2c_set_clientdata(client, mlx90615);
  490. mutex_init(&mlx90615->power_lock);
  491. mutex_init(&mlx90615->read_lock);
  492. #if 0
  493. /* Check if the device is there or not. */
  494. ret = i2c_master_send(mlx90615->i2c_client,
  495. CMD_READ_ID_REG, MLX90615_CMD_LENGTH);
  496. if (ret < 0) {
  497. pr_err("[BODYTEMP] %s: failed i2c_master_send (err = %d)\n",
  498. __func__, ret);
  499. /* goto err_i2c_master_send;*/
  500. }
  501. #endif
  502. /* allocate mlx90615 input_device */
  503. mlx90615->input = input_allocate_device();
  504. if (!mlx90615->input) {
  505. pr_err("[BODYTEMP] %s: could not allocate input device\n",
  506. __func__);
  507. goto err_input_allocate_device;
  508. }
  509. mlx90615->input->name = "bodytemp_sensor";
  510. input_set_capability(mlx90615->input, EV_REL, EVENT_TYPE_AMBIENT_TEMP);
  511. input_set_capability(mlx90615->input, EV_REL, EVENT_TYPE_OBJECT_TEMP);
  512. input_set_drvdata(mlx90615->input, mlx90615);
  513. ret = input_register_device(mlx90615->input);
  514. if (ret < 0) {
  515. input_free_device(mlx90615->input);
  516. pr_err("[BODYTEMP] %s: could not register input device\n",
  517. __func__);
  518. goto err_input_register_device;
  519. }
  520. ret = sensors_create_symlink(&mlx90615->input->dev.kobj,
  521. mlx90615->input->name);
  522. if (ret < 0) {
  523. input_unregister_device(mlx90615->input);
  524. goto err_sysfs_create_symlink;
  525. }
  526. ret = sysfs_create_group(&mlx90615->input->dev.kobj,
  527. &mlx90615_attribute_group);
  528. if (ret) {
  529. pr_err("[BODYTEMP] %s: could not create sysfs group\n",
  530. __func__);
  531. goto err_sysfs_create_group;
  532. }
  533. ret = sensors_register(mlx90615->mlx90615_dev,
  534. mlx90615, bodytemp_sensor_attrs, "bodytemp_sensor");
  535. if (ret) {
  536. pr_err("[BODYTEMP] %s: cound not register sensor device(%d).\n",
  537. __func__, ret);
  538. goto err_sysfs_create_symlink;
  539. }
  540. /* Timer settings. We poll for mlx90615 values using a timer. */
  541. hrtimer_init(&mlx90615->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  542. mlx90615->poll_delay = ns_to_ktime(DEFAULT_DELAY * NSEC_PER_MSEC);
  543. mlx90615->timer.function = timer_func;
  544. /* Timer just fires off a work queue request. We need a thread
  545. to read the i2c (can be slow and blocking). */
  546. mlx90615->mlx90615_wq =
  547. create_singlethread_workqueue("mlx90615_bidytemp_wq");
  548. if (!mlx90615->mlx90615_wq) {
  549. ret = -ENOMEM;
  550. pr_err("[BODYTEMP] %s: could not create mlx90615 workqueue\n", __func__);
  551. goto err_create_workqueue;
  552. }
  553. /* This is the thread function we run on the work queue */
  554. INIT_WORK(&mlx90615->work_mlx90615, mlx90615_work_func);
  555. pr_info("[BODYTEMP] %s is success.\n", __func__);
  556. goto done;
  557. err_create_workqueue:
  558. sysfs_remove_group(&mlx90615->input->dev.kobj,
  559. &mlx90615_attribute_group);
  560. /* sensors_classdev_unregister(mlx90615->mlx90615_dev);*/
  561. destroy_workqueue(mlx90615->mlx90615_wq);
  562. err_sysfs_create_group:
  563. input_unregister_device(mlx90615->input);
  564. err_sysfs_create_symlink:
  565. sensors_remove_symlink(&mlx90615->input->dev.kobj,
  566. mlx90615->input->name);
  567. err_input_register_device:
  568. err_input_allocate_device:
  569. mutex_destroy(&mlx90615->read_lock);
  570. mutex_destroy(&mlx90615->power_lock);
  571. err_setup_reg:
  572. //err_parse_dt:
  573. kfree(mlx90615);
  574. done:
  575. return ret;
  576. }
  577. static int mlx90615_i2c_remove(struct i2c_client *client)
  578. {
  579. struct mlx90615_data *mlx90615 = i2c_get_clientdata(client);
  580. /* device off */
  581. if (mlx90615->enabled)
  582. mlx90615_disable(mlx90615);
  583. /* destroy workqueue */
  584. destroy_workqueue(mlx90615->mlx90615_wq);
  585. #if 0
  586. /* sysfs destroy */
  587. device_remove_file(mlx90615->mlx90615_dev, &dev_attr_name);
  588. device_remove_file(mlx90615->mlx90615_dev, &dev_attr_vendor);
  589. device_remove_file(mlx90615->mlx90615_dev, &dev_attr_raw_data);
  590. #endif
  591. //sensors_classdev_unregister(mlx90615->mlx90615_dev);
  592. /* input device destroy */
  593. sysfs_remove_group(&mlx90615->input->dev.kobj,
  594. &mlx90615_attribute_group);
  595. input_unregister_device(mlx90615->input);
  596. /* lock destroy */
  597. mutex_destroy(&mlx90615->read_lock);
  598. mutex_destroy(&mlx90615->power_lock);
  599. kfree(mlx90615);
  600. return 0;
  601. }
  602. static int mlx90615_suspend(struct device *dev)
  603. {
  604. struct mlx90615_data *mlx90615 = dev_get_drvdata(dev);
  605. if (mlx90615->enabled)
  606. mlx90615_disable(mlx90615);
  607. return 0;
  608. }
  609. static int mlx90615_resume(struct device *dev)
  610. {
  611. struct mlx90615_data *mlx90615 = dev_get_drvdata(dev);
  612. if (mlx90615->enabled)
  613. mlx90615_enable(mlx90615);
  614. return 0;
  615. }
  616. //MODULE_DEVICE_TABLE(i2c, mlx90615_device_id);
  617. static struct of_device_id mlx90615_table[] = {
  618. { .compatible = "mlx90615",},
  619. {},
  620. };
  621. static const struct i2c_device_id mlx90615_device_id[] = {
  622. {"mlx90615_table", 0},
  623. {}
  624. };
  625. static const struct dev_pm_ops mlx90615_pm_ops = {
  626. .suspend = mlx90615_suspend,
  627. .resume = mlx90615_resume
  628. };
  629. static struct i2c_driver mlx90615_i2c_driver = {
  630. .driver = {
  631. .name = "mlx90615",
  632. .owner = THIS_MODULE,
  633. .pm = &mlx90615_pm_ops,
  634. .of_match_table = mlx90615_table,
  635. },
  636. .probe = mlx90615_i2c_probe,
  637. .remove = mlx90615_i2c_remove,
  638. .id_table = mlx90615_device_id,
  639. };
  640. static int __init mlx90615_init(void)
  641. {
  642. return i2c_add_driver(&mlx90615_i2c_driver);
  643. }
  644. static void __exit mlx90615_exit(void)
  645. {
  646. i2c_del_driver(&mlx90615_i2c_driver);
  647. }
  648. module_init(mlx90615_init);
  649. module_exit(mlx90615_exit);
  650. MODULE_AUTHOR("Samsung Electronics");
  651. MODULE_DESCRIPTION("mlx90615 Temperature Sensor & mlx90615 sensor device driver");
  652. MODULE_LICENSE("GPL");