hts221.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /* BOSCH Pressure Sensor Driver
  2. *
  3. * This software is licensed under the terms of the GNU General Public
  4. * License version 2, as published by the Free Software Foundation, and
  5. * may be copied, distributed, and modified under those terms.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * History: V1.0 --- [2013.02.18]Driver creation
  13. * V1.1 --- [2013.03.14]Instead late_resume, use resume to make sure
  14. * driver resume is ealier than processes resume.
  15. * V1.2 --- [2013.03.26]Re-write i2c function to fix the bug that
  16. * i2c access error on MT6589 platform.
  17. */
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/gpio.h>
  22. #include "cust_hmdy.h"
  23. #include "hts221.h"
  24. #include "humidity.h"
  25. #define POWER_NONE_MACRO MT65XX_POWER_NONE
  26. static DEFINE_MUTEX(hts221_i2c_mutex);
  27. static DEFINE_MUTEX(hts221_op_mutex);
  28. /* sensor type */
  29. enum SENSOR_TYPE_ENUM {
  30. HTS221_TYPE = 0x0,
  31. INVALID_TYPE = 0xff
  32. };
  33. /* power mode */
  34. enum HTS_POWERMODE_ENUM {
  35. HTS_SUSPEND_MODE = 0,
  36. HTS_NORMAL_MODE = 1,
  37. HTS_UNDEFINED_POWERMODE = 0xff
  38. };
  39. /* trace */
  40. enum HTS_TRC {
  41. HTS_TRC_READ = 0x01,
  42. HTS_TRC_RAWDATA = 0x02,
  43. HTS_TRC_IOCTL = 0x04,
  44. HTS_TRC_FILTER = 0x08,
  45. };
  46. /* s/w filter */
  47. struct data_filter {
  48. u32 raw[C_MAX_FIR_LENGTH][HTS221_DATA_NUM];
  49. int sum[HTS221_DATA_NUM];
  50. int num;
  51. int idx;
  52. };
  53. /* hts221 calibration */
  54. struct hts221_calibration_data {
  55. u8 temperature_calibration[2], temperature_calibration2[2];
  56. int calibX0, calibX1, calibW0, calibW1;
  57. u16 calibY0, calibY1;
  58. u16 calibZ0a, calibZ0b, calibZ1a, calibZ1b;
  59. int calibZ0, calibZ1;
  60. int h_slope, h_b, t_slope, t_b;
  61. };
  62. /* hts221 i2c client data */
  63. struct hts221_i2c_data {
  64. struct i2c_client *client;
  65. struct hmdy_hw *hw;
  66. /* sensor info */
  67. u8 sensor_name[MAX_SENSOR_NAME];
  68. enum SENSOR_TYPE_ENUM sensor_type;
  69. enum HTS_POWERMODE_ENUM power_mode;
  70. struct hts221_calibration_data hts221_cali;
  71. /* calculated temperature correction coefficient */
  72. s32 t_fine;
  73. /*misc */
  74. atomic_t trace;
  75. atomic_t suspend;
  76. atomic_t filter;
  77. };
  78. #define HTS_TAG "[hts221] "
  79. #define HTS_FUN(f) pr_debug(HTS_TAG"%s\n", __func__)
  80. #define HTS_ERR(fmt, args...) \
  81. pr_err(HTS_TAG"%s %d : "fmt, __func__, __LINE__, ##args)
  82. #define HTS_LOG(fmt, args...) pr_debug(HTS_TAG fmt, ##args)
  83. static struct i2c_driver hts221_i2c_driver;
  84. static struct hts221_i2c_data *obj_i2c_data;
  85. static const struct i2c_device_id hts221_i2c_id[] = {
  86. {HTS_DEV_NAME, 0},
  87. {}
  88. };
  89. struct hmdy_hw hmdy_cust;
  90. static struct hmdy_hw *hw = &hmdy_cust;
  91. /* For alsp driver get cust info */
  92. struct hmdy_hw *get_cust_hmdy(void)
  93. {
  94. return &hmdy_cust;
  95. }
  96. static int hts221_local_init(void);
  97. static int hts221_remove(void);
  98. static int hts221_init_flag = -1;
  99. static struct hmdy_init_info hts221_init_info = {
  100. .name = "hts221",
  101. .init = hts221_local_init,
  102. .uninit = hts221_remove,
  103. };
  104. /* I2C operation functions */
  105. static int hts221_i2c_read_block(struct i2c_client *client, u8 addr, u8 *data, u8 len)
  106. {
  107. u8 beg = addr;
  108. int err = 0;
  109. struct i2c_msg msgs[2] = { {0}, {0} };
  110. mutex_lock(&hts221_i2c_mutex);
  111. msgs[0].addr = client->addr;
  112. msgs[0].flags = 0;
  113. msgs[0].len = 1;
  114. msgs[0].buf = &beg;
  115. msgs[1].addr = client->addr;
  116. msgs[1].flags = I2C_M_RD;
  117. msgs[1].len = len;
  118. msgs[1].buf = data;
  119. if (!client) {
  120. mutex_unlock(&hts221_i2c_mutex);
  121. return -EINVAL;
  122. } else if (len > C_I2C_FIFO_SIZE) {
  123. HTS_ERR(" length %d exceeds %d\n", len, C_I2C_FIFO_SIZE);
  124. mutex_unlock(&hts221_i2c_mutex);
  125. return -EINVAL;
  126. }
  127. err = i2c_transfer(client->adapter, msgs, sizeof(msgs) / sizeof(msgs[0]));
  128. if (err != 2) {
  129. HTS_ERR("i2c_transfer error: (%d %p %d) %d\n", addr, data, len, err);
  130. err = -EIO;
  131. } else {
  132. err = 0;
  133. }
  134. mutex_unlock(&hts221_i2c_mutex);
  135. return err;
  136. }
  137. static int hts221_i2c_write_block(struct i2c_client *client, u8 addr, u8 *data, u8 len)
  138. { /*because address also occupies one byte, the maximum length for write is 7 bytes */
  139. int err, idx, num;
  140. char buf[C_I2C_FIFO_SIZE];
  141. err = 0;
  142. mutex_lock(&hts221_i2c_mutex);
  143. if (!client) {
  144. mutex_unlock(&hts221_i2c_mutex);
  145. return -EINVAL;
  146. } else if (len >= C_I2C_FIFO_SIZE) {
  147. HTS_ERR(" length %d exceeds %d\n", len, C_I2C_FIFO_SIZE);
  148. mutex_unlock(&hts221_i2c_mutex);
  149. return -EINVAL;
  150. }
  151. num = 0;
  152. buf[num++] = addr;
  153. for (idx = 0; idx < len; idx++)
  154. buf[num++] = data[idx];
  155. err = i2c_master_send(client, buf, num);
  156. if (err < 0) {
  157. HTS_ERR("send command error!!\n");
  158. mutex_unlock(&hts221_i2c_mutex);
  159. return -EFAULT;
  160. }
  161. mutex_unlock(&hts221_i2c_mutex);
  162. return err;
  163. }
  164. static void hts221_power(struct hmdy_hw *hw, unsigned int on)
  165. {
  166. }
  167. static int hts221_check_ID(struct i2c_client *client)
  168. {
  169. int res = 0;
  170. u8 buf[2];
  171. HTS_FUN();
  172. res = hts221_i2c_read_block(client, REG_WHOAMI_ADDR, buf, 1);
  173. if (res < 0)
  174. goto error;
  175. HTS_LOG("HTS211 device ID is 0X%XH\n", buf[0]);
  176. return res;
  177. error:
  178. HTS_ERR("hts221_check_ID failed 0x%02x,0x%02x: %d\n", buf[0], buf[1], res);
  179. return res;
  180. }
  181. static int hts221_set_sampling_period(struct i2c_client *client, u8 new_sampling, u8 BDU)
  182. {
  183. int res = 0;
  184. u8 buf[2];
  185. HTS_FUN();
  186. res = hts221_i2c_read_block(client, REG_CNTRL1_ADDR, buf, 1);
  187. if (res < 0)
  188. goto error;
  189. buf[0] = (buf[0] & HTS221_ODR_MASK) | new_sampling;
  190. buf[0] = (buf[0] & HTS221_BDU_MASK) | BDU;
  191. res = hts221_i2c_write_block(client, REG_CNTRL1_ADDR, buf, 1);
  192. if (res < 0)
  193. goto error;
  194. return res;
  195. error:
  196. HTS_ERR("update humidity resolution failed 0x%02x,0x%02x: %d\n", buf[0], buf[1], res);
  197. return res;
  198. }
  199. static int hts221_set_resolution(struct i2c_client *client, u8 new_resolution, u8 mask)
  200. {
  201. int res = 0;
  202. u8 buf[2];
  203. HTS_FUN();
  204. res = hts221_i2c_read_block(client, REG_AVCONFIG_ADDR, buf, 1);
  205. if (res < 0)
  206. goto error;
  207. buf[0] = (buf[0] & mask) | new_resolution;
  208. res = hts221_i2c_write_block(client, REG_AVCONFIG_ADDR, buf, 1);
  209. if (res < 0)
  210. goto error;
  211. return res;
  212. error:
  213. HTS_ERR("update humidity resolution failed 0x%02x,0x%02x: %d\n", buf[0], buf[1], res);
  214. return res;
  215. }
  216. static int hts221_get_calibration_data(struct i2c_client *client)
  217. {
  218. int err = 0;
  219. u8 humidity_calibration[2], temperature_calibration[2];
  220. HTS_FUN();
  221. err = hts221_i2c_read_block(client, REG_0RH_CAL_X_H + 0x80, humidity_calibration, 2);
  222. if (err < 0)
  223. return err;
  224. obj_i2c_data->hts221_cali.calibX0 = ((s32) ((s16) ((humidity_calibration[1] << 8) |
  225. (humidity_calibration[0]))));
  226. err = hts221_i2c_read_block(client, REG_1RH_CAL_X_H + 0x80, humidity_calibration, 2);
  227. if (err < 0)
  228. return err;
  229. obj_i2c_data->hts221_cali.calibX1 = ((s32) ((s16) ((humidity_calibration[1] << 8) |
  230. (humidity_calibration[0]))));
  231. err = hts221_i2c_read_block(client, REG_0RH_CAL_Y_H, humidity_calibration, 1);
  232. if (err < 0)
  233. return err;
  234. obj_i2c_data->hts221_cali.calibY0 = (u16) humidity_calibration[0];
  235. err = hts221_i2c_read_block(client, REG_1RH_CAL_Y_H, humidity_calibration, 1);
  236. if (err < 0)
  237. return err;
  238. obj_i2c_data->hts221_cali.calibY1 = (u16) humidity_calibration[0];
  239. obj_i2c_data->hts221_cali.h_slope =
  240. ((obj_i2c_data->hts221_cali.calibY1 -
  241. obj_i2c_data->hts221_cali.calibY0) * 8000) / (obj_i2c_data->hts221_cali.calibX1 -
  242. obj_i2c_data->hts221_cali.calibX0);
  243. obj_i2c_data->hts221_cali.h_b =
  244. (((obj_i2c_data->hts221_cali.calibX1 * obj_i2c_data->hts221_cali.calibY0) -
  245. (obj_i2c_data->hts221_cali.calibX0 * obj_i2c_data->hts221_cali.calibY1)) * 1000) /
  246. (obj_i2c_data->hts221_cali.calibX1 - obj_i2c_data->hts221_cali.calibX0);
  247. obj_i2c_data->hts221_cali.h_b = obj_i2c_data->hts221_cali.h_b * 8;
  248. err = hts221_i2c_read_block(client, REG_0T_CAL_X_L + 0x80, temperature_calibration, 2);
  249. if (err < 0)
  250. return err;
  251. obj_i2c_data->hts221_cali.calibW0 = ((s32) ((s16) ((temperature_calibration[1] << 8) |
  252. (temperature_calibration[0]))));
  253. err = hts221_i2c_read_block(client, REG_1T_CAL_X_L + 0x80, temperature_calibration, 2);
  254. if (err < 0)
  255. return err;
  256. obj_i2c_data->hts221_cali.calibW1 = ((s32) ((s16) ((temperature_calibration[1] << 8) |
  257. (temperature_calibration[0]))));
  258. err = hts221_i2c_read_block(client, REG_0T_CAL_Y_H, temperature_calibration, 1);
  259. if (err < 0)
  260. return err;
  261. obj_i2c_data->hts221_cali.calibZ0a = (u16) temperature_calibration[0];
  262. err = hts221_i2c_read_block(client, REG_T1_T0_CAL_Y_H, temperature_calibration, 1);
  263. if (err < 0)
  264. return err;
  265. obj_i2c_data->hts221_cali.calibZ0b = (u16) (temperature_calibration[0] & (0x3));
  266. obj_i2c_data->hts221_cali.calibZ0 =
  267. ((s32) ((obj_i2c_data->hts221_cali.calibZ0b << 8) | (obj_i2c_data->hts221_cali.calibZ0a)));
  268. err = hts221_i2c_read_block(client, REG_1T_CAL_Y_H, temperature_calibration, 1);
  269. if (err < 0)
  270. return err;
  271. obj_i2c_data->hts221_cali.calibZ1a = (u16) temperature_calibration[0];
  272. err = hts221_i2c_read_block(client, REG_T1_T0_CAL_Y_H, temperature_calibration, 1);
  273. if (err < 0)
  274. return err;
  275. obj_i2c_data->hts221_cali.calibZ1b = (u16) (temperature_calibration[0] & (0xC));
  276. obj_i2c_data->hts221_cali.calibZ1b = obj_i2c_data->hts221_cali.calibZ1b >> 2;
  277. obj_i2c_data->hts221_cali.calibZ1 =
  278. ((s32) ((obj_i2c_data->hts221_cali.calibZ1b << 8) | (obj_i2c_data->hts221_cali.calibZ1a)));
  279. obj_i2c_data->hts221_cali.t_slope =
  280. ((obj_i2c_data->hts221_cali.calibZ1 -
  281. obj_i2c_data->hts221_cali.calibZ0) * 8000) / (obj_i2c_data->hts221_cali.calibW1 -
  282. obj_i2c_data->hts221_cali.calibW0);
  283. obj_i2c_data->hts221_cali.t_b =
  284. (((obj_i2c_data->hts221_cali.calibW1 * obj_i2c_data->hts221_cali.calibZ0) -
  285. (obj_i2c_data->hts221_cali.calibW0 * obj_i2c_data->hts221_cali.calibZ1)) * 1000) /
  286. (obj_i2c_data->hts221_cali.calibW1 - obj_i2c_data->hts221_cali.calibW0);
  287. obj_i2c_data->hts221_cali.t_b = obj_i2c_data->hts221_cali.t_b * 8;
  288. #ifdef DEBUF
  289. HTS_LOG("reading calibX0=%X calibX1=%X\n", obj_i2c_data->hts221_cali.calibX0,
  290. obj_i2c_data->hts221_cali.calibX1);
  291. HTS_LOG("reading calibX0=%d calibX1=%d\n", obj_i2c_data->hts221_cali.calibX0,
  292. obj_i2c_data->hts221_cali.calibX1);
  293. HTS_LOG("reading calibW0=%X calibW1=%X\n", obj_i2c_data->hts221_cali.calibW0,
  294. obj_i2c_data->hts221_cali.calibW1);
  295. HTS_LOG("reading calibW0=%d calibW1=%d\n", obj_i2c_data->hts221_cali.calibW0,
  296. obj_i2c_data->hts221_cali.calibW1);
  297. HTS_LOG("reading calibY0=%X calibY1=%X\n", obj_i2c_data->hts221_cali.calibY0,
  298. obj_i2c_data->hts221_cali.calibY1);
  299. HTS_LOG("reading calibY0=%u calibY1=%u\n", obj_i2c_data->hts221_cali.calibY0,
  300. obj_i2c_data->hts221_cali.calibY1);
  301. HTS_LOG("reading calibZ0a=%X calibZ0b=%X calibZ0=%X\n", obj_i2c_data->hts221_cali.calibZ0a,
  302. obj_i2c_data->hts221_cali.calibZ0b, obj_i2c_data->hts221_cali.calibZ0);
  303. HTS_LOG("reading calibZ0a=%u calibZ0b=%u calibZ0=%d\n", obj_i2c_data->hts221_cali.calibZ0a,
  304. obj_i2c_data->hts221_cali.calibZ0b, obj_i2c_data->hts221_cali.calibZ0);
  305. HTS_LOG("reading calibZ1a=%X calibZ1b=%X calibZ1=%X\n", obj_i2c_data->hts221_cali.calibZ1a,
  306. obj_i2c_data->hts221_cali.calibZ1b, obj_i2c_data->hts221_cali.calibZ1);
  307. HTS_LOG("reading calibZ1a=%u calibZ1b=%u calibZ1=%d\n", obj_i2c_data->hts221_cali.calibZ1a,
  308. obj_i2c_data->hts221_cali.calibZ1b, obj_i2c_data->hts221_cali.calibZ1);
  309. HTS_LOG("reading t_slope=%X t_b=%X h_slope=%X h_b=%X\n", obj_i2c_data->hts221_cali.t_slope,
  310. obj_i2c_data->hts221_cali.t_b, obj_i2c_data->hts221_cali.h_slope, obj_i2c_data->hts221_cali.h_b);
  311. HTS_LOG("reading t_slope=%d t_b=%d h_slope=%d h_b=%d\n", obj_i2c_data->hts221_cali.t_slope,
  312. obj_i2c_data->hts221_cali.t_b, obj_i2c_data->hts221_cali.h_slope, obj_i2c_data->hts221_cali.h_b);
  313. #endif
  314. return 0;
  315. }
  316. static int hts221_device_power_off(struct i2c_client *client)
  317. {
  318. int res = 0;
  319. u8 buf[2];
  320. HTS_FUN();
  321. res = hts221_i2c_read_block(client, REG_CNTRL1_ADDR, buf, 1);
  322. if (res < 0)
  323. goto error;
  324. buf[0] = (buf[0] & HTS221_POWER_MASK) | DISABLE_SENSOR;
  325. res = hts221_i2c_write_block(client, REG_CNTRL1_ADDR, buf, 1);
  326. if (res < 0)
  327. goto error;
  328. return res;
  329. error:
  330. HTS_ERR("humidity power failed 0x%02x,0x%02x: %d\n", buf[0], buf[1], res);
  331. return res;
  332. }
  333. static int hts221_device_power_on(struct i2c_client *client)
  334. {
  335. int res = 0;
  336. u8 buf[2];
  337. HTS_FUN();
  338. res = hts221_i2c_read_block(client, REG_CNTRL1_ADDR, buf, 1);
  339. if (res < 0)
  340. goto error;
  341. buf[0] = (buf[0] & HTS221_POWER_MASK) | ENABLE_SENSOR;
  342. res = hts221_i2c_write_block(client, REG_CNTRL1_ADDR, buf, 1);
  343. if (res < 0)
  344. goto error;
  345. return res;
  346. error:
  347. HTS_ERR("humidity power failed 0x%02x,0x%02x: %d\n", buf[0], buf[1], res);
  348. return res;
  349. }
  350. static int hts221_set_powermode(struct i2c_client *client, enum HTS_POWERMODE_ENUM power_mode)
  351. {
  352. int err = 0;
  353. HTS_FUN();
  354. if (power_mode == HTS_NORMAL_MODE) {
  355. err = hts221_device_power_on(client);
  356. if (err < 0)
  357. HTS_LOG("HTS221 power on fail\n");
  358. } else {
  359. err = hts221_device_power_off(client);
  360. if (err < 0)
  361. HTS_LOG("HTS221 power off fail\n");
  362. }
  363. return 0;
  364. }
  365. /*****************************************************
  366. * Linear interpolation: (x0,y0) (x1,y1) y = ax+b
  367. *
  368. * a = (y1-y0)/(x1-x0)
  369. * b = (x1*y0-x0*y1)/(x1-x0)
  370. *
  371. * result = ((y1-y0)*x+((x1*y0)-(x0*y1)))/(x1-x0)
  372. *
  373. * For Humidity
  374. * (x1,y1) = (H1_T0_OUT, H1_RH)
  375. * (x0,y0) = (H0_T0_OUT, H0_RH)
  376. * x = H_OUT
  377. * For Temperature
  378. * (x1,y1) = (T1_OUT, T1_DegC)
  379. * (x0,y0) = (T0_OUT, T0_DegC)
  380. * x = T_OUT
  381. ******************************************************/
  382. static int hts221_convert(int slope, int b_gen, int *x, int type)
  383. {
  384. int err = 0;
  385. int X = 0;
  386. X = *x;
  387. *x = ((slope * X) + b_gen);
  388. if (type == 0)
  389. *x = (*x) >> 4; /*for Humidity, m RH */
  390. else
  391. *x = (*x) >> 6; /*for Humidity, m RH */
  392. return err;
  393. }
  394. static int hts221_start_convert(struct i2c_client *client)
  395. {
  396. int err = 0;
  397. u8 buff[2];
  398. HTS_FUN();
  399. buff[0] = START_NEW_CONVERT;
  400. err = hts221_i2c_write_block(client, REG_CNTRL2_ADDR, buff, 1);
  401. if (err < 0)
  402. return err;
  403. return err;
  404. }
  405. /*
  406. *get compensated temperature
  407. *unit:1000 degrees centigrade
  408. */
  409. static int hts221_get_temperature(struct i2c_client *client, char *buf, int bufsize)
  410. {
  411. int err = 0;
  412. u8 temperature_data[2];
  413. int data_t = 0;
  414. HTS_FUN();
  415. err = hts221_i2c_read_block(client, REG_T_OUT_L + 0x80, temperature_data, 2);
  416. if (err < 0)
  417. return err;
  418. data_t = ((s32) ((s16) ((temperature_data[1] << 8) | (temperature_data[0]))));
  419. err = hts221_convert(obj_i2c_data->hts221_cali.t_slope, obj_i2c_data->hts221_cali.t_b, &data_t, 1);
  420. if (err < 0)
  421. return err;
  422. HTS_LOG("hts221 get temperature: %d rH\n", data_t);
  423. sprintf(buf, "%08x", data_t);
  424. return 0;
  425. }
  426. /*
  427. *get compensated humidity
  428. *unit: 1000 %rH
  429. */
  430. static int hts221_get_humidity(struct i2c_client *client, char *buf, int bufsize)
  431. {
  432. int err = 0;
  433. u8 humidity_data[2];
  434. int data_h = 0;
  435. HTS_FUN();
  436. err = hts221_start_convert(client);
  437. if (err < 0) {
  438. HTS_ERR("ERROR\n");
  439. return err;
  440. }
  441. err = hts221_i2c_read_block(client, REG_H_OUT_L + 0x80, humidity_data, 2);
  442. if (err < 0)
  443. return err;
  444. data_h = ((s32) ((s16) ((humidity_data[1] << 8) | (humidity_data[0]))));
  445. HTS_LOG("humidity raw data: 0x%x\n", data_h);
  446. err = hts221_convert(obj_i2c_data->hts221_cali.h_slope, obj_i2c_data->hts221_cali.h_b, &data_h, 0);
  447. if (err < 0)
  448. return err;
  449. HTS_LOG("hts221 get humidity: %d rH\n", data_h);
  450. sprintf(buf, "%08x", data_h);
  451. return 0;
  452. }
  453. /* hts221 setting initialization */
  454. static int hts221_init_client(struct i2c_client *client)
  455. {
  456. int res = 0;
  457. HTS_FUN();
  458. res = hts221_check_ID(client);
  459. if (res < 0) {
  460. HTS_ERR("Error reading WHO_AM_I: device is available\n");
  461. return res;
  462. }
  463. res = hts221_set_sampling_period(client, HTS221_ODR_ONE_SHOT, HTS221_BDU);
  464. if (res < 0) {
  465. HTS_ERR("Error hts221_set_sampling_period\n");
  466. return res;
  467. }
  468. res = hts221_set_powermode(client, HTS_NORMAL_MODE);
  469. if (res < 0) {
  470. HTS_ERR("Error hts221_set_powermode\n");
  471. return res;
  472. }
  473. res = hts221_set_resolution(client, HTS221_H_RESOLUTION_32, HTS221_H_RESOLUTION_MASK);
  474. if (res < 0) {
  475. HTS_ERR("Error hts221_set_resolution humidity\n");
  476. return res;
  477. }
  478. res = hts221_set_resolution(client, HTS221_T_RESOLUTION_16, HTS221_T_RESOLUTION_MASK);
  479. if (res < 0) {
  480. HTS_ERR("Error hts221_set_resolution humidity\n");
  481. return res;
  482. }
  483. res = hts221_set_powermode(client, HTS_SUSPEND_MODE);
  484. if (res < 0) {
  485. HTS_ERR("Error hts221_set_powermode\n");
  486. return res;
  487. }
  488. res = hts221_get_calibration_data(client);
  489. if (res < 0) {
  490. HTS_ERR("Error hts221_get_calibration_data\n");
  491. return res;
  492. }
  493. return 0;
  494. }
  495. static ssize_t show_chipinfo_value(struct device_driver *ddri, char *buf)
  496. {
  497. struct hts221_i2c_data *obj = obj_i2c_data;
  498. if (NULL == obj) {
  499. HTS_ERR("hts221 i2c data pointer is null\n");
  500. return 0;
  501. }
  502. return snprintf(buf, PAGE_SIZE, "%s\n", obj->sensor_name);
  503. }
  504. static ssize_t show_sensordata_value(struct device_driver *ddri, char *buf)
  505. {
  506. struct hts221_i2c_data *obj = obj_i2c_data;
  507. char strbuf[HTS221_BUFSIZE] = "";
  508. if (NULL == obj) {
  509. HTS_ERR("hts221 i2c data pointer is null\n");
  510. return 0;
  511. }
  512. hts221_get_humidity(obj->client, strbuf, HTS221_BUFSIZE);
  513. return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
  514. }
  515. static ssize_t show_trace_value(struct device_driver *ddri, char *buf)
  516. {
  517. ssize_t res = 0;
  518. struct hts221_i2c_data *obj = obj_i2c_data;
  519. if (obj == NULL) {
  520. HTS_ERR("hts221 i2c data pointer is null\n");
  521. return 0;
  522. }
  523. res = snprintf(buf, PAGE_SIZE, "0x%04X\n", atomic_read(&obj->trace));
  524. return res;
  525. }
  526. static ssize_t store_trace_value(struct device_driver *ddri, const char *buf, size_t count)
  527. {
  528. struct hts221_i2c_data *obj = obj_i2c_data;
  529. int trace = 0;
  530. if (obj == NULL) {
  531. HTS_ERR("i2c_data obj is null\n");
  532. return 0;
  533. }
  534. if (1 == sscanf(buf, "0x%x", &trace))
  535. atomic_set(&obj->trace, trace);
  536. else
  537. HTS_ERR("invalid content: '%s', length = %d\n", buf, (int)count);
  538. return count;
  539. }
  540. static ssize_t show_status_value(struct device_driver *ddri, char *buf)
  541. {
  542. ssize_t len = 0;
  543. struct hts221_i2c_data *obj = obj_i2c_data;
  544. if (obj == NULL) {
  545. HTS_ERR("hts221 i2c data pointer is null\n");
  546. return 0;
  547. }
  548. if (obj->hw)
  549. len += snprintf(buf + len, PAGE_SIZE - len, "CUST: %d %d (%d %d)\n",
  550. obj->hw->i2c_num, obj->hw->direction, obj->hw->power_id, obj->hw->power_vol);
  551. else
  552. len += snprintf(buf + len, PAGE_SIZE - len, "CUST: NULL\n");
  553. len += snprintf(buf + len, PAGE_SIZE - len, "i2c addr:%#x,ver:%s\n", obj->client->addr, HTS_DRIVER_VERSION);
  554. return len;
  555. }
  556. static ssize_t show_power_mode_value(struct device_driver *ddri, char *buf)
  557. {
  558. ssize_t len = 0;
  559. struct hts221_i2c_data *obj = obj_i2c_data;
  560. if (obj == NULL) {
  561. HTS_ERR("hts221 i2c data pointer is null\n");
  562. return 0;
  563. }
  564. len += snprintf(buf + len, PAGE_SIZE - len, "%s mode\n",
  565. obj->power_mode == HTS_NORMAL_MODE ? "normal" : "suspend");
  566. return len;
  567. }
  568. static ssize_t store_power_mode_value(struct device_driver *ddri, const char *buf, size_t count)
  569. {
  570. struct hts221_i2c_data *obj = obj_i2c_data;
  571. unsigned long power_mode = 0;
  572. int err = 0;
  573. if (obj == NULL) {
  574. HTS_ERR("hts221 i2c data pointer is null\n");
  575. return 0;
  576. }
  577. err = kstrtoul(buf, 10, &power_mode);
  578. if (err == 0) {
  579. err = hts221_set_powermode(obj->client, (enum HTS_POWERMODE_ENUM)(!!(power_mode)));
  580. if (err)
  581. return err;
  582. return count;
  583. }
  584. return err;
  585. }
  586. static DRIVER_ATTR(chipinfo, S_IRUGO, show_chipinfo_value, NULL);
  587. static DRIVER_ATTR(sensordata, S_IRUGO, show_sensordata_value, NULL);
  588. static DRIVER_ATTR(trace, S_IWUSR | S_IRUGO, show_trace_value, store_trace_value);
  589. static DRIVER_ATTR(status, S_IRUGO, show_status_value, NULL);
  590. static DRIVER_ATTR(powermode, S_IWUSR | S_IRUGO, show_power_mode_value, store_power_mode_value);
  591. static struct driver_attribute *hts221_attr_list[] = {
  592. &driver_attr_chipinfo, /* chip information */
  593. &driver_attr_sensordata, /* dump sensor data */
  594. &driver_attr_trace, /* trace log */
  595. &driver_attr_status, /* cust setting */
  596. &driver_attr_powermode, /* power mode */
  597. };
  598. static int hts221_create_attr(struct device_driver *driver)
  599. {
  600. int idx = 0, err = 0;
  601. int num = (int)(sizeof(hts221_attr_list) / sizeof(hts221_attr_list[0]));
  602. HTS_FUN();
  603. if (NULL == driver)
  604. return -EINVAL;
  605. for (idx = 0; idx < num; idx++) {
  606. err = driver_create_file(driver, hts221_attr_list[idx]);
  607. if (err) {
  608. HTS_ERR("driver_create_file (%s) = %d\n", hts221_attr_list[idx]->attr.name, err);
  609. break;
  610. }
  611. }
  612. return err;
  613. }
  614. static int hts221_delete_attr(struct device_driver *driver)
  615. {
  616. int idx = 0, err = 0;
  617. int num = (int)(sizeof(hts221_attr_list) / sizeof(hts221_attr_list[0]));
  618. if (NULL == driver)
  619. return -EINVAL;
  620. for (idx = 0; idx < num; idx++)
  621. driver_remove_file(driver, hts221_attr_list[idx]);
  622. return err;
  623. }
  624. static int hts221_open(struct inode *inode, struct file *file)
  625. {
  626. file->private_data = obj_i2c_data;
  627. if (file->private_data == NULL) {
  628. HTS_ERR("null pointer\n");
  629. return -EINVAL;
  630. }
  631. return nonseekable_open(inode, file);
  632. }
  633. static int hts221_release(struct inode *inode, struct file *file)
  634. {
  635. file->private_data = NULL;
  636. return 0;
  637. }
  638. static long hts221_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  639. {
  640. struct hts221_i2c_data *obj = (struct hts221_i2c_data *)file->private_data;
  641. struct i2c_client *client = obj->client;
  642. char strbuf[HTS221_BUFSIZE];
  643. u32 dat = 0;
  644. void __user *data;
  645. int err = 0;
  646. if (_IOC_DIR(cmd) & _IOC_READ)
  647. err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
  648. else if (_IOC_DIR(cmd) & _IOC_WRITE)
  649. err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
  650. if (err) {
  651. HTS_ERR("access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
  652. return -EFAULT;
  653. }
  654. switch (cmd) {
  655. case HUMIDITY_IOCTL_INIT:
  656. hts221_init_client(client);
  657. err = hts221_set_powermode(client, HTS_NORMAL_MODE);
  658. if (err) {
  659. err = -EFAULT;
  660. break;
  661. }
  662. break;
  663. case HUMIDITY_IOCTL_READ_CHIPINFO:
  664. data = (void __user *)arg;
  665. if (NULL == data) {
  666. err = -EINVAL;
  667. break;
  668. }
  669. strcpy(strbuf, obj->sensor_name);
  670. if (copy_to_user(data, strbuf, strlen(strbuf) + 1)) {
  671. err = -EFAULT;
  672. break;
  673. }
  674. break;
  675. case HUMIDITY_GET_HMDY_DATA:
  676. data = (void __user *)arg;
  677. if (NULL == data) {
  678. err = -EINVAL;
  679. break;
  680. }
  681. hts221_get_humidity(client, strbuf, HTS221_BUFSIZE);
  682. err = kstrtoint(strbuf, 16, &dat);
  683. if (err == 0) {
  684. if (copy_to_user(data, &dat, sizeof(dat))) {
  685. err = -EFAULT;
  686. break;
  687. }
  688. }
  689. break;
  690. case HUMIDITY_GET_TEMP_DATA:
  691. data = (void __user *)arg;
  692. if (NULL == data) {
  693. err = -EINVAL;
  694. break;
  695. }
  696. hts221_get_temperature(client, strbuf, HTS221_BUFSIZE);
  697. err = kstrtoint(strbuf, 16, &dat);
  698. if (err == 0) {
  699. if (copy_to_user(data, &dat, sizeof(dat))) {
  700. err = -EFAULT;
  701. break;
  702. }
  703. }
  704. break;
  705. default:
  706. HTS_ERR("unknown IOCTL: 0x%08x\n", cmd);
  707. err = -ENOIOCTLCMD;
  708. break;
  709. }
  710. return err;
  711. }
  712. static const struct file_operations hts221_fops = {
  713. .owner = THIS_MODULE,
  714. .open = hts221_open,
  715. .release = hts221_release,
  716. .unlocked_ioctl = hts221_unlocked_ioctl,
  717. };
  718. static struct miscdevice hts221_device = {
  719. .minor = MISC_DYNAMIC_MINOR,
  720. .name = "humidity",
  721. .fops = &hts221_fops,
  722. };
  723. static int hts221_suspend(struct i2c_client *client, pm_message_t msg)
  724. {
  725. struct hts221_i2c_data *obj = i2c_get_clientdata(client);
  726. int err = 0;
  727. HTS_FUN();
  728. mutex_lock(&hts221_op_mutex);
  729. if (msg.event == PM_EVENT_SUSPEND) {
  730. if (NULL == obj) {
  731. HTS_ERR("null pointer\n");
  732. mutex_unlock(&hts221_op_mutex);
  733. return -EINVAL;
  734. }
  735. atomic_set(&obj->suspend, 1);
  736. err = hts221_set_powermode(obj->client, HTS_SUSPEND_MODE);
  737. if (err) {
  738. HTS_ERR("hts221 set suspend mode failed, err = %d\n", err);
  739. mutex_unlock(&hts221_op_mutex);
  740. return err;
  741. }
  742. hts221_power(obj->hw, 0);
  743. }
  744. mutex_unlock(&hts221_op_mutex);
  745. return err;
  746. }
  747. static int hts221_resume(struct i2c_client *client)
  748. {
  749. struct hts221_i2c_data *obj = i2c_get_clientdata(client);
  750. int err = 0;
  751. HTS_FUN();
  752. mutex_lock(&hts221_op_mutex);
  753. if (NULL == obj) {
  754. HTS_ERR("null pointer\n");
  755. mutex_unlock(&hts221_op_mutex);
  756. return -EINVAL;
  757. }
  758. hts221_power(obj->hw, 1);
  759. err = hts221_init_client(obj->client);
  760. if (err) {
  761. HTS_ERR("initialize client fail\n");
  762. mutex_unlock(&hts221_op_mutex);
  763. return err;
  764. }
  765. atomic_set(&obj->suspend, 0);
  766. mutex_unlock(&hts221_op_mutex);
  767. return 0;
  768. }
  769. static int hts221_i2c_detect(struct i2c_client *client, struct i2c_board_info *info)
  770. {
  771. strcpy(info->type, HTS_DEV_NAME);
  772. return 0;
  773. }
  774. static int hts221_open_report_data(int open)
  775. {
  776. return 0;
  777. }
  778. static int hts221_enable_nodata(int en)
  779. {
  780. int res = 0;
  781. int retry = 0;
  782. bool power = false;
  783. HTS_FUN();
  784. if (1 == en)
  785. power = true;
  786. if (0 == en)
  787. power = false;
  788. for (retry = 0; retry < 3; retry++) {
  789. res = hts221_set_powermode(obj_i2c_data->client, (enum HTS_POWERMODE_ENUM)(!!power));
  790. if (res == 0) {
  791. HTS_LOG("hts221_set_powermode done\n");
  792. break;
  793. }
  794. HTS_ERR("hts221_set_powermode fail\n");
  795. }
  796. if (res != 0) {
  797. HTS_ERR("hts221_set_powermode fail!\n");
  798. return -1;
  799. }
  800. HTS_LOG("hts221_set_powermode OK!\n");
  801. return 0;
  802. }
  803. static int hts221_set_delay(u64 ns)
  804. {
  805. HTS_FUN();
  806. return 0;
  807. }
  808. static int hts221_get_data(int *value, int *status)
  809. {
  810. char buff[HTS221_BUFSIZE];
  811. int err = 0;
  812. HTS_FUN();
  813. err = hts221_get_humidity(obj_i2c_data->client, buff, HTS221_BUFSIZE);
  814. if (err) {
  815. HTS_ERR("get compensated humidity value failed," "err = %d\n", err);
  816. return -1;
  817. }
  818. err = kstrtoint(buff, 16, value);
  819. if (err == 0)
  820. *status = SENSOR_STATUS_ACCURACY_MEDIUM;
  821. return 0;
  822. }
  823. static int hts221_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  824. {
  825. struct hts221_i2c_data *obj;
  826. struct hmdy_control_path ctl = { 0 };
  827. struct hmdy_data_path data = { 0 };
  828. int err = 0;
  829. HTS_FUN();
  830. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  831. if (!obj) {
  832. err = -ENOMEM;
  833. goto exit;
  834. }
  835. obj->hw = hw;
  836. obj_i2c_data = obj;
  837. obj->client = client;
  838. i2c_set_clientdata(client, obj);
  839. atomic_set(&obj->trace, 0);
  840. atomic_set(&obj->suspend, 0);
  841. obj->power_mode = HTS_UNDEFINED_POWERMODE;
  842. err = hts221_init_client(client);
  843. if (err)
  844. goto exit_init_client_failed;
  845. err = misc_register(&hts221_device);
  846. if (err) {
  847. HTS_ERR("misc device register failed, err = %d\n", err);
  848. goto exit_misc_device_register_failed;
  849. }
  850. ctl.is_use_common_factory = false;
  851. err = hts221_create_attr(&(hts221_init_info.platform_diver_addr->driver));
  852. if (err) {
  853. HTS_ERR("create attribute failed, err = %d\n", err);
  854. goto exit_create_attr_failed;
  855. }
  856. ctl.open_report_data = hts221_open_report_data;
  857. ctl.enable_nodata = hts221_enable_nodata;
  858. ctl.set_delay = hts221_set_delay;
  859. ctl.is_report_input_direct = false;
  860. ctl.is_support_batch = obj->hw->is_batch_supported;
  861. err = hmdy_register_control_path(&ctl);
  862. if (err) {
  863. HTS_ERR("register hmdy control path err\n");
  864. goto exit_hwmsen_attach_pressure_failed;
  865. }
  866. data.get_data = hts221_get_data;
  867. data.vender_div = 1000;
  868. err = hmdy_register_data_path(&data);
  869. if (err) {
  870. HTS_ERR("hmdy_register_data_path failed, err = %d\n", err);
  871. goto exit_hwmsen_attach_pressure_failed;
  872. }
  873. err = batch_register_support_info(ID_HUMIDITY, obj->hw->is_batch_supported, data.vender_div, 0);
  874. if (err) {
  875. HTS_ERR("register hmdy batch support err = %d\n", err);
  876. goto exit_hwmsen_attach_pressure_failed;
  877. }
  878. hts221_init_flag = 0;
  879. HTS_LOG("%s: OK\n", __func__);
  880. return 0;
  881. exit_hwmsen_attach_pressure_failed:
  882. hts221_delete_attr(&(hts221_init_info.platform_diver_addr->driver));
  883. exit_create_attr_failed:
  884. misc_deregister(&hts221_device);
  885. exit_misc_device_register_failed:
  886. exit_init_client_failed:
  887. kfree(obj);
  888. exit:
  889. HTS_ERR("err = %d\n", err);
  890. hts221_init_flag = -1;
  891. return err;
  892. }
  893. static int hts221_i2c_remove(struct i2c_client *client)
  894. {
  895. int err = 0;
  896. HTS_FUN();
  897. err = hwmsen_detach(ID_PRESSURE);
  898. if (err)
  899. HTS_ERR("hwmsen_detach ID_PRESSURE failed, err = %d\n", err);
  900. err = hts221_delete_attr(&(hts221_init_info.platform_diver_addr->driver));
  901. if (err)
  902. HTS_ERR("hts221_delete_attr failed, err = %d\n", err);
  903. err = misc_deregister(&hts221_device);
  904. if (err)
  905. HTS_ERR("misc_deregister failed, err = %d\n", err);
  906. obj_i2c_data = NULL;
  907. i2c_unregister_device(client);
  908. kfree(i2c_get_clientdata(client));
  909. return 0;
  910. }
  911. #ifdef CONFIG_OF
  912. static const struct of_device_id hmdy_of_match[] = {
  913. {.compatible = "mediatek,humidity"},
  914. {},
  915. };
  916. #endif
  917. static struct i2c_driver hts221_i2c_driver = {
  918. .driver = {
  919. .name = HTS_DEV_NAME,
  920. #ifdef CONFIG_OF
  921. .of_match_table = hmdy_of_match,
  922. #endif
  923. },
  924. .probe = hts221_i2c_probe,
  925. .remove = hts221_i2c_remove,
  926. .detect = hts221_i2c_detect,
  927. .suspend = hts221_suspend,
  928. .resume = hts221_resume,
  929. .id_table = hts221_i2c_id,
  930. };
  931. static int hts221_remove(void)
  932. {
  933. HTS_FUN();
  934. i2c_del_driver(&hts221_i2c_driver);
  935. return 0;
  936. }
  937. static int hts221_local_init(void)
  938. {
  939. HTS_FUN();
  940. if (i2c_add_driver(&hts221_i2c_driver)) {
  941. HTS_ERR("add driver error\n");
  942. return -1;
  943. }
  944. if (-1 == hts221_init_flag)
  945. return -1;
  946. return 0;
  947. }
  948. static int __init hts221_init(void)
  949. {
  950. const char *name = "mediatek,hts221";
  951. hw = get_hmdy_dts_func(name, hw);
  952. if (!hw)
  953. HMDY_ERR("get dts info fail\n");
  954. hmdy_driver_add(&hts221_init_info);
  955. return 0;
  956. }
  957. static void __exit hts221_exit(void)
  958. {
  959. HTS_FUN();
  960. }
  961. module_init(hts221_init);
  962. module_exit(hts221_exit);
  963. MODULE_LICENSE("GPLv2");
  964. MODULE_DESCRIPTION("HTS221 I2C Driver");
  965. MODULE_AUTHOR("hongxu.zhao@mediatek.com");
  966. MODULE_VERSION(HTS_DRIVER_VERSION);