lm95241.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
  3. *
  4. * The LM95241 is a sensor chip made by National Semiconductors.
  5. * It reports up to three temperatures (its own plus up to two external ones).
  6. * Complete datasheet can be obtained from National's website at:
  7. * http://www.national.com/ds.cgi/LM/LM95241.pdf
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/bitops.h>
  20. #include <linux/err.h>
  21. #include <linux/i2c.h>
  22. #include <linux/init.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/module.h>
  26. #include <linux/mutex.h>
  27. #include <linux/slab.h>
  28. #define DEVNAME "lm95241"
  29. static const unsigned short normal_i2c[] = {
  30. 0x19, 0x2a, 0x2b, I2C_CLIENT_END };
  31. /* LM95241 registers */
  32. #define LM95241_REG_R_MAN_ID 0xFE
  33. #define LM95241_REG_R_CHIP_ID 0xFF
  34. #define LM95241_REG_R_STATUS 0x02
  35. #define LM95241_REG_RW_CONFIG 0x03
  36. #define LM95241_REG_RW_REM_FILTER 0x06
  37. #define LM95241_REG_RW_TRUTHERM 0x07
  38. #define LM95241_REG_W_ONE_SHOT 0x0F
  39. #define LM95241_REG_R_LOCAL_TEMPH 0x10
  40. #define LM95241_REG_R_REMOTE1_TEMPH 0x11
  41. #define LM95241_REG_R_REMOTE2_TEMPH 0x12
  42. #define LM95241_REG_R_LOCAL_TEMPL 0x20
  43. #define LM95241_REG_R_REMOTE1_TEMPL 0x21
  44. #define LM95241_REG_R_REMOTE2_TEMPL 0x22
  45. #define LM95241_REG_RW_REMOTE_MODEL 0x30
  46. /* LM95241 specific bitfields */
  47. #define CFG_STOP BIT(6)
  48. #define CFG_CR0076 0x00
  49. #define CFG_CR0182 BIT(4)
  50. #define CFG_CR1000 BIT(5)
  51. #define CFG_CR2700 (BIT(4) | BIT(5))
  52. #define CFG_CRMASK (BIT(4) | BIT(5))
  53. #define R1MS_MASK BIT(0)
  54. #define R2MS_MASK BIT(2)
  55. #define R1DF_MASK BIT(1)
  56. #define R2DF_MASK BIT(2)
  57. #define R1FE_MASK BIT(0)
  58. #define R2FE_MASK BIT(2)
  59. #define R1DM BIT(0)
  60. #define R2DM BIT(1)
  61. #define TT1_SHIFT 0
  62. #define TT2_SHIFT 4
  63. #define TT_OFF 0
  64. #define TT_ON 1
  65. #define TT_MASK 7
  66. #define NATSEMI_MAN_ID 0x01
  67. #define LM95231_CHIP_ID 0xA1
  68. #define LM95241_CHIP_ID 0xA4
  69. static const u8 lm95241_reg_address[] = {
  70. LM95241_REG_R_LOCAL_TEMPH,
  71. LM95241_REG_R_LOCAL_TEMPL,
  72. LM95241_REG_R_REMOTE1_TEMPH,
  73. LM95241_REG_R_REMOTE1_TEMPL,
  74. LM95241_REG_R_REMOTE2_TEMPH,
  75. LM95241_REG_R_REMOTE2_TEMPL
  76. };
  77. /* Client data (each client gets its own) */
  78. struct lm95241_data {
  79. struct i2c_client *client;
  80. struct mutex update_lock;
  81. unsigned long last_updated; /* in jiffies */
  82. unsigned long interval; /* in milli-seconds */
  83. char valid; /* zero until following fields are valid */
  84. /* registers values */
  85. u8 temp[ARRAY_SIZE(lm95241_reg_address)];
  86. u8 status, config, model, trutherm;
  87. };
  88. /* Conversions */
  89. static int temp_from_reg_signed(u8 val_h, u8 val_l)
  90. {
  91. s16 val_hl = (val_h << 8) | val_l;
  92. return val_hl * 1000 / 256;
  93. }
  94. static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
  95. {
  96. u16 val_hl = (val_h << 8) | val_l;
  97. return val_hl * 1000 / 256;
  98. }
  99. static struct lm95241_data *lm95241_update_device(struct device *dev)
  100. {
  101. struct lm95241_data *data = dev_get_drvdata(dev);
  102. struct i2c_client *client = data->client;
  103. mutex_lock(&data->update_lock);
  104. if (time_after(jiffies, data->last_updated
  105. + msecs_to_jiffies(data->interval)) ||
  106. !data->valid) {
  107. int i;
  108. dev_dbg(dev, "Updating lm95241 data.\n");
  109. for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
  110. data->temp[i]
  111. = i2c_smbus_read_byte_data(client,
  112. lm95241_reg_address[i]);
  113. data->status = i2c_smbus_read_byte_data(client,
  114. LM95241_REG_R_STATUS);
  115. data->last_updated = jiffies;
  116. data->valid = 1;
  117. }
  118. mutex_unlock(&data->update_lock);
  119. return data;
  120. }
  121. static int lm95241_read_chip(struct device *dev, u32 attr, int channel,
  122. long *val)
  123. {
  124. struct lm95241_data *data = dev_get_drvdata(dev);
  125. switch (attr) {
  126. case hwmon_chip_update_interval:
  127. *val = data->interval;
  128. return 0;
  129. default:
  130. return -EOPNOTSUPP;
  131. }
  132. }
  133. static int lm95241_read_temp(struct device *dev, u32 attr, int channel,
  134. long *val)
  135. {
  136. struct lm95241_data *data = lm95241_update_device(dev);
  137. switch (attr) {
  138. case hwmon_temp_input:
  139. if (!channel || (data->config & BIT(channel - 1)))
  140. *val = temp_from_reg_signed(data->temp[channel * 2],
  141. data->temp[channel * 2 + 1]);
  142. else
  143. *val = temp_from_reg_unsigned(data->temp[channel * 2],
  144. data->temp[channel * 2 + 1]);
  145. return 0;
  146. case hwmon_temp_min:
  147. if (channel == 1)
  148. *val = (data->config & R1DF_MASK) ? -128000 : 0;
  149. else
  150. *val = (data->config & R2DF_MASK) ? -128000 : 0;
  151. return 0;
  152. case hwmon_temp_max:
  153. if (channel == 1)
  154. *val = (data->config & R1DF_MASK) ? 127875 : 255875;
  155. else
  156. *val = (data->config & R2DF_MASK) ? 127875 : 255875;
  157. return 0;
  158. case hwmon_temp_type:
  159. if (channel == 1)
  160. *val = (data->model & R1MS_MASK) ? 1 : 2;
  161. else
  162. *val = (data->model & R2MS_MASK) ? 1 : 2;
  163. return 0;
  164. case hwmon_temp_fault:
  165. if (channel == 1)
  166. *val = !!(data->status & R1DM);
  167. else
  168. *val = !!(data->status & R2DM);
  169. return 0;
  170. default:
  171. return -EOPNOTSUPP;
  172. }
  173. }
  174. static int lm95241_read(struct device *dev, enum hwmon_sensor_types type,
  175. u32 attr, int channel, long *val)
  176. {
  177. switch (type) {
  178. case hwmon_chip:
  179. return lm95241_read_chip(dev, attr, channel, val);
  180. case hwmon_temp:
  181. return lm95241_read_temp(dev, attr, channel, val);
  182. default:
  183. return -EOPNOTSUPP;
  184. }
  185. }
  186. static int lm95241_write_chip(struct device *dev, u32 attr, int channel,
  187. long val)
  188. {
  189. struct lm95241_data *data = dev_get_drvdata(dev);
  190. int convrate;
  191. u8 config;
  192. int ret;
  193. mutex_lock(&data->update_lock);
  194. switch (attr) {
  195. case hwmon_chip_update_interval:
  196. config = data->config & ~CFG_CRMASK;
  197. if (val < 130) {
  198. convrate = 76;
  199. config |= CFG_CR0076;
  200. } else if (val < 590) {
  201. convrate = 182;
  202. config |= CFG_CR0182;
  203. } else if (val < 1850) {
  204. convrate = 1000;
  205. config |= CFG_CR1000;
  206. } else {
  207. convrate = 2700;
  208. config |= CFG_CR2700;
  209. }
  210. data->interval = convrate;
  211. data->config = config;
  212. ret = i2c_smbus_write_byte_data(data->client,
  213. LM95241_REG_RW_CONFIG, config);
  214. break;
  215. default:
  216. ret = -EOPNOTSUPP;
  217. break;
  218. }
  219. mutex_unlock(&data->update_lock);
  220. return ret;
  221. }
  222. static int lm95241_write_temp(struct device *dev, u32 attr, int channel,
  223. long val)
  224. {
  225. struct lm95241_data *data = dev_get_drvdata(dev);
  226. struct i2c_client *client = data->client;
  227. int ret;
  228. mutex_lock(&data->update_lock);
  229. switch (attr) {
  230. case hwmon_temp_min:
  231. if (channel == 1) {
  232. if (val < 0)
  233. data->config |= R1DF_MASK;
  234. else
  235. data->config &= ~R1DF_MASK;
  236. } else {
  237. if (val < 0)
  238. data->config |= R2DF_MASK;
  239. else
  240. data->config &= ~R2DF_MASK;
  241. }
  242. data->valid = 0;
  243. ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
  244. data->config);
  245. break;
  246. case hwmon_temp_max:
  247. if (channel == 1) {
  248. if (val <= 127875)
  249. data->config |= R1DF_MASK;
  250. else
  251. data->config &= ~R1DF_MASK;
  252. } else {
  253. if (val <= 127875)
  254. data->config |= R2DF_MASK;
  255. else
  256. data->config &= ~R2DF_MASK;
  257. }
  258. data->valid = 0;
  259. ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
  260. data->config);
  261. break;
  262. case hwmon_temp_type:
  263. if (val != 1 && val != 2) {
  264. ret = -EINVAL;
  265. break;
  266. }
  267. if (channel == 1) {
  268. data->trutherm &= ~(TT_MASK << TT1_SHIFT);
  269. if (val == 1) {
  270. data->model |= R1MS_MASK;
  271. data->trutherm |= (TT_ON << TT1_SHIFT);
  272. } else {
  273. data->model &= ~R1MS_MASK;
  274. data->trutherm |= (TT_OFF << TT1_SHIFT);
  275. }
  276. } else {
  277. data->trutherm &= ~(TT_MASK << TT2_SHIFT);
  278. if (val == 1) {
  279. data->model |= R2MS_MASK;
  280. data->trutherm |= (TT_ON << TT2_SHIFT);
  281. } else {
  282. data->model &= ~R2MS_MASK;
  283. data->trutherm |= (TT_OFF << TT2_SHIFT);
  284. }
  285. }
  286. ret = i2c_smbus_write_byte_data(client,
  287. LM95241_REG_RW_REMOTE_MODEL,
  288. data->model);
  289. if (ret < 0)
  290. break;
  291. ret = i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
  292. data->trutherm);
  293. break;
  294. default:
  295. ret = -EOPNOTSUPP;
  296. break;
  297. }
  298. mutex_unlock(&data->update_lock);
  299. return ret;
  300. }
  301. static int lm95241_write(struct device *dev, enum hwmon_sensor_types type,
  302. u32 attr, int channel, long val)
  303. {
  304. switch (type) {
  305. case hwmon_chip:
  306. return lm95241_write_chip(dev, attr, channel, val);
  307. case hwmon_temp:
  308. return lm95241_write_temp(dev, attr, channel, val);
  309. default:
  310. return -EOPNOTSUPP;
  311. }
  312. }
  313. static umode_t lm95241_is_visible(const void *data,
  314. enum hwmon_sensor_types type,
  315. u32 attr, int channel)
  316. {
  317. switch (type) {
  318. case hwmon_chip:
  319. switch (attr) {
  320. case hwmon_chip_update_interval:
  321. return S_IRUGO | S_IWUSR;
  322. }
  323. break;
  324. case hwmon_temp:
  325. switch (attr) {
  326. case hwmon_temp_input:
  327. return S_IRUGO;
  328. case hwmon_temp_fault:
  329. return S_IRUGO;
  330. case hwmon_temp_min:
  331. case hwmon_temp_max:
  332. case hwmon_temp_type:
  333. return S_IRUGO | S_IWUSR;
  334. }
  335. break;
  336. default:
  337. break;
  338. }
  339. return 0;
  340. }
  341. /* Return 0 if detection is successful, -ENODEV otherwise */
  342. static int lm95241_detect(struct i2c_client *new_client,
  343. struct i2c_board_info *info)
  344. {
  345. struct i2c_adapter *adapter = new_client->adapter;
  346. const char *name;
  347. int mfg_id, chip_id;
  348. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  349. return -ENODEV;
  350. mfg_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID);
  351. if (mfg_id != NATSEMI_MAN_ID)
  352. return -ENODEV;
  353. chip_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID);
  354. switch (chip_id) {
  355. case LM95231_CHIP_ID:
  356. name = "lm95231";
  357. break;
  358. case LM95241_CHIP_ID:
  359. name = "lm95241";
  360. break;
  361. default:
  362. return -ENODEV;
  363. }
  364. /* Fill the i2c board info */
  365. strlcpy(info->type, name, I2C_NAME_SIZE);
  366. return 0;
  367. }
  368. static void lm95241_init_client(struct i2c_client *client,
  369. struct lm95241_data *data)
  370. {
  371. data->interval = 1000;
  372. data->config = CFG_CR1000;
  373. data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
  374. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
  375. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
  376. R1FE_MASK | R2FE_MASK);
  377. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
  378. data->trutherm);
  379. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
  380. data->model);
  381. }
  382. static const u32 lm95241_chip_config[] = {
  383. HWMON_C_UPDATE_INTERVAL,
  384. 0
  385. };
  386. static const struct hwmon_channel_info lm95241_chip = {
  387. .type = hwmon_chip,
  388. .config = lm95241_chip_config,
  389. };
  390. static const u32 lm95241_temp_config[] = {
  391. HWMON_T_INPUT,
  392. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | HWMON_T_TYPE |
  393. HWMON_T_FAULT,
  394. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | HWMON_T_TYPE |
  395. HWMON_T_FAULT,
  396. 0
  397. };
  398. static const struct hwmon_channel_info lm95241_temp = {
  399. .type = hwmon_temp,
  400. .config = lm95241_temp_config,
  401. };
  402. static const struct hwmon_channel_info *lm95241_info[] = {
  403. &lm95241_chip,
  404. &lm95241_temp,
  405. NULL
  406. };
  407. static const struct hwmon_ops lm95241_hwmon_ops = {
  408. .is_visible = lm95241_is_visible,
  409. .read = lm95241_read,
  410. .write = lm95241_write,
  411. };
  412. static const struct hwmon_chip_info lm95241_chip_info = {
  413. .ops = &lm95241_hwmon_ops,
  414. .info = lm95241_info,
  415. };
  416. static int lm95241_probe(struct i2c_client *client,
  417. const struct i2c_device_id *id)
  418. {
  419. struct device *dev = &client->dev;
  420. struct lm95241_data *data;
  421. struct device *hwmon_dev;
  422. data = devm_kzalloc(dev, sizeof(struct lm95241_data), GFP_KERNEL);
  423. if (!data)
  424. return -ENOMEM;
  425. data->client = client;
  426. mutex_init(&data->update_lock);
  427. /* Initialize the LM95241 chip */
  428. lm95241_init_client(client, data);
  429. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  430. data,
  431. &lm95241_chip_info,
  432. NULL);
  433. return PTR_ERR_OR_ZERO(hwmon_dev);
  434. }
  435. /* Driver data (common to all clients) */
  436. static const struct i2c_device_id lm95241_id[] = {
  437. { "lm95231", 0 },
  438. { "lm95241", 0 },
  439. { }
  440. };
  441. MODULE_DEVICE_TABLE(i2c, lm95241_id);
  442. static struct i2c_driver lm95241_driver = {
  443. .class = I2C_CLASS_HWMON,
  444. .driver = {
  445. .name = DEVNAME,
  446. },
  447. .probe = lm95241_probe,
  448. .id_table = lm95241_id,
  449. .detect = lm95241_detect,
  450. .address_list = normal_i2c,
  451. };
  452. module_i2c_driver(lm95241_driver);
  453. MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
  454. MODULE_DESCRIPTION("LM95231/LM95241 sensor driver");
  455. MODULE_LICENSE("GPL");