max86900.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /*
  2. * Copyright (c)2013 Maxim Integrated Products, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU 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 "max86900.h"
  19. #ifdef CONFIG_OF
  20. #include <linux/of_gpio.h>
  21. #endif
  22. #define MAX86900_I2C_RETRY_DELAY 10
  23. #define MAX86900_I2C_MAX_RETRIES 5
  24. #define MAX86900_COUNT_MAX 65532
  25. #define MAX86900_WHOAMI_REG 0xFE
  26. #define MAX86900_WHOAMI 0x01
  27. #define MAX86900A_WHOAMI 0x11
  28. #define MAX86900A_REV_ID 0x00
  29. #define MAX86900B_REV_ID 0x04
  30. #define MAX86900C_REV_ID 0x05
  31. #define MAX86900_DEFAULT_CURRENT 0x55
  32. #define MAX86900A_DEFAULT_CURRENT 0xFF
  33. #define MAX86900C_DEFAULT_CURRENT 0x0F
  34. #define MODULE_NAME_HRM "hrm_sensor"
  35. #define CHIP_NAME "MAX86900"
  36. #define VENDOR "MAXIM"
  37. #define HRM_LDO_ON 1
  38. #define HRM_LDO_OFF 0
  39. #define MAX_EOL_RESULT 132
  40. #define MAX_LIB_VER 20
  41. #define SAMPLE_RATE 4
  42. /* I2C function */
  43. static int max86900_write_reg(struct max86900_device_data *device,
  44. u8 reg_addr, u8 data)
  45. {
  46. int err;
  47. int tries = 0;
  48. u8 buffer[2] = { reg_addr, data };
  49. struct i2c_msg msgs[] = {
  50. {
  51. .addr = device->client->addr,
  52. .flags = device->client->flags & I2C_M_TEN,
  53. .len = 2,
  54. .buf = buffer,
  55. },
  56. };
  57. do {
  58. mutex_lock(&device->i2clock);
  59. err = i2c_transfer(device->client->adapter, msgs, 1);
  60. mutex_unlock(&device->i2clock);
  61. if (err != 1)
  62. msleep_interruptible(MAX86900_I2C_RETRY_DELAY);
  63. } while ((err != 1) && (++tries < MAX86900_I2C_MAX_RETRIES));
  64. if (err != 1) {
  65. pr_err("%s -write transfer error\n", __func__);
  66. err = -EIO;
  67. return err;
  68. }
  69. return 0;
  70. }
  71. static int max86900_read_reg(struct max86900_device_data *data,
  72. u8 *buffer, int length)
  73. {
  74. int err = -1;
  75. int tries = 0; /* # of attempts to read the device */
  76. struct i2c_msg msgs[] = {
  77. {
  78. .addr = data->client->addr,
  79. .flags = data->client->flags & I2C_M_TEN,
  80. .len = 1,
  81. .buf = buffer,
  82. },
  83. {
  84. .addr = data->client->addr,
  85. .flags = (data->client->flags & I2C_M_TEN) | I2C_M_RD,
  86. .len = length,
  87. .buf = buffer,
  88. },
  89. };
  90. do {
  91. mutex_lock(&data->i2clock);
  92. err = i2c_transfer(data->client->adapter, msgs, 2);
  93. mutex_unlock(&data->i2clock);
  94. if (err != 2)
  95. msleep_interruptible(MAX86900_I2C_RETRY_DELAY);
  96. } while ((err != 2) && (++tries < MAX86900_I2C_MAX_RETRIES));
  97. if (err != 2) {
  98. pr_err("%s -read transfer error\n", __func__);
  99. err = -EIO;
  100. } else
  101. err = 0;
  102. return err;
  103. }
  104. /* Device Control */
  105. static int max86900_regulator_onoff(struct max86900_device_data *data, int onoff)
  106. {
  107. data->vdd_1p8 = regulator_get(NULL, data->sub_ldo4);
  108. if (IS_ERR(data->vdd_1p8)) {
  109. pr_err("%s - regulator_get fail\n", __func__);
  110. goto err_1p8;
  111. }
  112. #if defined(CONFIG_SEC_S_PROJECT)
  113. data->vdd_3p3 = regulator_get(NULL, data->led_l19);
  114. if (IS_ERR(data->vdd_3p3)) {
  115. pr_err("%s - regulator_get fail\n", __func__);
  116. goto err_1p8;
  117. }
  118. #endif
  119. pr_info("%s - onoff = %d\n", __func__, onoff);
  120. if (onoff == HRM_LDO_ON) {
  121. regulator_set_voltage(data->vdd_1p8, 1800000, 1800000);
  122. regulator_enable(data->vdd_1p8);
  123. #if defined(CONFIG_SEC_S_PROJECT)
  124. regulator_set_voltage(data->vdd_3p3, 3300000, 3300000);
  125. regulator_enable(data->vdd_3p3);
  126. #endif
  127. } else {
  128. regulator_disable(data->vdd_1p8);
  129. #if defined(CONFIG_SEC_S_PROJECT)
  130. regulator_disable(data->vdd_3p3);
  131. #endif
  132. }
  133. regulator_put(data->vdd_1p8);
  134. #if defined(CONFIG_SEC_S_PROJECT)
  135. regulator_put(data->vdd_3p3);
  136. #endif
  137. return 0;
  138. err_1p8:
  139. return -ENODEV;
  140. }
  141. static int max86900_init_device(struct max86900_device_data *data)
  142. {
  143. int err;
  144. u8 recvData;
  145. err = max86900_write_reg(data, MAX86900_MODE_CONFIGURATION, 0x40);
  146. if (err != 0) {
  147. pr_err("%s - error sw shutdown device!\n", __func__);
  148. return -EIO;
  149. }
  150. /* Interrupt Clear */
  151. recvData = MAX86900_INTERRUPT_STATUS;
  152. if ((err = max86900_read_reg(data, &recvData, 1)) != 0) {
  153. pr_err("%s - max86900_read_reg err:%d, address:0x%02x\n",
  154. __func__, err, recvData);
  155. return -EIO;
  156. }
  157. err = max86900_write_reg(data, MAX86900_MODE_CONFIGURATION, 0x83);
  158. if (err != 0) {
  159. pr_err("%s - error initializing MAX86900_MODE_CONFIGURATION!\n",
  160. __func__);
  161. return -EIO;
  162. }
  163. err = max86900_write_reg(data, MAX86900_INTERRUPT_ENABLE, 0x10);
  164. if (err != 0) {
  165. pr_err("%s - error initializing MAX86900_INTERRUPT_ENABLE!\n",
  166. __func__);
  167. return -EIO;
  168. }
  169. err = max86900_write_reg(data, MAX86900_SPO2_CONFIGURATION, 0x51); //400hz 400us
  170. if (err != 0) {
  171. pr_err("%s - error initializing MAX86900_SPO2_CONFIGURATION!\n",
  172. __func__);
  173. return -EIO;
  174. }
  175. err = max86900_write_reg(data, MAX86900_LED_CONFIGURATION, 0x00);
  176. if (err != 0) {
  177. pr_err("%s - error initializing MAX86900_LED_CONFIGURATION!\n",
  178. __func__);
  179. return -EIO;
  180. }
  181. /* Turn off ALC in case of Max86900A */
  182. if (data->part_type == PART_TYPE_MAX86900A) {
  183. /* ALC OFF start */
  184. err = max86900_write_reg(data, MAX86900_TEST_MODE, 0x54);
  185. if (err != 0) {
  186. pr_err("%s - error initializing MAX86900_TEST_MODE!\n",
  187. __func__);
  188. return -EIO;
  189. }
  190. err = max86900_write_reg(data, MAX86900_TEST_MODE, 0x4D);
  191. if (err != 0) {
  192. pr_err("%s - error initializing MAX86900_TEST_MODE!\n",
  193. __func__);
  194. return -EIO;
  195. }
  196. err = max86900_write_reg(data, MAX86900_TEST_GTST, 0x02);
  197. if (err != 0) {
  198. pr_err("%s - error initializing MAX86900_TEST_GTST!\n",
  199. __func__);
  200. return -EIO;
  201. }
  202. err = max86900_write_reg(data, MAX86900_TEST_ALC, 0x01);
  203. if (err != 0) {
  204. pr_err("%s - error initializing MAX86900_TEST_ALC!\n",
  205. __func__);
  206. return -EIO;
  207. }
  208. err = max86900_write_reg(data, MAX86900_TEST_MODE, 0x00);
  209. if (err != 0) {
  210. pr_err("%s - error Exiting MAX86900_TEST_MODE!\n",
  211. __func__);
  212. return -EIO;
  213. }
  214. /* ALC OFF end */
  215. }
  216. pr_info("%s done, part_type = %u\n", __func__, data->part_type);
  217. return 0;
  218. }
  219. static int max86900_enable(struct max86900_device_data *data)
  220. {
  221. int err;
  222. data->led = 0;
  223. data->sample_cnt = 0;
  224. data->ir_sum = 0;
  225. data->r_sum = 0;
  226. mutex_lock(&data->activelock);
  227. err = max86900_write_reg(data, MAX86900_LED_CONFIGURATION, data->default_current);
  228. if (err != 0) {
  229. pr_err("%s - error initializing MAX86900_LED_CONFIGURATION!\n",
  230. __func__);
  231. mutex_unlock(&data->activelock);
  232. return -EIO;
  233. }
  234. err = max86900_write_reg(data, MAX86900_FIFO_WRITE_POINTER, 0x00);
  235. if (err != 0) {
  236. pr_err("%s - error initializing MAX86900_FIFO_WRITE_POINTER!\n",
  237. __func__);
  238. mutex_unlock(&data->activelock);
  239. return -EIO;
  240. }
  241. err = max86900_write_reg(data, MAX86900_OVF_COUNTER, 0x00);
  242. if (err != 0) {
  243. pr_err("%s - error initializing MAX86900_OVF_COUNTER!\n",
  244. __func__);
  245. mutex_unlock(&data->activelock);
  246. return -EIO;
  247. }
  248. err = max86900_write_reg(data, MAX86900_FIFO_READ_POINTER, 0x00);
  249. if (err != 0) {
  250. pr_err("%s - error initializing MAX86900_FIFO_READ_POINTER!\n",
  251. __func__);
  252. mutex_unlock(&data->activelock);
  253. return -EIO;
  254. }
  255. err = max86900_write_reg(data, MAX86900_MODE_CONFIGURATION, 0x0B);
  256. if (err != 0) {
  257. printk("%s - error initializing MAX86900_MODE_CONFIGURATION!\n",
  258. __func__);
  259. mutex_unlock(&data->activelock);
  260. return -EIO;
  261. }
  262. enable_irq(data->irq);
  263. mutex_unlock(&data->activelock);
  264. return 0;
  265. }
  266. static int max86900_disable(struct max86900_device_data *data)
  267. {
  268. u8 err;
  269. mutex_lock(&data->activelock);
  270. disable_irq(data->irq);
  271. err = max86900_write_reg(data, MAX86900_MODE_CONFIGURATION, 0x40);
  272. if (err != 0) {
  273. pr_err("%s - error initializing MAX86900_MODE_CONFIGURATION!\n",
  274. __func__);
  275. mutex_unlock(&data->activelock);
  276. return -EIO;
  277. }
  278. err = max86900_write_reg(data, MAX86900_MODE_CONFIGURATION, 0x80);
  279. if (err != 0) {
  280. pr_err("%s - error initializing MAX86900_MODE_CONFIGURATION!\n",
  281. __func__);
  282. mutex_unlock(&data->activelock);
  283. return -EIO;
  284. }
  285. mutex_unlock(&data->activelock);
  286. return 0;
  287. }
  288. static int max86900_read_temperature(struct max86900_device_data *data)
  289. {
  290. u8 recvData[2] = { 0x00, };
  291. int err;
  292. recvData[0] = MAX86900_TEMP_INTEGER;
  293. err = max86900_read_reg(data, recvData, 2);
  294. if (err != 0) {
  295. pr_err("%s max86900_read_reg err:%d, address:0x%02x\n",
  296. __func__, err, recvData[0]);
  297. return -EIO;
  298. }
  299. data->hrm_temp = ((char)recvData[0]) * 16 + recvData[1];
  300. pr_info("%s - %d(%x, %x)\n", __func__, data->hrm_temp, recvData[0], recvData[1]);
  301. return 0;
  302. }
  303. static int max86900_eol_test_control(struct max86900_device_data *data)
  304. {
  305. int err = 0;
  306. u8 led_current = 0;
  307. if (data->sample_cnt < data->hr_range2) {
  308. data->hr_range = 1;
  309. } else if (data->sample_cnt < (data->hr_range2 + 297)) {
  310. /* Fake pulse */
  311. if (data->sample_cnt % 8 < 4) {
  312. data->test_current_ir++;
  313. data->test_current_red++;
  314. } else {
  315. data->test_current_ir--;
  316. data->test_current_red--;
  317. }
  318. led_current = (data->test_current_red << 4) | data->test_current_ir;
  319. err = max86900_write_reg(data, MAX86900_LED_CONFIGURATION,
  320. led_current);
  321. if (err != 0) {
  322. pr_err("%s - error initializing\
  323. MAX86900_LED_CONFIGURATION!\n", __func__);
  324. return -EIO;
  325. }
  326. data->hr_range = 2;
  327. } else if (data->sample_cnt == (data->hr_range2 + 297)) {
  328. /* Measure */
  329. err = max86900_write_reg(data, MAX86900_LED_CONFIGURATION, data->led_current);
  330. if (err != 0) {
  331. pr_err("%s - error initializing\
  332. MAX86900_LED_CONFIGURATION!\n", __func__);
  333. return -EIO;
  334. }
  335. /* 400Hz setting */
  336. err = max86900_write_reg(data, MAX86900_SPO2_CONFIGURATION, 0x51);
  337. if (err != 0) {
  338. pr_err("%s - error initializing\
  339. MAX86900_SPO2_CONFIGURATION!\n", __func__);
  340. return -EIO;
  341. }
  342. } else if (data->sample_cnt < ((data->hr_range2 + 297) + 400 * 10)) {
  343. data->hr_range = 3;
  344. } else if (data->sample_cnt == ((data->hr_range2 + 297) + 400 * 10)) {
  345. err = max86900_write_reg(data, MAX86900_LED_CONFIGURATION,
  346. data->default_current);
  347. if (err != 0) {
  348. pr_err("%s - error initializing\
  349. MAX86900_LED_CONFIGURATION!\n", __func__);
  350. return -EIO;
  351. }
  352. /* 100Hz setting */
  353. err = max86900_write_reg(data, MAX86900_SPO2_CONFIGURATION, 0x47);
  354. if (err != 0) {
  355. pr_err("%s - error initializing\
  356. MAX86900_SPO2_CONFIGURATION!\n", __func__);
  357. return -EIO;
  358. }
  359. }
  360. data->sample_cnt++;
  361. return 0;
  362. }
  363. static int max86900_read_data(struct max86900_device_data *device, u16 *data)
  364. {
  365. u8 err;
  366. u8 recvData[4] = { 0x00, };
  367. int i;
  368. int ret = 0;
  369. if (device->sample_cnt == MAX86900_COUNT_MAX)
  370. device->sample_cnt = 0;
  371. recvData[0] = MAX86900_FIFO_DATA;
  372. if ((err = max86900_read_reg(device, recvData, 4)) != 0) {
  373. pr_err("%s max86900_read_reg err:%d, address:0x%02x\n",
  374. __func__, err, recvData[0]);
  375. return -EIO;
  376. }
  377. for (i = 0; i < 2; i++) {
  378. data[i] = ((((u16)recvData[i * 2]) << 8 ) & 0xff00)
  379. | ((((u16)recvData[i * 2 + 1])) & 0x00ff);
  380. }
  381. data[2] = device->led;
  382. if ((device->sample_cnt % 1000) == 1)
  383. pr_info("%s - %u, %u, %u, %u\n", __func__,
  384. data[0], data[1], data[2], data[3]);
  385. if (device->sample_cnt == 20 && device->led == 0) {
  386. err = max86900_read_temperature(device);
  387. if (err < 0) {
  388. pr_err("%s - max86900_read_temperature err : %d\n",
  389. __func__, err);
  390. return -EIO;
  391. }
  392. }
  393. if (device->eol_test_is_enable) {
  394. err = max86900_eol_test_control(device);
  395. if (err < 0) {
  396. pr_err("%s - max86900_eol_test_control err : %d\n",
  397. __func__, err);
  398. return -EIO;
  399. }
  400. } else {
  401. device->ir_sum += data[0];
  402. device->r_sum += data[1];
  403. if ((device->sample_cnt % SAMPLE_RATE) == SAMPLE_RATE - 1) {
  404. data[0] = device->ir_sum / SAMPLE_RATE;
  405. data[1] = device->r_sum / SAMPLE_RATE;
  406. device->ir_sum = 0;
  407. device->r_sum = 0;
  408. ret = 0;
  409. } else
  410. ret = 1;
  411. if (device->sample_cnt++ > 100 && device->led == 0) {
  412. device->led = 1;
  413. }
  414. }
  415. return ret;
  416. }
  417. void max86900_mode_enable(struct max86900_device_data *data, int onoff)
  418. {
  419. int err;
  420. if (onoff) {
  421. if (data->sub_ldo4 != NULL) {
  422. err = max86900_regulator_onoff(data, HRM_LDO_ON);
  423. if (err < 0)
  424. pr_err("%s max86900_regulator_on fail err = %d\n",
  425. __func__, err);
  426. usleep_range(1000, 1100);
  427. err = max86900_init_device(data);
  428. if (err)
  429. pr_err("%s max86900_init device fail err = %d\n",
  430. __func__, err);
  431. }
  432. err = max86900_enable(data);
  433. if (err != 0)
  434. pr_err("max86900_enable err : %d\n", err);
  435. atomic_set(&data->is_enable, 1);
  436. } else {
  437. err = max86900_disable(data);
  438. if (err != 0)
  439. pr_err("max86900_disable err : %d\n", err);
  440. if (data->sub_ldo4 != NULL) {
  441. err = max86900_regulator_onoff(data, HRM_LDO_OFF);
  442. if (err < 0)
  443. pr_err("%s max86900_regulator_off fail err = %d\n",
  444. __func__, err);
  445. }
  446. atomic_set(&data->is_enable, 0);
  447. }
  448. pr_info("%s - part_type = %u, onoff = %d\n", __func__, data->part_type, onoff);
  449. }
  450. /* sysfs */
  451. static ssize_t max86900_enable_show(struct device *dev,
  452. struct device_attribute *attr, char *buf)
  453. {
  454. struct max86900_device_data *data = dev_get_drvdata(dev);
  455. return sprintf(buf, "%d\n", atomic_read(&data->is_enable));
  456. }
  457. static ssize_t max86900_enable_store(struct device *dev,
  458. struct device_attribute *attr, const char *buf, size_t count)
  459. {
  460. struct max86900_device_data *data = dev_get_drvdata(dev);
  461. int new_value;
  462. if (sysfs_streq(buf, "1"))
  463. new_value = 1;
  464. else if (sysfs_streq(buf, "0"))
  465. new_value = 0;
  466. else {
  467. pr_err("%s - invalid value %d\n", __func__, *buf);
  468. return -EINVAL;
  469. }
  470. max86900_mode_enable(data, new_value);
  471. return count;
  472. }
  473. static ssize_t max86900_poll_delay_show(struct device *dev,
  474. struct device_attribute *attr, char *buf)
  475. {
  476. return sprintf(buf, "%lld\n", 10000000LL);
  477. }
  478. static ssize_t max86900_poll_delay_store(struct device *dev,
  479. struct device_attribute *attr,
  480. const char *buf, size_t size)
  481. {
  482. pr_info("%s - max86900 hrm sensor delay was fixed as 10ms\n", __func__);
  483. return size;
  484. }
  485. static DEVICE_ATTR(enable, S_IRUGO|S_IWUSR|S_IWGRP,
  486. max86900_enable_show, max86900_enable_store);
  487. static DEVICE_ATTR(poll_delay, S_IRUGO|S_IWUSR|S_IWGRP,
  488. max86900_poll_delay_show, max86900_poll_delay_store);
  489. static struct attribute *hrm_sysfs_attrs[] = {
  490. &dev_attr_enable.attr,
  491. &dev_attr_poll_delay.attr,
  492. NULL
  493. };
  494. static struct attribute_group hrm_attribute_group = {
  495. .attrs = hrm_sysfs_attrs,
  496. };
  497. /* test sysfs */
  498. static int max86900_set_led_current(struct max86900_device_data *data)
  499. {
  500. int err;
  501. err = max86900_write_reg(data, MAX86900_LED_CONFIGURATION,
  502. data->led_current);
  503. if (err != 0) {
  504. pr_err("%s - error initializing MAX86900_LED_CONFIGURATION!\n",
  505. __func__);
  506. return -EIO;
  507. }
  508. pr_info("%s - led current = %u\n", __func__, data->led_current);
  509. return 0;
  510. }
  511. static int max86900_set_hr_range(struct max86900_device_data *data)
  512. {
  513. pr_info("%s - hr_range = %u(0x%x)\n", __func__,
  514. data->hr_range, data->hr_range);
  515. return 0;
  516. }
  517. static int max86900_set_hr_range2(struct max86900_device_data *data)
  518. {
  519. pr_info("%s - hr_range2 = %u\n", __func__, data->hr_range2);
  520. return 0;
  521. }
  522. static int max86900_set_look_mode_ir(struct max86900_device_data *data)
  523. {
  524. pr_info("%s - look mode ir = %u\n", __func__, data->look_mode_ir);
  525. return 0;
  526. }
  527. static int max86900_set_look_mode_red(struct max86900_device_data *data)
  528. {
  529. pr_info("%s - look mode red = %u\n", __func__, data->look_mode_red);
  530. return 0;
  531. }
  532. static int max86900_eol_test_enable(struct max86900_device_data *data)
  533. {
  534. int err;
  535. u8 led_current;
  536. data->led = 1; /* Prevent resetting MAX86900_LED_CONFIGURATION */
  537. data->sample_cnt = 0;
  538. pr_info("%s\n", __func__);
  539. mutex_lock(&data->activelock);
  540. /* Test Mode Setting Start */
  541. data->hr_range = 0; /* Set test phase as 0 */
  542. data->eol_test_status = 0;
  543. data->test_current_ir = data->look_mode_ir;
  544. data->test_current_red = data->look_mode_red;
  545. led_current = (data->test_current_red << 4) | data->test_current_ir;
  546. err = max86900_write_reg(data, MAX86900_LED_CONFIGURATION, led_current);
  547. if (err != 0) {
  548. pr_err("%s - error initializing MAX86900_LED_CONFIGURATION!\n",
  549. __func__);
  550. mutex_unlock(&data->activelock);
  551. return -EIO;
  552. }
  553. err = max86900_write_reg(data, MAX86900_SPO2_CONFIGURATION, 0x47); //100Hz 1600us
  554. if (err != 0) {
  555. pr_err("%s - error initializing MAX86900_SPO2_CONFIGURATION!\n",
  556. __func__);
  557. return -EIO;
  558. }
  559. /* Clear FIFO */
  560. err = max86900_write_reg(data, MAX86900_FIFO_WRITE_POINTER, 0x00);
  561. if (err != 0) {
  562. pr_err("%s - error initializing MAX86900_FIFO_WRITE_POINTER!\n",
  563. __func__);
  564. mutex_unlock(&data->activelock);
  565. return -EIO;
  566. }
  567. err = max86900_write_reg(data, MAX86900_OVF_COUNTER, 0x00);
  568. if (err != 0) {
  569. pr_err("%s - error initializing MAX86900_OVF_COUNTER!\n",
  570. __func__);
  571. mutex_unlock(&data->activelock);
  572. return -EIO;
  573. }
  574. err = max86900_write_reg(data, MAX86900_FIFO_READ_POINTER, 0x00);
  575. if (err != 0) {
  576. pr_err("%s - error initializing MAX86900_FIFO_READ_POINTER!\n",
  577. __func__);
  578. mutex_unlock(&data->activelock);
  579. return -EIO;
  580. }
  581. /* Shutdown Clear */
  582. err = max86900_write_reg(data, MAX86900_MODE_CONFIGURATION, 0x0B);
  583. if (err != 0) {
  584. pr_err("%s - error initializing MAX86900_MODE_CONFIGURATION!\n",
  585. __func__);
  586. mutex_unlock(&data->activelock);
  587. return -EIO;
  588. }
  589. mutex_unlock(&data->activelock);
  590. return 0;
  591. }
  592. static void max86900_eol_test_onoff(struct max86900_device_data *data, int onoff)
  593. {
  594. int err;
  595. if (onoff) {
  596. err = max86900_eol_test_enable(data);
  597. data->eol_test_is_enable = 1;
  598. if (err != 0)
  599. pr_err("max86900_eol_test_enable err : %d\n", err);
  600. } else {
  601. pr_info("%s - eol test off\n", __func__);
  602. err = max86900_disable(data);
  603. if (err != 0)
  604. pr_err("max86900_disable err : %d\n", err);
  605. data->hr_range = 0;
  606. data->led_current = data->default_current;
  607. err = max86900_init_device(data);
  608. if (err)
  609. pr_err("%s max86900_init device fail err = %d", __func__, err);
  610. err = max86900_enable(data);
  611. if (err != 0)
  612. pr_err("max86900_enable err : %d\n", err);
  613. data->eol_test_is_enable = 0;
  614. }
  615. pr_info("%s - onoff = %d\n", __func__, onoff);
  616. }
  617. static ssize_t max86900_name_show(struct device *dev,
  618. struct device_attribute *attr, char *buf)
  619. {
  620. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_NAME);
  621. }
  622. static ssize_t max86900_vendor_show(struct device *dev,
  623. struct device_attribute *attr, char *buf)
  624. {
  625. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR);
  626. }
  627. static ssize_t led_current_store(struct device *dev,
  628. struct device_attribute *attr, const char *buf, size_t size)
  629. {
  630. int err;
  631. struct max86900_device_data *data = dev_get_drvdata(dev);
  632. err = kstrtou8(buf, 10, &data->led_current);
  633. if (err < 0)
  634. return err;
  635. err = max86900_set_led_current(data);
  636. if (err < 0)
  637. return err;
  638. data->default_current = data->led_current;
  639. return size;
  640. }
  641. static ssize_t led_current_show(struct device *dev,
  642. struct device_attribute *attr, char *buf)
  643. {
  644. struct max86900_device_data *data = dev_get_drvdata(dev);
  645. pr_info("max86900_%s - led_current = %u\n", __func__, data->led_current);
  646. return snprintf(buf, PAGE_SIZE, "%u\n", data->led_current);
  647. }
  648. static ssize_t hr_range_store(struct device *dev,
  649. struct device_attribute *attr, const char *buf, size_t size)
  650. {
  651. int err;
  652. struct max86900_device_data *data = dev_get_drvdata(dev);
  653. err = kstrtou8(buf, 10, &data->hr_range);
  654. if (err < 0)
  655. return err;
  656. err = max86900_set_hr_range(data);
  657. if (err < 0)
  658. return err;
  659. return size;
  660. }
  661. static ssize_t hr_range_show(struct device *dev,
  662. struct device_attribute *attr, char *buf)
  663. {
  664. struct max86900_device_data *data = dev_get_drvdata(dev);
  665. pr_info("max86900_%s - hr_range = %x\n", __func__, data->hr_range);
  666. return snprintf(buf, PAGE_SIZE, "%u\n", data->hr_range);
  667. }
  668. static ssize_t hr_range2_store(struct device *dev,
  669. struct device_attribute *attr, const char *buf, size_t size)
  670. {
  671. int err;
  672. struct max86900_device_data *data = dev_get_drvdata(dev);
  673. err = kstrtou8(buf, 10, &data->hr_range2);
  674. if (err < 0)
  675. return err;
  676. err = max86900_set_hr_range2(data);
  677. if (err < 0)
  678. return err;
  679. return size;
  680. }
  681. static ssize_t hr_range2_show(struct device *dev,
  682. struct device_attribute *attr, char *buf)
  683. {
  684. struct max86900_device_data *data = dev_get_drvdata(dev);
  685. pr_info("max86900_%s - hr_range2 = %x\n", __func__, data->hr_range2);
  686. return snprintf(buf, PAGE_SIZE, "%u\n", data->hr_range2);
  687. }
  688. static ssize_t look_mode_ir_store(struct device *dev,
  689. struct device_attribute *attr, const char *buf, size_t size)
  690. {
  691. int err;
  692. struct max86900_device_data *data = dev_get_drvdata(dev);
  693. err = kstrtou8(buf, 10, &data->look_mode_ir);
  694. if (err < 0)
  695. return err;
  696. err = max86900_set_look_mode_ir(data);
  697. if (err < 0)
  698. return err;
  699. return size;
  700. }
  701. static ssize_t look_mode_ir_show(struct device *dev,
  702. struct device_attribute *attr, char *buf)
  703. {
  704. struct max86900_device_data *data = dev_get_drvdata(dev);
  705. pr_info("max86900_%s - look_mode_ir = %x\n", __func__, data->look_mode_ir);
  706. return snprintf(buf, PAGE_SIZE, "%u\n", data->look_mode_ir);
  707. }
  708. static ssize_t look_mode_red_store(struct device *dev,
  709. struct device_attribute *attr, const char *buf, size_t size)
  710. {
  711. int err;
  712. struct max86900_device_data *data = dev_get_drvdata(dev);
  713. err = kstrtou8(buf, 10, &data->look_mode_red);
  714. if (err < 0)
  715. return err;
  716. err = max86900_set_look_mode_red(data);
  717. if (err < 0)
  718. return err;
  719. return size;
  720. }
  721. static ssize_t look_mode_red_show(struct device *dev,
  722. struct device_attribute *attr, char *buf)
  723. {
  724. struct max86900_device_data *data = dev_get_drvdata(dev);
  725. pr_info("max86900_%s - look_mode_red = %x\n", __func__, data->look_mode_red);
  726. return snprintf(buf, PAGE_SIZE, "%u\n", data->look_mode_red);
  727. }
  728. static ssize_t eol_test_store(struct device *dev,
  729. struct device_attribute *attr, const char *buf, size_t size)
  730. {
  731. int test_onoff;
  732. struct max86900_device_data *data = dev_get_drvdata(dev);
  733. if (sysfs_streq(buf, "1")) /* eol_test start */
  734. test_onoff = 1;
  735. else if (sysfs_streq(buf, "0")) /* eol_test stop */
  736. test_onoff = 0;
  737. else {
  738. pr_debug("max86900_%s: invalid value %d\n", __func__, *buf);
  739. return -EINVAL;
  740. }
  741. max86900_eol_test_onoff(data, test_onoff);
  742. return size;
  743. }
  744. static ssize_t eol_test_show(struct device *dev,
  745. struct device_attribute *attr, char *buf)
  746. {
  747. struct max86900_device_data *data = dev_get_drvdata(dev);
  748. return snprintf(buf, PAGE_SIZE, "%u\n", data->eol_test_is_enable);
  749. }
  750. static ssize_t eol_test_result_store(struct device *dev,
  751. struct device_attribute *attr, const char *buf, size_t size)
  752. {
  753. struct max86900_device_data *data = dev_get_drvdata(dev);
  754. unsigned int buf_len;
  755. buf_len = strlen(buf) + 1;
  756. if (buf_len > MAX_EOL_RESULT)
  757. buf_len = MAX_EOL_RESULT;
  758. mutex_lock(&data->storelock);
  759. if (data->eol_test_result != NULL)
  760. kfree(data->eol_test_result);
  761. data->eol_test_result = kzalloc(sizeof(char) * buf_len, GFP_KERNEL);
  762. if (data->eol_test_result == NULL) {
  763. pr_err("max86900_%s - couldn't allocate memory\n",
  764. __func__);
  765. mutex_unlock(&data->storelock);
  766. return -ENOMEM;
  767. }
  768. strncpy(data->eol_test_result, buf, buf_len);
  769. mutex_unlock(&data->storelock);
  770. pr_info("max86900_%s - result = %s, buf_len(%u)\n", __func__, data->eol_test_result, buf_len);
  771. data->eol_test_status = 1;
  772. return size;
  773. }
  774. static ssize_t eol_test_result_show(struct device *dev,
  775. struct device_attribute *attr, char *buf)
  776. {
  777. struct max86900_device_data *data = dev_get_drvdata(dev);
  778. if (data->eol_test_result == NULL) {
  779. pr_info("max86900_%s - data->eol_test_result is NULL\n", __func__);
  780. data->eol_test_status = 0;
  781. return snprintf(buf, PAGE_SIZE, "%s\n", "NO_EOL_TEST");
  782. }
  783. pr_info("max86900_%s - result = %s\n", __func__, data->eol_test_result);
  784. data->eol_test_status = 0;
  785. return snprintf(buf, PAGE_SIZE, "%s\n", data->eol_test_result);
  786. }
  787. static ssize_t eol_test_status_show(struct device *dev,
  788. struct device_attribute *attr, char *buf)
  789. {
  790. struct max86900_device_data *data = dev_get_drvdata(dev);
  791. return snprintf(buf, PAGE_SIZE, "%u\n", data->eol_test_status);
  792. }
  793. static ssize_t int_pin_check(struct device *dev,
  794. struct device_attribute *attr, char *buf)
  795. {
  796. struct max86900_device_data *data = dev_get_drvdata(dev);
  797. int err = -1;
  798. int pin_state = -1;
  799. u8 recvData;
  800. /* DEVICE Power-up */
  801. err = max86900_regulator_onoff(data, HRM_LDO_ON);
  802. if (err < 0) {
  803. pr_err("max86900_%s - regulator on fail\n", __func__);
  804. goto exit;
  805. }
  806. usleep_range(1000, 1100);
  807. /* check INT pin state */
  808. pin_state = gpio_get_value_cansleep(data->hrm_int);
  809. if (pin_state) {
  810. pr_err("max86900_%s - INT pin state is high before INT clear\n", __func__);
  811. err = -1;
  812. max86900_regulator_onoff(data, HRM_LDO_OFF);
  813. goto exit;
  814. }
  815. pr_info("max86900_%s - Before INT clear %d\n", __func__, pin_state);
  816. /* Interrupt Clear */
  817. recvData = MAX86900_INTERRUPT_STATUS;
  818. if ((err = max86900_read_reg(data, &recvData, 1)) != 0) {
  819. pr_err("max86900_%s - max86900_read_reg err:%d, address:0x%02x\n",
  820. __func__, err, recvData);
  821. max86900_regulator_onoff(data, HRM_LDO_OFF);
  822. goto exit;
  823. }
  824. /* check INT pin state */
  825. pin_state = gpio_get_value_cansleep(data->hrm_int);
  826. if (!pin_state) {
  827. pr_err("max86900_%s - INT pin state is low after INT clear\n", __func__);
  828. err = -1;
  829. max86900_regulator_onoff(data, HRM_LDO_OFF);
  830. goto exit;
  831. }
  832. pr_info("max86900_%s - After INT clear %d\n", __func__, pin_state);
  833. err = max86900_regulator_onoff(data, HRM_LDO_OFF);
  834. if (err < 0)
  835. pr_err("max86900_%s - regulator off fail\n", __func__);
  836. pr_info("max86900_%s - success\n", __func__);
  837. exit:
  838. return snprintf(buf, PAGE_SIZE, "%d\n", err);
  839. }
  840. static ssize_t max86900_lib_ver_store(struct device *dev,
  841. struct device_attribute *attr, const char *buf, size_t size)
  842. {
  843. struct max86900_device_data *data = dev_get_drvdata(dev);
  844. unsigned int buf_len;
  845. buf_len = strlen(buf) + 1;
  846. if (buf_len > MAX_LIB_VER)
  847. buf_len = MAX_LIB_VER;
  848. mutex_lock(&data->storelock);
  849. if (data->lib_ver != NULL)
  850. kfree(data->lib_ver);
  851. data->lib_ver = kzalloc(sizeof(char) * buf_len, GFP_KERNEL);
  852. if (data->lib_ver == NULL) {
  853. pr_err("%s - couldn't allocate memory\n", __func__);
  854. mutex_unlock(&data->storelock);
  855. return -ENOMEM;
  856. }
  857. strncpy(data->lib_ver, buf, buf_len);
  858. mutex_unlock(&data->storelock);
  859. pr_info("%s - lib_ver = %s\n", __func__, data->lib_ver);
  860. return size;
  861. }
  862. static ssize_t max86900_lib_ver_show(struct device *dev,
  863. struct device_attribute *attr, char *buf)
  864. {
  865. struct max86900_device_data *data = dev_get_drvdata(dev);
  866. if (data->lib_ver == NULL) {
  867. pr_info("%s - data->lib_ver is NULL\n", __func__);
  868. return snprintf(buf, PAGE_SIZE, "%s\n", "NULL");
  869. }
  870. pr_info("%s - lib_ver = %s\n", __func__, data->lib_ver);
  871. return snprintf(buf, PAGE_SIZE, "%s\n", data->lib_ver);
  872. }
  873. static DEVICE_ATTR(name, S_IRUGO, max86900_name_show, NULL);
  874. static DEVICE_ATTR(vendor, S_IRUGO, max86900_vendor_show, NULL);
  875. static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR | S_IWGRP,
  876. led_current_show, led_current_store);
  877. static DEVICE_ATTR(hr_range, S_IRUGO | S_IWUSR | S_IWGRP,
  878. hr_range_show, hr_range_store);
  879. static DEVICE_ATTR(hr_range2, S_IRUGO | S_IWUSR | S_IWGRP,
  880. hr_range2_show, hr_range2_store);
  881. static DEVICE_ATTR(look_mode_ir, S_IRUGO | S_IWUSR | S_IWGRP,
  882. look_mode_ir_show, look_mode_ir_store);
  883. static DEVICE_ATTR(look_mode_red, S_IRUGO | S_IWUSR | S_IWGRP,
  884. look_mode_red_show, look_mode_red_store);
  885. static DEVICE_ATTR(eol_test, S_IRUGO | S_IWUSR | S_IWGRP,
  886. eol_test_show, eol_test_store);
  887. static DEVICE_ATTR(eol_test_result, S_IRUGO | S_IWUSR | S_IWGRP,
  888. eol_test_result_show, eol_test_result_store);
  889. static DEVICE_ATTR(eol_test_status, S_IRUGO, eol_test_status_show, NULL);
  890. static DEVICE_ATTR(int_pin_check, S_IRUGO, int_pin_check, NULL);
  891. static DEVICE_ATTR(lib_ver, S_IRUGO | S_IWUSR | S_IWGRP,
  892. max86900_lib_ver_show, max86900_lib_ver_store);
  893. static struct device_attribute *hrm_sensor_attrs[] = {
  894. &dev_attr_name,
  895. &dev_attr_vendor,
  896. &dev_attr_led_current,
  897. &dev_attr_hr_range,
  898. &dev_attr_hr_range2,
  899. &dev_attr_look_mode_ir,
  900. &dev_attr_look_mode_red,
  901. &dev_attr_eol_test,
  902. &dev_attr_eol_test_result,
  903. &dev_attr_eol_test_status,
  904. &dev_attr_int_pin_check,
  905. &dev_attr_lib_ver,
  906. NULL,
  907. };
  908. irqreturn_t max86900_irq_handler(int irq, void *device)
  909. {
  910. int err;
  911. struct max86900_device_data *data = device;
  912. u16 raw_data[4] = {0x00, };
  913. err = max86900_read_data(data, raw_data);
  914. if (err < 0)
  915. pr_err("max86900_read_data err : %d\n", err);
  916. if (err == 0) {
  917. input_report_rel(data->hrm_input_dev, REL_X, raw_data[0] + 1); /* IR */
  918. input_report_rel(data->hrm_input_dev, REL_Y, raw_data[1] + 1); /* RED */
  919. input_report_rel(data->hrm_input_dev, REL_Z, data->hrm_temp + 1);
  920. input_sync(data->hrm_input_dev);
  921. }
  922. return IRQ_HANDLED;
  923. }
  924. #if defined(CONFIG_MACH_KLTE_KDI)
  925. extern unsigned int system_rev;
  926. #endif
  927. static int max86900_parse_dt(struct max86900_device_data *data,
  928. struct device *dev)
  929. {
  930. struct device_node *dNode = dev->of_node;
  931. enum of_gpio_flags flags;
  932. if (dNode == NULL)
  933. return -ENODEV;
  934. #if defined(CONFIG_MACH_KLTE_KDI)
  935. if(system_rev==11)
  936. data->hrm_int = of_get_named_gpio_flags(dNode,
  937. "max86900,hrm_int-kdi-gpio", 0, &flags);
  938. else
  939. data->hrm_int = of_get_named_gpio_flags(dNode,
  940. "max86900,hrm_int-gpio", 0, &flags);
  941. #else
  942. data->hrm_int = of_get_named_gpio_flags(dNode,
  943. "max86900,hrm_int-gpio", 0, &flags);
  944. #endif
  945. if (data->hrm_int < 0) {
  946. pr_err("%s - get hrm_int error\n", __func__);
  947. return -ENODEV;
  948. }
  949. if (of_property_read_string(dNode, "max86900,sub_ldo4", &data->sub_ldo4) < 0)
  950. pr_err("%s - get sub_ldo4 error\n", __func__);
  951. #if defined(CONFIG_SEC_KACTIVE_PROJECT) || defined(CONFIG_MACH_KSPORTSLTE_SPR) || defined(CONFIG_SEC_S_PROJECT)
  952. if (of_property_read_string(dNode, "max86900,led_l19", &data->led_l19) < 0)
  953. pr_err("%s - get led_l19 error\n", __func__);
  954. #endif
  955. return 0;
  956. }
  957. static int max86900_gpio_setup(struct max86900_device_data *data)
  958. {
  959. int errorno = -EIO;
  960. errorno = gpio_request(data->hrm_int, "hrm_int");
  961. if (errorno) {
  962. pr_err("%s - failed to request hrm_int\n", __func__);
  963. return errorno;
  964. }
  965. errorno = gpio_direction_input(data->hrm_int);
  966. if (errorno) {
  967. pr_err("%s - failed to set hrm_int as input\n", __func__);
  968. goto err_gpio_direction_input;
  969. }
  970. data->irq = gpio_to_irq(data->hrm_int);
  971. errorno = request_threaded_irq(data->irq, NULL,
  972. max86900_irq_handler, IRQF_TRIGGER_FALLING,
  973. "hrm_sensor_irq", data);
  974. if (errorno < 0) {
  975. pr_err("%s - request_irq(%d) failed for gpio %d (%d)\n",
  976. __func__, data->irq, data->hrm_int, errorno);
  977. errorno = -ENODEV;
  978. goto err_request_irq;
  979. }
  980. disable_irq(data->irq);
  981. goto done;
  982. err_request_irq:
  983. err_gpio_direction_input:
  984. gpio_free(data->hrm_int);
  985. done:
  986. return errorno;
  987. }
  988. int max86900_probe(struct i2c_client *client, const struct i2c_device_id *id )
  989. {
  990. int err = -ENODEV;
  991. u8 buffer[2] = {0, };
  992. struct max86900_device_data *data;
  993. /* check to make sure that the adapter supports I2C */
  994. if (!i2c_check_functionality(client->adapter,I2C_FUNC_I2C)) {
  995. pr_err("%s - I2C_FUNC_I2C not supported\n", __func__);
  996. return -ENODEV;
  997. }
  998. /* allocate some memory for the device */
  999. data = kzalloc(sizeof(struct max86900_device_data), GFP_KERNEL);
  1000. if (data == NULL) {
  1001. pr_err("%s - couldn't allocate memory\n", __func__);
  1002. return -ENOMEM;
  1003. }
  1004. data->client = client;
  1005. i2c_set_clientdata(client, data);
  1006. mutex_init(&data->i2clock);
  1007. mutex_init(&data->activelock);
  1008. mutex_init(&data->storelock);
  1009. data->dev = &client->dev;
  1010. pr_info("%s - start \n", __func__);
  1011. data->hr_range = 0;
  1012. err = max86900_parse_dt(data, &client->dev);
  1013. if (err < 0) {
  1014. pr_err("[SENSOR] %s - of_node error\n", __func__);
  1015. err = -ENODEV;
  1016. goto err_of_node;
  1017. }
  1018. if (data->sub_ldo4 != NULL) {
  1019. err = max86900_regulator_onoff(data, HRM_LDO_ON);
  1020. if (err < 0) {
  1021. pr_err("%s max86900_regulator_on fail err = %d\n",
  1022. __func__, err);
  1023. goto err_of_node;
  1024. }
  1025. }
  1026. usleep_range(1000, 1100);
  1027. data->client->addr = MAX86900A_SLAVE_ADDR;
  1028. buffer[0] = MAX86900_WHOAMI_REG;
  1029. err = max86900_read_reg(data, buffer, 2);
  1030. if (buffer[1] == MAX86900A_WHOAMI) {
  1031. /* MAX86900A & MAX86900B */
  1032. switch(buffer[0]) {
  1033. case MAX86900A_REV_ID:
  1034. data->part_type = PART_TYPE_MAX86900A;
  1035. data->default_current = MAX86900A_DEFAULT_CURRENT;
  1036. break;
  1037. case MAX86900B_REV_ID:
  1038. data->part_type = PART_TYPE_MAX86900B;
  1039. data->default_current = MAX86900A_DEFAULT_CURRENT;
  1040. break;
  1041. case MAX86900C_REV_ID:
  1042. data->part_type = PART_TYPE_MAX86900C;
  1043. data->default_current = MAX86900C_DEFAULT_CURRENT;
  1044. break;
  1045. default:
  1046. pr_err("%s WHOAMI read error : REV ID : 0x%02x\n",
  1047. __func__, buffer[0]);
  1048. err = -ENODEV;
  1049. goto err_of_read_chipid;
  1050. }
  1051. pr_info("%s - MAX86900 OS21(0x%X), REV ID : 0x%02x\n",
  1052. __func__, MAX86900A_SLAVE_ADDR, buffer[0]);
  1053. } else {
  1054. /* MAX86900 */
  1055. data->client->addr = MAX86900_SLAVE_ADDR;
  1056. buffer[0] = MAX86900_WHOAMI_REG;
  1057. err = max86900_read_reg(data, buffer, 2);
  1058. if (err) {
  1059. pr_err("%s WHOAMI read fail\n", __func__);
  1060. err = -ENODEV;
  1061. goto err_of_read_chipid;
  1062. }
  1063. data->part_type = PART_TYPE_MAX86900;
  1064. data->default_current = MAX86900_DEFAULT_CURRENT;
  1065. pr_info("%s - MAX86900 OS20 (0x%X)\n", __func__,
  1066. MAX86900_SLAVE_ADDR);
  1067. }
  1068. data->led_current = data->default_current;
  1069. /* allocate input device */
  1070. data->hrm_input_dev = input_allocate_device();
  1071. if (!data->hrm_input_dev) {
  1072. pr_err("%s - could not allocate input device\n",
  1073. __func__);
  1074. goto err_input_allocate_device;
  1075. }
  1076. input_set_drvdata(data->hrm_input_dev, data);
  1077. data->hrm_input_dev->name = MODULE_NAME_HRM;
  1078. input_set_capability(data->hrm_input_dev, EV_REL, REL_X);
  1079. input_set_capability(data->hrm_input_dev, EV_REL, REL_Y);
  1080. input_set_capability(data->hrm_input_dev, EV_REL, REL_Z);
  1081. err = input_register_device(data->hrm_input_dev);
  1082. if (err < 0) {
  1083. input_free_device(data->hrm_input_dev);
  1084. pr_err("%s - could not register input device\n", __func__);
  1085. goto err_input_register_device;
  1086. }
  1087. err = sensors_create_symlink(&data->hrm_input_dev->dev.kobj,
  1088. data->hrm_input_dev->name);
  1089. if (err < 0) {
  1090. pr_err("%s - create_symlink error\n", __func__);
  1091. goto err_sensors_create_symlink;
  1092. }
  1093. err = sysfs_create_group(&data->hrm_input_dev->dev.kobj,
  1094. &hrm_attribute_group);
  1095. if (err) {
  1096. pr_err("[SENSOR] %s - could not create sysfs group\n",
  1097. __func__);
  1098. goto err_sysfs_create_group;
  1099. }
  1100. err = max86900_gpio_setup(data);
  1101. if (err) {
  1102. pr_err("[SENSOR] %s - could not setup gpio\n", __func__);
  1103. goto err_setup_gpio;
  1104. }
  1105. /* set sysfs for hrm sensor */
  1106. err = sensors_register(data->dev, data, hrm_sensor_attrs,
  1107. "hrm_sensor");
  1108. if (err) {
  1109. pr_err("[SENSOR] %s - cound not register hrm_sensor(%d).\n",
  1110. __func__, err);
  1111. goto hrm_sensor_register_failed;
  1112. }
  1113. err = max86900_init_device(data);
  1114. if (err) {
  1115. pr_err("%s max86900_init device fail err = %d", __func__, err);
  1116. goto max86900_init_device_failed;
  1117. }
  1118. err = dev_set_drvdata(data->dev, data);
  1119. if (err) {
  1120. pr_err("%s dev_set_drvdata fail err = %d", __func__, err);
  1121. goto dev_set_drvdata_failed;
  1122. }
  1123. if (data->sub_ldo4 != NULL) {
  1124. err = max86900_regulator_onoff(data, HRM_LDO_OFF);
  1125. if (err < 0) {
  1126. pr_err("%s max86900_regulator_off fail(%d, %d)\n",
  1127. __func__, err, HRM_LDO_OFF);
  1128. goto dev_set_drvdata_failed;
  1129. }
  1130. }
  1131. pr_info("%s success\n", __func__);
  1132. goto done;
  1133. dev_set_drvdata_failed:
  1134. max86900_init_device_failed:
  1135. sensors_unregister(data->dev, hrm_sensor_attrs);
  1136. hrm_sensor_register_failed:
  1137. gpio_free(data->hrm_int);
  1138. err_setup_gpio:
  1139. err_sysfs_create_group:
  1140. sensors_remove_symlink(&data->hrm_input_dev->dev.kobj,
  1141. data->hrm_input_dev->name);
  1142. err_sensors_create_symlink:
  1143. input_unregister_device(data->hrm_input_dev);
  1144. err_input_register_device:
  1145. err_input_allocate_device:
  1146. err_of_read_chipid:
  1147. max86900_regulator_onoff(data, HRM_LDO_OFF);
  1148. err_of_node:
  1149. mutex_destroy(&data->i2clock);
  1150. mutex_destroy(&data->activelock);
  1151. kfree(data);
  1152. pr_err("%s failed\n", __func__);
  1153. done:
  1154. return err;
  1155. }
  1156. /*
  1157. * Remove function for this I2C driver.
  1158. */
  1159. int max86900_remove(struct i2c_client *client)
  1160. {
  1161. pr_info("%s\n", __func__);
  1162. free_irq(client->irq, NULL);
  1163. return 0;
  1164. }
  1165. static void max86900_shutdown(struct i2c_client *client)
  1166. {
  1167. pr_info("%s\n", __func__);
  1168. }
  1169. static int max86900_pm_suspend(struct device *dev)
  1170. {
  1171. struct max86900_device_data *data = dev_get_drvdata(dev);
  1172. if (atomic_read(&data->is_enable)) {
  1173. max86900_mode_enable(data, HRM_LDO_OFF);
  1174. atomic_set(&data->is_suspend, 1);
  1175. }
  1176. pr_info("%s\n", __func__);
  1177. return 0;
  1178. }
  1179. static int max86900_pm_resume(struct device *dev)
  1180. {
  1181. struct max86900_device_data *data = dev_get_drvdata(dev);
  1182. if (atomic_read(&data->is_suspend)) {
  1183. max86900_mode_enable(data, HRM_LDO_ON);
  1184. atomic_set(&data->is_suspend, 0);
  1185. }
  1186. pr_info("%s\n", __func__);
  1187. return 0;
  1188. }
  1189. static const struct dev_pm_ops max86900_pm_ops = {
  1190. .suspend = max86900_pm_suspend,
  1191. .resume = max86900_pm_resume
  1192. };
  1193. static struct of_device_id max86900_match_table[] = {
  1194. { .compatible = "max86900",},
  1195. {},
  1196. };
  1197. static const struct i2c_device_id max86900_device_id[] = {
  1198. { "max86900_match_table", 0 },
  1199. { }
  1200. };
  1201. /* descriptor of the max86900 I2C driver */
  1202. static struct i2c_driver max86900_i2c_driver =
  1203. {
  1204. .driver = {
  1205. .name = CHIP_NAME,
  1206. .owner = THIS_MODULE,
  1207. .pm = &max86900_pm_ops,
  1208. .of_match_table = max86900_match_table,
  1209. },
  1210. .probe = max86900_probe,
  1211. .shutdown = max86900_shutdown,
  1212. .remove = max86900_remove,
  1213. .id_table = max86900_device_id,
  1214. };
  1215. /* initialization and exit functions */
  1216. static int __init max86900_init(void)
  1217. {
  1218. return i2c_add_driver(&max86900_i2c_driver);
  1219. }
  1220. static void __exit max86900_exit(void)
  1221. {
  1222. i2c_del_driver(&max86900_i2c_driver);
  1223. }
  1224. module_init(max86900_init);
  1225. module_exit(max86900_exit);
  1226. MODULE_DESCRIPTION("max86900 Driver");
  1227. MODULE_AUTHOR("Samsung Electronics");
  1228. MODULE_LICENSE("GPL");