k303c_mag.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  1. /******************** (C) COPYRIGHT 2013 STMicroelectronics ********************
  2. *
  3. * File Name : k303c_mag.c
  4. * Authors : MSH - C&I BU - Application Team
  5. * : Matteo Dameno (matteo.dameno@st.com)
  6. * : Denis Ciocca (denis.ciocca@st.com)
  7. * : Both authors are willing to be considered the contact
  8. * : and update points for the driver.
  9. * Version : V.1.0.0
  10. * Date : 2013/Jun/17
  11. * Description : K303C magnetometer driver
  12. *
  13. ********************************************************************************
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License version 2 as
  17. * published by the Free Software Foundation.
  18. *
  19. * THE PRESENT SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
  20. * OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, FOR THE SOLE
  21. * PURPOSE TO SUPPORT YOUR APPLICATION DEVELOPMENT.
  22. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  23. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  24. * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  25. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  26. *
  27. ********************************************************************************
  28. *******************************************************************************/
  29. #include <linux/err.h>
  30. #include <linux/errno.h>
  31. #include <linux/delay.h>
  32. #include <linux/fs.h>
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/i2c.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/input.h>
  39. #include <linux/kernel.h>
  40. #include <linux/kobject.h>
  41. #include <linux/device.h>
  42. #include <linux/gpio.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/irq.h>
  45. #include <linux/hrtimer.h>
  46. #include <linux/ktime.h>
  47. #include <linux/regulator/consumer.h>
  48. #include <linux/of_gpio.h>
  49. #include <linux/wakelock.h>
  50. #include <linux/sensor/sensors_core.h>
  51. #define VENDOR_NAME "STM"
  52. #define MODEL_NAME "K303C"
  53. #define MODULE_NAME "magnetic_sensor"
  54. #define K303C_MAG_DEV_NAME "k303c_mag"
  55. #define I2C_AUTO_INCREMENT (0x80)
  56. #define MS_TO_NS(x) (x*1000000L)
  57. #define MAG_G_MAX_POS 983520 /** max positive value mag [ugauss] */
  58. #define MAG_G_MAX_NEG 983040 /** max negative value mag [ugauss] */
  59. #define FUZZ 0
  60. #define FLAT 0
  61. #define MAG_SELF_TEST_16G_MAX_LSB (-1711)
  62. #define MAG_SELF_TEST_16G_MIN_LSB (-5133)
  63. #define MAG_SELF_TEST_16G_MAX_LSB_Z (-171)
  64. #define MAG_SELF_TEST_16G_MIN_LSB_Z (-1711)
  65. #define K303C_MAG_MIN_POLL_PERIOD_MS 13
  66. /* Address registers */
  67. #define REG_WHOAMI_ADDR (0x0F) /** Who am i address register */
  68. #define REG_CNTRL1_ADDR (0x20) /** CNTRL1 address register */
  69. #define REG_CNTRL2_ADDR (0x21) /** CNTRL2 address register */
  70. #define REG_CNTRL3_ADDR (0x22) /** CNTRL3 address register */
  71. #define REG_CNTRL4_ADDR (0x23) /** CNTRL4 address register */
  72. #define REG_CNTRL5_ADDR (0x24) /** CNTRL5 address register */
  73. #define REG_MAG_DATA_ADDR (0x28) /** Mag. data low address register */
  74. /* Sensitivity */
  75. #define SENSITIVITY_MAG_4G 146156 /** ngauss/LSB */
  76. #define SENSITIVITY_MAG_8G 292312 /** ngauss/LSB */
  77. #define SENSITIVITY_MAG_10G 365364 /** ngauss/LSB */
  78. #define SENSITIVITY_MAG_16G 584454 /** ngauss/LSB */
  79. /* ODR */
  80. #define ODR_MAG_MASK (0X1C) /* Mask for odr change on mag */
  81. #define K303C_MAG_ODR0_625 (0x00) /* 0.625Hz output data rate */
  82. #define K303C_MAG_ODR1_25 (0x04) /* 1.25Hz output data rate */
  83. #define K303C_MAG_ODR2_5 (0x08) /* 2.5Hz output data rate */
  84. #define K303C_MAG_ODR5 (0x0C) /* 5Hz output data rate */
  85. #define K303C_MAG_ODR10 (0x10) /* 10Hz output data rate */
  86. #define K303C_MAG_ODR20 (0x14) /* 20Hz output data rate */
  87. #define K303C_MAG_ODR40 (0x18) /* 40Hz output data rate */
  88. #define K303C_MAG_ODR80 (0x1C) /* 80Hz output data rate */
  89. /* Magnetometer Sensor Full Scale */
  90. #define K303C_MAG_FS_MASK (0x60)
  91. #define K303C_MAG_FS_4G (0x00) /* Full scale 4 G */
  92. #define K303C_MAG_FS_8G (0x20) /* Full scale 8 G */
  93. #define K303C_MAG_FS_10G (0x40) /* Full scale 10 G */
  94. #define K303C_MAG_FS_16G (0x60) /* Full scale 16 G */
  95. /* Magnetic sensor mode */
  96. #define MSMS_MASK (0x03) /* Mask magnetic sensor mode */
  97. #define POWEROFF_MAG (0x02) /* Power Down */
  98. #define CONTINUOS_CONVERSION (0x00) /* Continuos Conversion */
  99. /* X and Y axis operative mode selection */
  100. #define X_Y_PERFORMANCE_MASK (0x60)
  101. #define X_Y_LOW_PERFORMANCE (0x00)
  102. #define X_Y_MEDIUM_PERFORMANCE (0x20)
  103. #define X_Y_HIGH_PERFORMANCE (0x40)
  104. #define X_Y_ULTRA_HIGH_PERFORMANCE (0x60)
  105. /* Z axis operative mode selection */
  106. #define Z_PERFORMANCE_MASK (0x0c)
  107. #define Z_LOW_PERFORMANCE (0x00)
  108. #define Z_MEDIUM_PERFORMANCE (0x04)
  109. #define Z_HIGH_PERFORMANCE (0x08)
  110. #define Z_ULTRA_HIGH_PERFORMANCE (0x0c)
  111. /* Default values loaded in probe function */
  112. #define WHOIAM_VALUE (0x3d) /** Who Am I default value */
  113. #define REG_DEF_CNTRL1 (0xE0) /** CNTRL1 default value */
  114. #define REG_DEF_CNTRL2 (0x60) /** CNTRL2 default value */
  115. #define REG_DEF_CNTRL3 (0x03) /** CNTRL3 default value */
  116. #define REG_DEF_CNTRL4 (0x0C) /** CNTRL4 default value */
  117. #define REG_DEF_CNTRL5 (0x40) /** CNTRL5 default value */
  118. #define REG_DEF_ALL_ZEROS (0x00)
  119. #define MAX_VDD 3000000
  120. #define AVG_VDD 2850000
  121. #define AVG_VIO 1800000
  122. struct {
  123. unsigned int cutoff_us;
  124. u8 value;
  125. } k303c_mag_odr_table[] = {
  126. { 12, K303C_MAG_ODR80},
  127. { 25, K303C_MAG_ODR40},
  128. { 50, K303C_MAG_ODR20},
  129. { 100, K303C_MAG_ODR10},
  130. { 200, K303C_MAG_ODR5},
  131. { 400, K303C_MAG_ODR2_5},
  132. { 800, K303C_MAG_ODR1_25},
  133. { 1600, K303C_MAG_ODR0_625},
  134. };
  135. struct interrupt_enable {
  136. atomic_t enable;
  137. u8 address;
  138. u8 mask;
  139. };
  140. struct interrupt_value {
  141. int value;
  142. u8 address;
  143. };
  144. struct k303c_mag_platform_data {
  145. unsigned int poll_interval;
  146. unsigned int min_interval;
  147. u8 fs_range;
  148. u8 axis_map_x;
  149. u8 axis_map_y;
  150. u8 axis_map_z;
  151. u8 negate_x;
  152. u8 negate_y;
  153. u8 negate_z;
  154. };
  155. struct k303c_mag_p {
  156. struct i2c_client *client;
  157. struct mutex lock;
  158. struct work_struct input_work_mag;
  159. struct workqueue_struct *mag_wq;
  160. struct hrtimer hr_timer_mag;
  161. ktime_t ktime_mag;
  162. struct device *factory_device;
  163. struct input_dev *input;
  164. int hw_initialized;
  165. /* hw_working=-1 means not tested yet */
  166. int hw_working;
  167. atomic_t enabled_mag;
  168. int on_before_suspend;
  169. int use_smbus;
  170. u32 sensitivity_mag;
  171. u8 xy_mode;
  172. u8 z_mode;
  173. struct regulator *reg_vdd;
  174. struct regulator *reg_vio;
  175. struct k303c_mag_platform_data *pdata;
  176. int (*power_onoff)(struct k303c_mag_p *data, bool onoff);
  177. };
  178. static const struct k303c_mag_platform_data default_k303c_mag_pdata = {
  179. .poll_interval = 100,
  180. .min_interval = K303C_MAG_MIN_POLL_PERIOD_MS,
  181. .fs_range = K303C_MAG_FS_16G,
  182. .axis_map_x = 0,
  183. .axis_map_y = 1,
  184. .axis_map_z = 2,
  185. .negate_x = 0,
  186. .negate_y = 0,
  187. .negate_z = 0,
  188. };
  189. struct reg_rw {
  190. u8 address;
  191. u8 default_value;
  192. u8 resume_value;
  193. };
  194. struct reg_r {
  195. u8 address;
  196. u8 value;
  197. };
  198. static struct dataus_registers {
  199. struct reg_r who_am_i;
  200. struct reg_rw cntrl1;
  201. struct reg_rw cntrl2;
  202. struct reg_rw cntrl3;
  203. struct reg_rw cntrl4;
  204. struct reg_rw cntrl5;
  205. } dataus_registers = {
  206. .who_am_i.address = REG_WHOAMI_ADDR,
  207. .who_am_i.value = WHOIAM_VALUE,
  208. .cntrl1.address = REG_CNTRL1_ADDR,
  209. .cntrl1.default_value = REG_DEF_CNTRL1,
  210. .cntrl2.address = REG_CNTRL2_ADDR,
  211. .cntrl2.default_value = REG_DEF_CNTRL2,
  212. .cntrl3.address = REG_CNTRL3_ADDR,
  213. .cntrl3.default_value = REG_DEF_CNTRL3,
  214. .cntrl4.address = REG_CNTRL4_ADDR,
  215. .cntrl4.default_value = REG_DEF_CNTRL4,
  216. .cntrl5.address = REG_CNTRL5_ADDR,
  217. .cntrl5.default_value = REG_DEF_CNTRL5,
  218. };
  219. static int k303c_mag_i2c_read(struct k303c_mag_p *data, u8 *buf, int len)
  220. {
  221. int ret;
  222. u8 reg = buf[0];
  223. u8 cmd = reg;
  224. if (len > 1)
  225. cmd = (I2C_AUTO_INCREMENT | reg);
  226. if (data->use_smbus) {
  227. if (len == 1) {
  228. ret = i2c_smbus_read_byte_data(data->client, cmd);
  229. buf[0] = ret & 0xff;
  230. #ifdef DEBUG
  231. dev_warn(&data->client->dev,
  232. "i2c_smbus_read_byte_data: ret=0x%02x, len:%d ,"
  233. "command=0x%02x, buf[0]=0x%02x\n",
  234. ret, len, cmd , buf[0]);
  235. #endif
  236. } else if (len > 1) {
  237. ret = i2c_smbus_read_i2c_block_data(data->client,
  238. cmd, len, buf);
  239. } else
  240. ret = -1;
  241. if (ret < 0) {
  242. dev_err(&data->client->dev,
  243. "read transfer error: len:%d, command=0x%02x\n",
  244. len, cmd);
  245. return 0;
  246. }
  247. return len;
  248. }
  249. ret = i2c_master_send(data->client, &cmd, sizeof(cmd));
  250. if (ret != sizeof(cmd))
  251. return ret;
  252. return i2c_master_recv(data->client, buf, len);
  253. }
  254. static int k303c_mag_i2c_write(struct k303c_mag_p *data, u8 *buf, int len)
  255. {
  256. int ret;
  257. u8 reg, value;
  258. if (len > 1)
  259. buf[0] = (I2C_AUTO_INCREMENT | buf[0]);
  260. reg = buf[0];
  261. value = buf[1];
  262. if (data->use_smbus) {
  263. if (len == 1) {
  264. ret = i2c_smbus_write_byte_data(data->client,
  265. reg, value);
  266. #ifdef DEBUG
  267. dev_warn(&data->client->dev,
  268. "i2c_smbus_write_byte_data: ret=%d, len:%d, "
  269. "command=0x%02x, value=0x%02x\n",
  270. ret, len, reg , value);
  271. #endif
  272. return ret;
  273. } else if (len > 1) {
  274. ret = i2c_smbus_write_i2c_block_data(data->client,
  275. reg, len, buf + 1);
  276. return ret;
  277. }
  278. }
  279. ret = i2c_master_send(data->client, buf, len+1);
  280. return (ret == len+1) ? 0 : ret;
  281. }
  282. static int k303c_mag_hw_init(struct k303c_mag_p *data)
  283. {
  284. int err;
  285. u8 buf[6];
  286. buf[0] = dataus_registers.who_am_i.address;
  287. err = k303c_mag_i2c_read(data, buf, 1);
  288. if (err < 0) {
  289. dev_warn(&data->client->dev,
  290. "Error reading WHO_AM_I: is device available/working?\n");
  291. goto err_firstread;
  292. } else
  293. data->hw_working = 1;
  294. if (buf[0] != dataus_registers.who_am_i.value) {
  295. dev_err(&data->client->dev,
  296. "device unknown. Expected: 0x%02x, Replies: 0x%02x\n",
  297. dataus_registers.who_am_i.value, buf[0]);
  298. err = -1;
  299. goto err_unknown_device;
  300. }
  301. dataus_registers.cntrl1.resume_value =
  302. dataus_registers.cntrl1.default_value;
  303. dataus_registers.cntrl2.resume_value =
  304. dataus_registers.cntrl2.default_value;
  305. dataus_registers.cntrl3.resume_value =
  306. dataus_registers.cntrl3.default_value;
  307. dataus_registers.cntrl4.resume_value =
  308. dataus_registers.cntrl4.default_value;
  309. dataus_registers.cntrl5.resume_value =
  310. dataus_registers.cntrl5.default_value;
  311. buf[0] = dataus_registers.cntrl1.address;
  312. buf[1] = dataus_registers.cntrl1.default_value;
  313. buf[2] = dataus_registers.cntrl2.default_value;
  314. buf[3] = dataus_registers.cntrl3.default_value;
  315. buf[4] = dataus_registers.cntrl4.default_value;
  316. buf[5] = dataus_registers.cntrl5.default_value;
  317. err = k303c_mag_i2c_write(data, buf, 5);
  318. if (err < 0) {
  319. dev_warn(&data->client->dev,
  320. "Error initializing CLTR_REG registers\n");
  321. goto err_reginit;
  322. }
  323. data->xy_mode = X_Y_ULTRA_HIGH_PERFORMANCE;
  324. data->z_mode = Z_ULTRA_HIGH_PERFORMANCE;
  325. data->hw_initialized = 1;
  326. return 0;
  327. err_reginit:
  328. err_unknown_device:
  329. err_firstread:
  330. data->hw_working = 0;
  331. data->hw_initialized = 0;
  332. return err;
  333. }
  334. static int sensor_regulator_onoff(struct k303c_mag_p *data, bool onoff)
  335. {
  336. int ret = -1;
  337. if (IS_ERR_OR_NULL(data->reg_vdd) || IS_ERR_OR_NULL(data->reg_vio)) {
  338. pr_err("[SENSOR]: %s: Failed to enable regulator.\n", __func__);
  339. return -ENODEV;
  340. }
  341. if (onoff) {
  342. ret = regulator_enable(data->reg_vdd);
  343. if (ret) {
  344. pr_err("[SENSOR]: %s: Failed to enable regulator vdd.\n",
  345. __func__);
  346. return ret;
  347. }
  348. ret = regulator_enable(data->reg_vio);
  349. if (ret) {
  350. pr_err("[SENSOR]: %s: Failed to enable regulator vio.\n",
  351. __func__);
  352. return ret;
  353. }
  354. msleep(30);
  355. } else {
  356. ret = regulator_disable(data->reg_vdd);
  357. if (ret) {
  358. pr_err("[SENSOR]: %s: Failed to disable regulatorvdd.\n",
  359. __func__);
  360. return ret;
  361. }
  362. ret = regulator_disable(data->reg_vio);
  363. if (ret) {
  364. pr_err("[SENSOR]: %s: Failed to disable regulator vio.\n",
  365. __func__);
  366. return ret;
  367. }
  368. }
  369. return 0;
  370. }
  371. static int k303c_mag_device_power_off(struct k303c_mag_p *data)
  372. {
  373. int err;
  374. u8 buf[2];
  375. buf[0] = dataus_registers.cntrl3.address;
  376. buf[1] = ((MSMS_MASK & POWEROFF_MAG) |
  377. ((~MSMS_MASK) & dataus_registers.cntrl3.resume_value));
  378. err = k303c_mag_i2c_write(data, buf, 1);
  379. if (err < 0)
  380. dev_err(&data->client->dev,
  381. "magnetometer soft power off failed: %d\n", err);
  382. err = sensor_regulator_onoff(data, false);
  383. if (err < 0)
  384. return err;
  385. atomic_set(&data->enabled_mag, 0);
  386. return 0;
  387. }
  388. static int k303c_mag_device_power_on(struct k303c_mag_p *data)
  389. {
  390. int err;
  391. u8 buf[6];
  392. err = sensor_regulator_onoff(data, true);
  393. if (err < 0)
  394. return err;
  395. buf[0] = dataus_registers.cntrl1.address;
  396. buf[1] = dataus_registers.cntrl1.resume_value;
  397. err = k303c_mag_i2c_write(data, buf, 1);
  398. if (err < 0)
  399. goto err_resume_datae;
  400. buf[0] = dataus_registers.cntrl3.address;
  401. buf[1] = ((MSMS_MASK & CONTINUOS_CONVERSION) |
  402. ((~MSMS_MASK) & dataus_registers.cntrl3.resume_value));
  403. err = k303c_mag_i2c_write(data, buf, 1);
  404. if (err < 0)
  405. goto err_resume_datae;
  406. atomic_set(&data->enabled_mag, 1);
  407. return 0;
  408. err_resume_datae:
  409. atomic_set(&data->enabled_mag, 0);
  410. dev_err(&data->client->dev,
  411. "magnetometer hw power on error 0x%02x,0x%02x: %d\n",
  412. buf[0], buf[1], err);
  413. return err;
  414. }
  415. static int k303c_mag_update_fs_range(struct k303c_mag_p *data,
  416. u8 new_fs_range)
  417. {
  418. int err = -1;
  419. u32 sensitivity;
  420. u8 updated_val;
  421. u8 buf[2];
  422. switch (new_fs_range) {
  423. case K303C_MAG_FS_4G:
  424. sensitivity = SENSITIVITY_MAG_4G;
  425. break;
  426. case K303C_MAG_FS_8G:
  427. sensitivity = SENSITIVITY_MAG_8G;
  428. break;
  429. case K303C_MAG_FS_10G:
  430. sensitivity = SENSITIVITY_MAG_10G;
  431. break;
  432. case K303C_MAG_FS_16G:
  433. sensitivity = SENSITIVITY_MAG_16G;
  434. break;
  435. default:
  436. dev_err(&data->client->dev,
  437. "invalid magnetometer fs range requested: %u\n",
  438. new_fs_range);
  439. return -EINVAL;
  440. }
  441. buf[0] = dataus_registers.cntrl2.address;
  442. err = k303c_mag_i2c_read(data, buf, 1);
  443. if (err < 0)
  444. goto error;
  445. dataus_registers.cntrl2.resume_value = buf[0];
  446. updated_val = (K303C_MAG_FS_MASK & new_fs_range);
  447. buf[1] = updated_val;
  448. buf[0] = dataus_registers.cntrl2.address;
  449. err = k303c_mag_i2c_write(data, buf, 1);
  450. if (err < 0)
  451. goto error;
  452. dataus_registers.cntrl2.resume_value = updated_val;
  453. data->sensitivity_mag = sensitivity;
  454. return err;
  455. error:
  456. dev_err(&data->client->dev,
  457. "update magnetometer fs range failed 0x%02x,0x%02x: %d\n",
  458. buf[0], buf[1], err);
  459. return err;
  460. }
  461. static int k303c_mag_update_odr(struct k303c_mag_p *data,
  462. unsigned int poll_interval_ms)
  463. {
  464. int err = -1;
  465. u8 config[2];
  466. int i;
  467. for (i = ARRAY_SIZE(k303c_mag_odr_table) - 1; i >= 0; i--) {
  468. if ((k303c_mag_odr_table[i].cutoff_us <= poll_interval_ms)
  469. || (i == 0))
  470. break;
  471. }
  472. config[1] = ((ODR_MAG_MASK & k303c_mag_odr_table[i].value) |
  473. ((~ODR_MAG_MASK) & dataus_registers.cntrl1.resume_value));
  474. if (atomic_read(&data->enabled_mag)) {
  475. config[0] = dataus_registers.cntrl1.address;
  476. err = k303c_mag_i2c_write(data, config, 1);
  477. if (err < 0)
  478. goto error;
  479. dataus_registers.cntrl1.resume_value = config[1];
  480. data->ktime_mag = ktime_set(0, MS_TO_NS(poll_interval_ms));
  481. }
  482. return err;
  483. error:
  484. dev_err(&data->client->dev,
  485. "update magnetometer odr failed 0x%02x,0x%02x: %d\n",
  486. config[0], config[1], err);
  487. return err;
  488. }
  489. static int k303c_mag_update_operative_mode(struct k303c_mag_p *data,
  490. int axis, u8 value)
  491. {
  492. int err = -1;
  493. u8 config[2];
  494. u8 mask;
  495. u8 addr;
  496. if (axis == 0) {
  497. config[0] = REG_CNTRL1_ADDR;
  498. mask = X_Y_PERFORMANCE_MASK;
  499. addr = REG_CNTRL1_ADDR;
  500. } else {
  501. config[0] = REG_CNTRL4_ADDR;
  502. mask = Z_PERFORMANCE_MASK;
  503. addr = REG_CNTRL4_ADDR;
  504. }
  505. err = k303c_mag_i2c_read(data, config, 1);
  506. if (err < 0)
  507. goto error;
  508. config[1] = ((mask & value) |
  509. ((~mask) & config[0]));
  510. config[0] = addr;
  511. err = k303c_mag_i2c_write(data, config, 1);
  512. if (err < 0)
  513. goto error;
  514. if (axis == 0)
  515. data->xy_mode = value;
  516. else
  517. data->z_mode = value;
  518. return err;
  519. error:
  520. dev_err(&data->client->dev, "update operative mode failed 0x%02x,0x%02x: %d\n",
  521. config[0], config[1], err);
  522. return err;
  523. }
  524. static int k303c_mag_validate_polling(unsigned int *min_interval,
  525. unsigned int *poll_interval, unsigned int min, u8 *axis_map_x,
  526. u8 *axis_map_y, u8 *axis_map_z, struct i2c_client *client)
  527. {
  528. *min_interval = max(min, *min_interval);
  529. *poll_interval = max(*poll_interval, *min_interval);
  530. if (*axis_map_x > 2 || *axis_map_y > 2 || *axis_map_z > 2) {
  531. dev_err(&client->dev,
  532. "invalid axis_map value x:%u y:%u z%u\n",
  533. *axis_map_x, *axis_map_y, *axis_map_z);
  534. return -EINVAL;
  535. }
  536. return 0;
  537. }
  538. static int k303c_mag_validate_negate(u8 *negate_x, u8 *negate_y, u8 *negate_z,
  539. struct i2c_client *client)
  540. {
  541. if (*negate_x > 1 || *negate_y > 1 || *negate_z > 1) {
  542. dev_err(&client->dev,
  543. "invalid negate value x:%u y:%u z:%u\n",
  544. *negate_x, *negate_y, *negate_z);
  545. return -EINVAL;
  546. }
  547. return 0;
  548. }
  549. static int k303c_mag_validate_pdata(struct k303c_mag_p *data)
  550. {
  551. int res = -1;
  552. res = k303c_mag_validate_polling(&data->pdata->min_interval,
  553. &data->pdata->poll_interval,
  554. (unsigned int)K303C_MAG_MIN_POLL_PERIOD_MS,
  555. &data->pdata->axis_map_x,
  556. &data->pdata->axis_map_y,
  557. &data->pdata->axis_map_z,
  558. data->client);
  559. if (res < 0)
  560. return -EINVAL;
  561. res = k303c_mag_validate_negate(&data->pdata->negate_x,
  562. &data->pdata->negate_y,
  563. &data->pdata->negate_z,
  564. data->client);
  565. if (res < 0)
  566. return -EINVAL;
  567. return 0;
  568. }
  569. static int k303c_mag_enable(struct k303c_mag_p *data)
  570. {
  571. int err;
  572. if (!atomic_cmpxchg(&data->enabled_mag, 0, 1)) {
  573. err = k303c_mag_device_power_on(data);
  574. if (err < 0) {
  575. atomic_set(&data->enabled_mag, 0);
  576. return err;
  577. }
  578. hrtimer_start(&data->hr_timer_mag,
  579. data->ktime_mag, HRTIMER_MODE_REL);
  580. }
  581. return 0;
  582. }
  583. static int k303c_mag_disable(struct k303c_mag_p *data)
  584. {
  585. if (atomic_cmpxchg(&data->enabled_mag, 1, 0)) {
  586. cancel_work_sync(&data->input_work_mag);
  587. hrtimer_cancel(&data->hr_timer_mag);
  588. k303c_mag_device_power_off(data);
  589. }
  590. return 0;
  591. }
  592. static void k303c_mag_input_cleanup(struct k303c_mag_p *data)
  593. {
  594. input_unregister_device(data->input);
  595. input_free_device(data->input);
  596. }
  597. static ssize_t k303c_mag_delay_show(struct device *dev,
  598. struct device_attribute *attr, char *buf)
  599. {
  600. unsigned int val;
  601. struct k303c_mag_p *data = dev_get_drvdata(dev);
  602. mutex_lock(&data->lock);
  603. val = data->pdata->poll_interval;
  604. mutex_unlock(&data->lock);
  605. return snprintf(buf, 0xff, "%u\n", val);
  606. }
  607. static ssize_t k303c_mag_delay_store(struct device *dev,
  608. struct device_attribute *attr, const char *buf, size_t size)
  609. {
  610. struct k303c_mag_p *data = dev_get_drvdata(dev);
  611. unsigned long delay;
  612. unsigned int interval_ms;
  613. if (kstrtoul(buf, 10, &delay)) {
  614. pr_err("[SENSOR]: %s - set delay error\n", __func__);
  615. return -EINVAL;
  616. }
  617. interval_ms = (unsigned int)(delay / 1000000L);
  618. interval_ms = max_t(unsigned int, (unsigned int)interval_ms,
  619. data->pdata->min_interval);
  620. mutex_lock(&data->lock);
  621. data->pdata->poll_interval = (unsigned int)interval_ms;
  622. k303c_mag_update_odr(data, interval_ms);
  623. mutex_unlock(&data->lock);
  624. return size;
  625. }
  626. static ssize_t k303c_mag_enable_show(struct device *dev,
  627. struct device_attribute *attr, char *buf)
  628. {
  629. struct k303c_mag_p *data = dev_get_drvdata(dev);
  630. int val = (int)atomic_read(&data->enabled_mag);
  631. return snprintf(buf, 0xff, "%d\n", val);
  632. }
  633. static ssize_t k303c_mag_enable_store(struct device *dev,
  634. struct device_attribute *attr, const char *buf, size_t size)
  635. {
  636. struct k303c_mag_p *data = dev_get_drvdata(dev);
  637. unsigned long val;
  638. if (kstrtoul(buf, 10, &val))
  639. return -EINVAL;
  640. if (val)
  641. k303c_mag_enable(data);
  642. else
  643. k303c_mag_disable(data);
  644. return size;
  645. }
  646. static ssize_t attr_get_range_mag(struct device *dev,
  647. struct device_attribute *attr, char *buf)
  648. {
  649. u8 val;
  650. int range = 2;
  651. struct k303c_mag_p *data = dev_get_drvdata(dev);
  652. mutex_lock(&data->lock);
  653. val = data->pdata->fs_range;
  654. switch (val) {
  655. case K303C_MAG_FS_4G:
  656. range = 4;
  657. break;
  658. case K303C_MAG_FS_8G:
  659. range = 8;
  660. break;
  661. case K303C_MAG_FS_10G:
  662. range = 10;
  663. break;
  664. case K303C_MAG_FS_16G:
  665. range = 16;
  666. break;
  667. }
  668. mutex_unlock(&data->lock);
  669. return snprintf(buf, 0xff, "%d\n", range);
  670. }
  671. static ssize_t attr_set_range_mag(struct device *dev,
  672. struct device_attribute *attr, const char *buf, size_t size)
  673. {
  674. struct k303c_mag_p *data = dev_get_drvdata(dev);
  675. unsigned long val;
  676. u8 range;
  677. int err;
  678. if (kstrtoul(buf, 10, &val))
  679. return -EINVAL;
  680. switch (val) {
  681. case 4:
  682. range = K303C_MAG_FS_4G;
  683. break;
  684. case 8:
  685. range = K303C_MAG_FS_8G;
  686. break;
  687. case 10:
  688. range = K303C_MAG_FS_10G;
  689. break;
  690. case 16:
  691. range = K303C_MAG_FS_16G;
  692. break;
  693. default:
  694. dev_err(&data->client->dev,
  695. "magnetometer invalid range request: %lu, discarded\n",
  696. val);
  697. return -EINVAL;
  698. }
  699. mutex_lock(&data->lock);
  700. err = k303c_mag_update_fs_range(data, range);
  701. if (err < 0) {
  702. mutex_unlock(&data->lock);
  703. return err;
  704. }
  705. data->pdata->fs_range = range;
  706. mutex_unlock(&data->lock);
  707. dev_info(&data->client->dev,
  708. "magnetometer range set to: %lu g\n", val);
  709. return size;
  710. }
  711. static ssize_t attr_get_xy_mode(struct device *dev,
  712. struct device_attribute *attr, char *buf)
  713. {
  714. u8 val;
  715. char mode[13];
  716. struct k303c_mag_p *data = dev_get_drvdata(dev);
  717. mutex_lock(&data->lock);
  718. val = data->xy_mode;
  719. switch (val) {
  720. case X_Y_HIGH_PERFORMANCE:
  721. strlcpy(mode, "high", sizeof(mode));
  722. break;
  723. case X_Y_LOW_PERFORMANCE:
  724. strlcpy(mode, "low", sizeof(mode));
  725. break;
  726. case X_Y_MEDIUM_PERFORMANCE:
  727. strlcpy(mode, "medium", sizeof(mode));
  728. break;
  729. case X_Y_ULTRA_HIGH_PERFORMANCE:
  730. strlcpy(mode, "ultra_high", sizeof(mode));
  731. break;
  732. }
  733. mutex_unlock(&data->lock);
  734. return snprintf(buf, 0xff, "%s\n", mode);
  735. }
  736. static ssize_t attr_set_xy_mode(struct device *dev,
  737. struct device_attribute *attr, const char *buf, size_t size)
  738. {
  739. struct k303c_mag_p *data = dev_get_drvdata(dev);
  740. u8 mode;
  741. int err;
  742. err = strncmp(buf, "high", 4);
  743. if (err == 0) {
  744. mode = X_Y_HIGH_PERFORMANCE;
  745. goto valid;
  746. }
  747. err = strncmp(buf, "low", 3);
  748. if (err == 0) {
  749. mode = X_Y_LOW_PERFORMANCE;
  750. goto valid;
  751. }
  752. err = strncmp(buf, "medium", 6);
  753. if (err == 0) {
  754. mode = X_Y_MEDIUM_PERFORMANCE;
  755. goto valid;
  756. }
  757. err = strncmp(buf, "ultra_high", 10);
  758. if (err == 0) {
  759. mode = X_Y_ULTRA_HIGH_PERFORMANCE;
  760. goto valid;
  761. }
  762. goto error;
  763. valid:
  764. err = k303c_mag_update_operative_mode(data, 0, mode);
  765. if (err < 0)
  766. goto error;
  767. dev_info(&data->client->dev,
  768. "magnetometer x_y op. mode set to: %s", buf);
  769. return size;
  770. error:
  771. dev_err(&data->client->dev,
  772. "magnetometer invalid value request: %s, discarded\n", buf);
  773. return -EINVAL;
  774. }
  775. static ssize_t attr_get_z_mode(struct device *dev,
  776. struct device_attribute *attr, char *buf)
  777. {
  778. u8 val;
  779. char mode[13];
  780. struct k303c_mag_p *data = dev_get_drvdata(dev);
  781. mutex_lock(&data->lock);
  782. val = data->z_mode;
  783. switch (val) {
  784. case Z_HIGH_PERFORMANCE:
  785. strlcpy(mode, "high", sizeof(mode));
  786. break;
  787. case Z_LOW_PERFORMANCE:
  788. strlcpy(mode, "low", sizeof(mode));
  789. break;
  790. case Z_MEDIUM_PERFORMANCE:
  791. strlcpy(mode, "medium", sizeof(mode));
  792. break;
  793. case Z_ULTRA_HIGH_PERFORMANCE:
  794. strlcpy(mode, "ultra_high", sizeof(mode));
  795. break;
  796. }
  797. mutex_unlock(&data->lock);
  798. return snprintf(buf, 0xff, "%s\n", mode);
  799. }
  800. static ssize_t attr_set_z_mode(struct device *dev,
  801. struct device_attribute *attr, const char *buf, size_t size)
  802. {
  803. struct k303c_mag_p *data = dev_get_drvdata(dev);
  804. u8 mode;
  805. int err;
  806. err = strncmp(buf, "high", 4);
  807. if (err == 0) {
  808. mode = Z_HIGH_PERFORMANCE;
  809. goto valid;
  810. }
  811. err = strncmp(buf, "low", 3);
  812. if (err == 0) {
  813. mode = Z_LOW_PERFORMANCE;
  814. goto valid;
  815. }
  816. err = strncmp(buf, "medium", 6);
  817. if (err == 0) {
  818. mode = Z_MEDIUM_PERFORMANCE;
  819. goto valid;
  820. }
  821. err = strncmp(buf, "ultra_high", 10);
  822. if (err == 0) {
  823. mode = Z_ULTRA_HIGH_PERFORMANCE;
  824. goto valid;
  825. }
  826. goto error;
  827. valid:
  828. err = k303c_mag_update_operative_mode(data, 1, mode);
  829. if (err < 0)
  830. goto error;
  831. dev_info(&data->client->dev,
  832. "magnetometer z op. mode set to: %s", buf);
  833. return size;
  834. error:
  835. dev_err(&data->client->dev,
  836. "magnetometer invalid value request: %s, discarded\n", buf);
  837. return -EINVAL;
  838. }
  839. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  840. k303c_mag_delay_show, k303c_mag_delay_store);
  841. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  842. k303c_mag_enable_show, k303c_mag_enable_store);
  843. static struct attribute *k303c_mag_attributes[] = {
  844. &dev_attr_poll_delay.attr,
  845. &dev_attr_enable.attr,
  846. NULL
  847. };
  848. static struct attribute_group k303c_mag_attribute_group = {
  849. .attrs = k303c_mag_attributes
  850. };
  851. static struct device_attribute attributes[] = {
  852. __ATTR(full_scale, 0666, attr_get_range_mag, attr_set_range_mag),
  853. __ATTR(x_y_opearative_mode, 0666, attr_get_xy_mode, attr_set_xy_mode),
  854. __ATTR(z_opearative_mode, 0666, attr_get_z_mode, attr_set_z_mode),
  855. };
  856. static void remove_sysfs_interfaces(struct device *dev)
  857. {
  858. int i;
  859. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  860. device_remove_file(dev, attributes + i);
  861. }
  862. int k303c_mag_input_open(struct input_dev *input)
  863. {
  864. struct k303c_mag_p *data = input_get_drvdata(input);
  865. dev_dbg(&data->client->dev, "%s\n", __func__);
  866. return 0;
  867. }
  868. void k303c_mag_input_close(struct input_dev *dev)
  869. {
  870. struct k303c_mag_p *data = input_get_drvdata(dev);
  871. dev_dbg(&data->client->dev, "%s\n", __func__);
  872. k303c_mag_disable(data);
  873. }
  874. static int k303c_mag_get_data(struct k303c_mag_p *data, int *xyz)
  875. {
  876. int err = -1;
  877. u8 mag_data[6];
  878. s32 hw_d[3] = { 0 };
  879. mag_data[0] = (REG_MAG_DATA_ADDR);
  880. err = k303c_mag_i2c_read(data, mag_data, 6);
  881. if (err < 0)
  882. return err;
  883. hw_d[0] = (s16)((mag_data[1] << 8) | mag_data[0]);
  884. hw_d[1] = (s16)((mag_data[3] << 8) | mag_data[2]);
  885. hw_d[2] = (s16)((mag_data[5] << 8) | mag_data[4]);
  886. #ifdef DEBUG
  887. pr_debug("%s read x=0x%02x 0x%02x (regH regL), x=%d (dec) [LSB]\n",
  888. K303C_MAG_DEV_NAME, mag_data[1], mag_data[0], hw_d[0]);
  889. pr_debug("%s read y=0x%02x 0x%02x (regH regL), y=%d (dec) [LSB]\n",
  890. K303C_MAG_DEV_NAME, mag_data[3], mag_data[2], hw_d[1]);
  891. pr_debug("%s read z=0x%02x 0x%02x (regH regL), z=%d (dec) [LSB]\n",
  892. K303C_MAG_DEV_NAME, mag_data[5], mag_data[4], hw_d[2]);
  893. #endif
  894. xyz[0] = ((data->pdata->negate_x) ? (-hw_d[data->pdata->axis_map_x])
  895. : (hw_d[data->pdata->axis_map_x]));
  896. xyz[1] = ((data->pdata->negate_y) ? (-hw_d[data->pdata->axis_map_y])
  897. : (hw_d[data->pdata->axis_map_y]));
  898. xyz[2] = ((data->pdata->negate_z) ? (-hw_d[data->pdata->axis_map_z])
  899. : (hw_d[data->pdata->axis_map_z]));
  900. return err;
  901. }
  902. static void k303c_mag_report_values(struct k303c_mag_p *data, int *xyz)
  903. {
  904. input_report_abs(data->input, ABS_X, xyz[0]);
  905. input_report_abs(data->input, ABS_Y, xyz[1]);
  906. input_report_abs(data->input, ABS_Z, xyz[2]);
  907. input_sync(data->input);
  908. }
  909. static int k303c_mag_input_init(struct k303c_mag_p *data)
  910. {
  911. int err;
  912. data->input = input_allocate_device();
  913. if (!data->input) {
  914. err = -ENOMEM;
  915. dev_err(&data->client->dev,
  916. "magnetometer input device allocation failed\n");
  917. goto err0;
  918. }
  919. data->input->open = k303c_mag_input_open;
  920. data->input->close = k303c_mag_input_close;
  921. data->input->name = MODULE_NAME;
  922. data->input->id.bustype = BUS_I2C;
  923. data->input->dev.parent = &data->client->dev;
  924. input_set_drvdata(data->input, data);
  925. set_bit(EV_ABS, data->input->evbit);
  926. input_set_abs_params(data->input, ABS_X,
  927. -MAG_G_MAX_NEG, MAG_G_MAX_POS, FUZZ, FLAT);
  928. input_set_abs_params(data->input, ABS_Y,
  929. -MAG_G_MAX_NEG, MAG_G_MAX_POS, FUZZ, FLAT);
  930. input_set_abs_params(data->input, ABS_Z,
  931. -MAG_G_MAX_NEG, MAG_G_MAX_POS, FUZZ, FLAT);
  932. err = input_register_device(data->input);
  933. if (err) {
  934. dev_err(&data->client->dev,
  935. "unable to register magnetometer input device %s\n",
  936. data->input->name);
  937. goto err1;
  938. }
  939. err = sensors_create_symlink(&data->input->dev.kobj,
  940. data->input->name);
  941. if (err < 0) {
  942. input_unregister_device(data->input);
  943. return err;
  944. }
  945. /* sysfs node creation */
  946. err = sysfs_create_group(&data->input->dev.kobj,
  947. &k303c_mag_attribute_group);
  948. if (err < 0) {
  949. sensors_remove_symlink(&data->input->dev.kobj,
  950. data->input->name);
  951. input_unregister_device(data->input);
  952. return err;
  953. }
  954. return 0;
  955. err1:
  956. input_free_device(data->input);
  957. err0:
  958. return err;
  959. }
  960. static void poll_function_work_mag(struct work_struct *input_work_mag)
  961. {
  962. struct k303c_mag_p *data;
  963. int xyz[3] = { 0 };
  964. int err;
  965. data = container_of((struct work_struct *)input_work_mag,
  966. struct k303c_mag_p, input_work_mag);
  967. mutex_lock(&data->lock);
  968. if (atomic_read(&data->enabled_mag)) {
  969. err = k303c_mag_get_data(data, xyz);
  970. if (err < 0)
  971. dev_err(&data->client->dev,
  972. "get_magnetometer_data failed\n");
  973. else
  974. k303c_mag_report_values(data, xyz);
  975. }
  976. mutex_unlock(&data->lock);
  977. hrtimer_start(&data->hr_timer_mag, data->ktime_mag, HRTIMER_MODE_REL);
  978. }
  979. enum hrtimer_restart poll_function_read_mag(struct hrtimer *timer)
  980. {
  981. struct k303c_mag_p *data;
  982. data = container_of((struct hrtimer *)timer,
  983. struct k303c_mag_p, hr_timer_mag);
  984. queue_work(data->mag_wq, &data->input_work_mag);
  985. return HRTIMER_NORESTART;
  986. }
  987. static int k303c_mag_parse_dt(struct k303c_mag_p *data, struct device *dev)
  988. {
  989. struct device_node *dev_node = dev->of_node;
  990. int ret;
  991. u32 temp;
  992. if (dev_node == NULL)
  993. return -ENODEV;
  994. ret = of_property_read_u32(dev_node, "k303c_mag,axis_map_x", &temp);
  995. if ((data->pdata->axis_map_x > 2) || (ret < 0)) {
  996. pr_err("[SENSOR]: %s: invalid x axis_map value %u\n",
  997. __func__, data->pdata->axis_map_x);
  998. data->pdata->axis_map_x = 0;
  999. } else {
  1000. data->pdata->axis_map_x = (u8)temp;
  1001. }
  1002. ret = of_property_read_u32(dev_node, "k303c_mag,axis_map_y", &temp);
  1003. if ((data->pdata->axis_map_y > 2) || (ret < 0)) {
  1004. pr_err("[SENSOR]: %s: invalid y axis_map value %u\n",
  1005. __func__, data->pdata->axis_map_y);
  1006. data->pdata->axis_map_y = 1;
  1007. } else {
  1008. data->pdata->axis_map_y = (u8)temp;
  1009. }
  1010. ret = of_property_read_u32(dev_node, "k303c_mag,axis_map_z", &temp);
  1011. if ((data->pdata->axis_map_z > 2) || (ret < 0)) {
  1012. pr_err("[SENSOR]: %s: invalid z axis_map value %u\n",
  1013. __func__, data->pdata->axis_map_z);
  1014. data->pdata->axis_map_z = 2;
  1015. } else {
  1016. data->pdata->axis_map_z = (u8)temp;
  1017. }
  1018. ret = of_property_read_u32(dev_node, "k303c_mag,negate_x", &temp);
  1019. if ((data->pdata->negate_x > 1) || (ret < 0)) {
  1020. pr_err("[SENSOR]: %s: invalid x axis_map value %u\n",
  1021. __func__, data->pdata->negate_x);
  1022. data->pdata->negate_x = 0;
  1023. } else {
  1024. data->pdata->negate_x = (u8)temp;
  1025. }
  1026. ret = of_property_read_u32(dev_node, "k303c_mag,negate_y", &temp);
  1027. if ((data->pdata->negate_y > 1) || (ret < 0)) {
  1028. pr_err("[SENSOR]: %s: invalid y axis_map value %u\n",
  1029. __func__, data->pdata->negate_y);
  1030. data->pdata->negate_y = 0;
  1031. } else {
  1032. data->pdata->negate_y = (u8)temp;
  1033. }
  1034. ret = of_property_read_u32(dev_node, "k303c_mag,negate_z", &temp);
  1035. if ((data->pdata->negate_z > 1) || (ret < 0)) {
  1036. pr_err("[SENSOR]: %s: invalid z axis_map value %u\n",
  1037. __func__, data->pdata->negate_z);
  1038. data->pdata->negate_z = 0;
  1039. } else {
  1040. data->pdata->negate_z = (u8)temp;
  1041. }
  1042. data->reg_vdd = devm_regulator_get(dev, "k303c_mag,vdd");
  1043. if (IS_ERR(data->reg_vdd)) {
  1044. pr_err("[SENSOR] could not get vdd, %ld\n",
  1045. PTR_ERR(data->reg_vdd));
  1046. } else {
  1047. if (!((AVG_VDD <= regulator_get_voltage(data->reg_vdd))
  1048. && (regulator_get_voltage(data->reg_vdd) <= MAX_VDD)))
  1049. ret = regulator_set_voltage(data->reg_vdd,
  1050. AVG_VDD, AVG_VDD);
  1051. if (ret) {
  1052. pr_err("[SENSOR]: %s: set voltage failed on vdd, rc=%d\n",
  1053. __func__, ret);
  1054. return ret;
  1055. }
  1056. }
  1057. data->reg_vio = devm_regulator_get(dev, "k303c_mag,vio");
  1058. if (IS_ERR(data->reg_vio)) {
  1059. pr_err("[SENSOR] could not get vio, %ld\n",
  1060. PTR_ERR(data->reg_vio));
  1061. } else {
  1062. ret = regulator_set_voltage(data->reg_vio, AVG_VIO, AVG_VIO);
  1063. if (ret) {
  1064. pr_err("[SENSOR]: %s: set voltage failed on vio rc=%d\n",
  1065. __func__, ret);
  1066. return ret;
  1067. }
  1068. }
  1069. return 0;
  1070. }
  1071. static ssize_t k303c_mag_adc_show(struct device *dev,
  1072. struct device_attribute *attr, char *buf)
  1073. {
  1074. struct k303c_mag_p *data;
  1075. int xyz[3] = { 0 };
  1076. int err;
  1077. pr_info("%s\n", __func__);
  1078. data = dev_get_drvdata(dev);
  1079. mutex_lock(&data->lock);
  1080. if (!atomic_read(&data->enabled_mag)) {
  1081. k303c_mag_device_power_on(data);
  1082. err = k303c_mag_get_data(data, xyz);
  1083. k303c_mag_device_power_off(data);
  1084. } else {
  1085. err = k303c_mag_get_data(data, xyz);
  1086. }
  1087. mutex_unlock(&data->lock);
  1088. if (err < 0)
  1089. pr_err("%s k303c_mag_get_data err\n", __func__);
  1090. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", xyz[0], xyz[1], xyz[2]);
  1091. }
  1092. static ssize_t k303c_mag_name_show(struct device *dev,
  1093. struct device_attribute *attr, char *buf)
  1094. {
  1095. pr_info("%s\n", __func__);
  1096. return snprintf(buf, PAGE_SIZE, "%s\n", MODEL_NAME);
  1097. }
  1098. static ssize_t k303c_mag_raw_data_show(struct device *dev,
  1099. struct device_attribute *attr, char *buf)
  1100. {
  1101. struct k303c_mag_p *data;
  1102. int xyz[3] = { 0 };
  1103. int err;
  1104. pr_info("%s\n", __func__);
  1105. data = dev_get_drvdata(dev);
  1106. mutex_lock(&data->lock);
  1107. if (!atomic_read(&data->enabled_mag)) {
  1108. k303c_mag_device_power_on(data);
  1109. err = k303c_mag_get_data(data, xyz);
  1110. k303c_mag_device_power_off(data);
  1111. } else {
  1112. err = k303c_mag_get_data(data, xyz);
  1113. }
  1114. mutex_unlock(&data->lock);
  1115. if (err < 0)
  1116. pr_err("%s k303c_mag_get_data err\n", __func__);
  1117. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", xyz[0], xyz[1], xyz[2]);
  1118. }
  1119. static ssize_t k303c_mag_selftest_show(struct device *dev,
  1120. struct device_attribute *attr, char *buf)
  1121. {
  1122. struct k303c_mag_p *data = dev_get_drvdata(dev);
  1123. int val, i, en_state = 0;
  1124. ssize_t ret;
  1125. u8 x[8];
  1126. s32 NO_ST[3] = {0, 0, 0};
  1127. s32 ST[3] = {0, 0, 0};
  1128. s32 ST_MIN_LSB, ST_MAX_LSB;
  1129. pr_info("%s\n", __func__);
  1130. en_state = atomic_read(&data->enabled_mag);
  1131. k303c_mag_disable(data);
  1132. k303c_mag_device_power_on(data);
  1133. x[0] = REG_CNTRL1_ADDR;
  1134. x[1] = 0x1c;
  1135. x[2] = 0x60;
  1136. k303c_mag_i2c_write(data, x, 2);
  1137. msleep(20);
  1138. x[0] = REG_CNTRL3_ADDR;
  1139. x[1] = 0x00;
  1140. k303c_mag_i2c_write(data, x, 1);
  1141. msleep(20);
  1142. x[0] = REG_MAG_DATA_ADDR;
  1143. k303c_mag_i2c_read(data, x, 6);
  1144. for (i = 0; i < 6; i++) {
  1145. while (1) {
  1146. x[0] = 0x27;
  1147. val = k303c_mag_i2c_read(data, x, 1);
  1148. if (val < 0) {
  1149. pr_err("[SENSOR] %s : I2C fail. (%d)\n",
  1150. __func__, val);
  1151. goto ST_EXIT;
  1152. }
  1153. if (x[0] & 0x08)
  1154. break;
  1155. }
  1156. x[0] = REG_MAG_DATA_ADDR;
  1157. k303c_mag_i2c_read(data, x, 6);
  1158. if (i > 0) {
  1159. NO_ST[0] += (s16)((x[1] << 8) | x[0]);
  1160. NO_ST[1] += (s16)((x[3] << 8) | x[2]);
  1161. NO_ST[2] += (s16)((x[5] << 8) | x[4]);
  1162. }
  1163. }
  1164. NO_ST[0] /= 5;
  1165. NO_ST[1] /= 5;
  1166. NO_ST[2] /= 5;
  1167. x[0] = REG_CNTRL1_ADDR;
  1168. x[1] = 0x1d;
  1169. k303c_mag_i2c_write(data, x, 1);
  1170. msleep(60);
  1171. x[0] = REG_MAG_DATA_ADDR;
  1172. k303c_mag_i2c_read(data, x, 6);
  1173. for (i = 0; i < 6; i++) {
  1174. while (1) {
  1175. x[0] = 0x27;
  1176. val = k303c_mag_i2c_read(data, x, 1);
  1177. if (val < 0) {
  1178. pr_err("[SENSOR] %s : I2C fail. (%d)\n",
  1179. __func__, val);
  1180. goto ST_EXIT;
  1181. }
  1182. if (x[0] & 0x08)
  1183. break;
  1184. }
  1185. x[0] = REG_MAG_DATA_ADDR;
  1186. k303c_mag_i2c_read(data, x, 6);
  1187. if (i > 0) {
  1188. ST[0] += (s16)((x[1] << 8) | x[0]);
  1189. ST[1] += (s16)((x[3] << 8) | x[2]);
  1190. ST[2] += (s16)((x[5] << 8) | x[4]);
  1191. }
  1192. }
  1193. ST[0] /= 5;
  1194. ST[1] /= 5;
  1195. ST[2] /= 5;
  1196. for (val = 1, i = 0; i < 3; i++) {
  1197. ST[i] -= NO_ST[i];
  1198. switch (i) {
  1199. case 0:
  1200. case 1:
  1201. ST_MIN_LSB = MAG_SELF_TEST_16G_MIN_LSB;
  1202. ST_MAX_LSB = MAG_SELF_TEST_16G_MAX_LSB;
  1203. break;
  1204. case 2:
  1205. ST_MIN_LSB = MAG_SELF_TEST_16G_MIN_LSB_Z;
  1206. ST_MAX_LSB = MAG_SELF_TEST_16G_MAX_LSB_Z;
  1207. break;
  1208. default:
  1209. ST_MIN_LSB = 0;
  1210. ST_MAX_LSB = 0;
  1211. }
  1212. if ((ST_MIN_LSB > ST[i]) || (ST[i] > ST_MAX_LSB)) {
  1213. pr_err("[SENSOR] %s :[%d]: Out of range!! (%d)\n",
  1214. __func__, i, ST[i]);
  1215. val = 0;
  1216. }
  1217. }
  1218. ST_EXIT:
  1219. x[0] = REG_CNTRL1_ADDR;
  1220. x[1] = 0x00;
  1221. k303c_mag_i2c_write(data, x, 1);
  1222. x[0] = REG_CNTRL3_ADDR;
  1223. x[1] = 0x00;
  1224. k303c_mag_i2c_write(data, x, 1);
  1225. k303c_mag_device_power_off(data);
  1226. if (en_state)
  1227. k303c_mag_enable(data);
  1228. if (val) {
  1229. pr_info("[SENSOR] %s :Self test: OK (%d, %d, %d)\n",
  1230. __func__, ST[0], ST[1], ST[2]);
  1231. ret = snprintf(buf, PAGE_SIZE, "1,%d,%d,%d\n",
  1232. ST[0], ST[1], ST[2]);
  1233. } else {
  1234. pr_info("[SENSOR] %s :Self test: NG (%d, %d, %d)\n",
  1235. __func__, ST[0], ST[1], ST[2]);
  1236. ret = snprintf(buf, PAGE_SIZE, "0,%d,%d,%d\n",
  1237. ST[0], ST[1], ST[2]);
  1238. }
  1239. return ret;
  1240. }
  1241. static ssize_t k303c_mag_status_show(struct device *dev,
  1242. struct device_attribute *attr, char *buf)
  1243. {
  1244. struct k303c_mag_p *data = dev_get_drvdata(dev);
  1245. int err;
  1246. u8 val;
  1247. pr_info("%s\n", __func__);
  1248. if (!atomic_read(&data->enabled_mag)) {
  1249. k303c_mag_device_power_on(data);
  1250. val = dataus_registers.who_am_i.address;
  1251. err = k303c_mag_i2c_read(data, &val, 1);
  1252. k303c_mag_device_power_off(data);
  1253. } else {
  1254. val = dataus_registers.who_am_i.address;
  1255. err = k303c_mag_i2c_read(data, &val, 1);
  1256. }
  1257. if (err < 0) {
  1258. pr_err("%s k303c_mag_i2c_read err:%d\n", __func__, err);
  1259. err = 0;
  1260. } else if (val != dataus_registers.who_am_i.value) {
  1261. pr_err("%s value val:%d err:%d\n", __func__, val, err);
  1262. err = 0;
  1263. } else {
  1264. err = 1;
  1265. }
  1266. return snprintf(buf, PAGE_SIZE, "%d,%d\n", err, 0);
  1267. }
  1268. static ssize_t k303c_mag_vendor_show(struct device *dev,
  1269. struct device_attribute *attr, char *buf)
  1270. {
  1271. pr_info("%s\n", __func__);
  1272. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
  1273. }
  1274. static DEVICE_ATTR(adc, S_IRUGO, k303c_mag_adc_show, NULL);
  1275. static DEVICE_ATTR(name, S_IRUGO, k303c_mag_name_show, NULL);
  1276. static DEVICE_ATTR(raw_data, S_IRUGO, k303c_mag_raw_data_show, NULL);
  1277. static DEVICE_ATTR(selftest, S_IRUGO, k303c_mag_selftest_show, NULL);
  1278. static DEVICE_ATTR(status, S_IRUGO, k303c_mag_status_show, NULL);
  1279. static DEVICE_ATTR(vendor, S_IRUGO, k303c_mag_vendor_show, NULL);
  1280. static struct device_attribute *sensor_attrs[] = {
  1281. &dev_attr_adc,
  1282. &dev_attr_name,
  1283. &dev_attr_raw_data,
  1284. &dev_attr_selftest,
  1285. &dev_attr_status,
  1286. &dev_attr_vendor,
  1287. NULL,
  1288. };
  1289. static int k303c_mag_probe(struct i2c_client *client,
  1290. const struct i2c_device_id *id)
  1291. {
  1292. struct k303c_mag_p *data;
  1293. u32 smbus_func = I2C_FUNC_SMBUS_BYTE_DATA |
  1294. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
  1295. int err = -1;
  1296. pr_err("[SENSOR] %s, Start!\n", __func__);
  1297. data = kzalloc(sizeof(struct k303c_mag_p), GFP_KERNEL);
  1298. if (data == NULL) {
  1299. err = -ENOMEM;
  1300. dev_err(&client->dev, "failed to kzalloc err:%d\n", err);
  1301. goto exit_kzalloc_failed;
  1302. }
  1303. data->use_smbus = 0;
  1304. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1305. dev_warn(&client->dev, "client not i2c capable\n");
  1306. if (i2c_check_functionality(client->adapter, smbus_func)) {
  1307. data->use_smbus = 1;
  1308. dev_warn(&client->dev, "client using SMBUS\n");
  1309. } else {
  1310. err = -ENODEV;
  1311. dev_err(&client->dev, "client nor SMBUS capable\n");
  1312. goto exit_check_functionality_failed;
  1313. }
  1314. }
  1315. if (data->mag_wq == 0)
  1316. data->mag_wq = create_workqueue("mag_wq");
  1317. hrtimer_init(&data->hr_timer_mag, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1318. data->hr_timer_mag.function = &poll_function_read_mag;
  1319. mutex_init(&data->lock);
  1320. mutex_lock(&data->lock);
  1321. data->client = client;
  1322. i2c_set_clientdata(client, data);
  1323. data->pdata = kmalloc(sizeof(*data->pdata), GFP_KERNEL);
  1324. if (data->pdata == NULL) {
  1325. err = -ENOMEM;
  1326. dev_err(&client->dev,
  1327. "failed to allocate memory for pdata: %d\n", err);
  1328. goto exit_pdata_kmalloc;
  1329. }
  1330. if (client->dev.platform_data == NULL) {
  1331. memcpy(data->pdata, &default_k303c_mag_pdata,
  1332. sizeof(*data->pdata));
  1333. dev_info(&client->dev,
  1334. "using default plaform_data for magnetometer\n");
  1335. } else {
  1336. memcpy(data->pdata, client->dev.platform_data,
  1337. sizeof(*data->pdata));
  1338. }
  1339. err = k303c_mag_parse_dt(data, &client->dev);
  1340. if (err < 0) {
  1341. pr_err("[SENSOR] %s, of_node error\n", __func__);
  1342. err = -ENODEV;
  1343. goto exit_of_node;
  1344. }
  1345. err = k303c_mag_validate_pdata(data);
  1346. if (err < 0) {
  1347. dev_err(&client->dev, "failed to k303c_mag_validate_pdata\n");
  1348. goto exit_kfree_pdata;
  1349. }
  1350. err = k303c_mag_hw_init(data);
  1351. if (err < 0) {
  1352. dev_err(&client->dev, "hw init failed: %d\n", err);
  1353. goto err_hw_init;
  1354. }
  1355. err = k303c_mag_device_power_on(data);
  1356. if (err < 0) {
  1357. dev_err(&client->dev,
  1358. "magnetometer power on failed: %d\n", err);
  1359. goto err_pdata_init;
  1360. }
  1361. err = k303c_mag_update_fs_range(data, data->pdata->fs_range);
  1362. if (err < 0) {
  1363. dev_err(&client->dev,
  1364. "update_fs_range on magnetometer failed\n");
  1365. goto err_power_off_mag;
  1366. }
  1367. err = k303c_mag_update_odr(data, data->pdata->poll_interval);
  1368. if (err < 0) {
  1369. dev_err(&client->dev, "update_odr on magnetometer failed\n");
  1370. goto err_power_off;
  1371. }
  1372. err = k303c_mag_input_init(data);
  1373. if (err < 0) {
  1374. dev_err(&client->dev, "magnetometer input init failed\n");
  1375. goto err_power_off;
  1376. }
  1377. sensors_register(data->factory_device, data, sensor_attrs, MODULE_NAME);
  1378. k303c_mag_device_power_off(data);
  1379. INIT_WORK(&data->input_work_mag, poll_function_work_mag);
  1380. mutex_unlock(&data->lock);
  1381. pr_info("[SENSOR] %s done!\n", __func__);
  1382. return 0;
  1383. err_power_off:
  1384. k303c_mag_input_cleanup(data);
  1385. err_power_off_mag:
  1386. k303c_mag_device_power_off(data);
  1387. err_hw_init:
  1388. err_pdata_init:
  1389. exit_kfree_pdata:
  1390. mutex_unlock(&data->lock);
  1391. if (!data->mag_wq) {
  1392. flush_workqueue(data->mag_wq);
  1393. destroy_workqueue(data->mag_wq);
  1394. }
  1395. exit_of_node:
  1396. exit_pdata_kmalloc:
  1397. exit_check_functionality_failed:
  1398. kfree(data);
  1399. exit_kzalloc_failed:
  1400. pr_err("[SENSOR] %s: Driver Init failed\n", K303C_MAG_DEV_NAME);
  1401. return err;
  1402. }
  1403. static int k303c_mag_resume(struct device *dev)
  1404. {
  1405. struct k303c_mag_p *data = dev_get_drvdata(dev);
  1406. if (data->on_before_suspend)
  1407. return k303c_mag_enable(data);
  1408. return 0;
  1409. }
  1410. static int k303c_mag_suspend(struct device *dev)
  1411. {
  1412. struct k303c_mag_p *data = dev_get_drvdata(dev);
  1413. data->on_before_suspend = atomic_read(&data->enabled_mag);
  1414. k303c_mag_disable(data);
  1415. return 0;
  1416. }
  1417. static int k303c_mag_remove(struct i2c_client *client)
  1418. {
  1419. struct k303c_mag_p *data = i2c_get_clientdata(client);
  1420. k303c_mag_disable(data);
  1421. k303c_mag_input_cleanup(data);
  1422. remove_sysfs_interfaces(&client->dev);
  1423. if (!data->mag_wq) {
  1424. flush_workqueue(data->mag_wq);
  1425. destroy_workqueue(data->mag_wq);
  1426. }
  1427. devm_regulator_put(data->reg_vdd);
  1428. devm_regulator_put(data->reg_vio);
  1429. kfree(data);
  1430. return 0;
  1431. }
  1432. static const struct i2c_device_id k303c_mag_id[] = {
  1433. { "k303c_mag", 0 },
  1434. { },
  1435. };
  1436. static struct of_device_id k303c_mag_table[] = {
  1437. { .compatible = "k303c_mag",},
  1438. {},
  1439. };
  1440. MODULE_DEVICE_TABLE(i2c, k303c_mag_id);
  1441. static const struct dev_pm_ops k303c_mag_pm_ops = {
  1442. .suspend = k303c_mag_suspend,
  1443. .resume = k303c_mag_resume
  1444. };
  1445. static struct i2c_driver k303c_mag_driver = {
  1446. .driver = {
  1447. .owner = THIS_MODULE,
  1448. .name = K303C_MAG_DEV_NAME,
  1449. .of_match_table = k303c_mag_table,
  1450. .pm = &k303c_mag_pm_ops
  1451. },
  1452. .probe = k303c_mag_probe,
  1453. .remove = k303c_mag_remove,
  1454. .id_table = k303c_mag_id,
  1455. };
  1456. static int __init k303c_mag_init(void)
  1457. {
  1458. return i2c_add_driver(&k303c_mag_driver);
  1459. }
  1460. static void __exit k303c_mag_exit(void)
  1461. {
  1462. i2c_del_driver(&k303c_mag_driver);
  1463. }
  1464. module_init(k303c_mag_init);
  1465. module_exit(k303c_mag_exit);
  1466. MODULE_DESCRIPTION("k303c magnetometer driver");
  1467. MODULE_AUTHOR("Matteo Dameno, Denis Ciocca, STMicroelectronics");
  1468. MODULE_LICENSE("GPL");