tmp421.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* tmp421.c
  3. *
  4. * Copyright (C) 2009 Andre Prendel <[email protected]>
  5. * Preliminary support by:
  6. * Melvin Rook, Raymond Ng
  7. */
  8. /*
  9. * Driver for the Texas Instruments TMP421 SMBus temperature sensor IC.
  10. * Supported models: TMP421, TMP422, TMP423, TMP441, TMP442
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/i2c.h>
  17. #include <linux/hwmon.h>
  18. #include <linux/hwmon-sysfs.h>
  19. #include <linux/err.h>
  20. #include <linux/mutex.h>
  21. #include <linux/of_device.h>
  22. #include <linux/sysfs.h>
  23. /* Addresses to scan */
  24. static const unsigned short normal_i2c[] = { 0x2a, 0x4c, 0x4d, 0x4e, 0x4f,
  25. I2C_CLIENT_END };
  26. enum chips { tmp421, tmp422, tmp423, tmp441, tmp442 };
  27. #define MAX_CHANNELS 4
  28. /* The TMP421 registers */
  29. #define TMP421_STATUS_REG 0x08
  30. #define TMP421_CONFIG_REG_1 0x09
  31. #define TMP421_CONFIG_REG_2 0x0A
  32. #define TMP421_CONFIG_REG_REN(x) (BIT(3 + (x)))
  33. #define TMP421_CONFIG_REG_REN_MASK GENMASK(6, 3)
  34. #define TMP421_CONVERSION_RATE_REG 0x0B
  35. #define TMP421_N_FACTOR_REG_1 0x21
  36. #define TMP421_MANUFACTURER_ID_REG 0xFE
  37. #define TMP421_DEVICE_ID_REG 0xFF
  38. static const u8 TMP421_TEMP_MSB[MAX_CHANNELS] = { 0x00, 0x01, 0x02, 0x03 };
  39. static const u8 TMP421_TEMP_LSB[MAX_CHANNELS] = { 0x10, 0x11, 0x12, 0x13 };
  40. /* Flags */
  41. #define TMP421_CONFIG_SHUTDOWN 0x40
  42. #define TMP421_CONFIG_RANGE 0x04
  43. /* Manufacturer / Device ID's */
  44. #define TMP421_MANUFACTURER_ID 0x55
  45. #define TMP421_DEVICE_ID 0x21
  46. #define TMP422_DEVICE_ID 0x22
  47. #define TMP423_DEVICE_ID 0x23
  48. #define TMP441_DEVICE_ID 0x41
  49. #define TMP442_DEVICE_ID 0x42
  50. static const struct i2c_device_id tmp421_id[] = {
  51. { "tmp421", 2 },
  52. { "tmp422", 3 },
  53. { "tmp423", 4 },
  54. { "tmp441", 2 },
  55. { "tmp442", 3 },
  56. { }
  57. };
  58. MODULE_DEVICE_TABLE(i2c, tmp421_id);
  59. static const struct of_device_id __maybe_unused tmp421_of_match[] = {
  60. {
  61. .compatible = "ti,tmp421",
  62. .data = (void *)2
  63. },
  64. {
  65. .compatible = "ti,tmp422",
  66. .data = (void *)3
  67. },
  68. {
  69. .compatible = "ti,tmp423",
  70. .data = (void *)4
  71. },
  72. {
  73. .compatible = "ti,tmp441",
  74. .data = (void *)2
  75. },
  76. {
  77. .compatible = "ti,tmp442",
  78. .data = (void *)3
  79. },
  80. { },
  81. };
  82. MODULE_DEVICE_TABLE(of, tmp421_of_match);
  83. struct tmp421_channel {
  84. const char *label;
  85. bool enabled;
  86. s16 temp;
  87. };
  88. struct tmp421_data {
  89. struct i2c_client *client;
  90. struct mutex update_lock;
  91. u32 temp_config[MAX_CHANNELS + 1];
  92. struct hwmon_channel_info temp_info;
  93. const struct hwmon_channel_info *info[2];
  94. struct hwmon_chip_info chip;
  95. bool valid;
  96. unsigned long last_updated;
  97. unsigned long channels;
  98. u8 config;
  99. struct tmp421_channel channel[MAX_CHANNELS];
  100. };
  101. static int temp_from_raw(u16 reg, bool extended)
  102. {
  103. /* Mask out status bits */
  104. int temp = reg & ~0xf;
  105. if (extended)
  106. temp = temp - 64 * 256;
  107. else
  108. temp = (s16)temp;
  109. return DIV_ROUND_CLOSEST(temp * 1000, 256);
  110. }
  111. static int tmp421_update_device(struct tmp421_data *data)
  112. {
  113. struct i2c_client *client = data->client;
  114. int ret = 0;
  115. int i;
  116. mutex_lock(&data->update_lock);
  117. if (time_after(jiffies, data->last_updated + (HZ / 2)) ||
  118. !data->valid) {
  119. ret = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
  120. if (ret < 0)
  121. goto exit;
  122. data->config = ret;
  123. for (i = 0; i < data->channels; i++) {
  124. ret = i2c_smbus_read_byte_data(client, TMP421_TEMP_MSB[i]);
  125. if (ret < 0)
  126. goto exit;
  127. data->channel[i].temp = ret << 8;
  128. ret = i2c_smbus_read_byte_data(client, TMP421_TEMP_LSB[i]);
  129. if (ret < 0)
  130. goto exit;
  131. data->channel[i].temp |= ret;
  132. }
  133. data->last_updated = jiffies;
  134. data->valid = true;
  135. }
  136. exit:
  137. mutex_unlock(&data->update_lock);
  138. if (ret < 0) {
  139. data->valid = false;
  140. return ret;
  141. }
  142. return 0;
  143. }
  144. static int tmp421_enable_channels(struct tmp421_data *data)
  145. {
  146. int err;
  147. struct i2c_client *client = data->client;
  148. struct device *dev = &client->dev;
  149. int old = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_2);
  150. int new, i;
  151. if (old < 0) {
  152. dev_err(dev, "error reading register, can't disable channels\n");
  153. return old;
  154. }
  155. new = old & ~TMP421_CONFIG_REG_REN_MASK;
  156. for (i = 0; i < data->channels; i++)
  157. if (data->channel[i].enabled)
  158. new |= TMP421_CONFIG_REG_REN(i);
  159. if (new == old)
  160. return 0;
  161. err = i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_2, new);
  162. if (err < 0)
  163. dev_err(dev, "error writing register, can't disable channels\n");
  164. return err;
  165. }
  166. static int tmp421_read(struct device *dev, enum hwmon_sensor_types type,
  167. u32 attr, int channel, long *val)
  168. {
  169. struct tmp421_data *tmp421 = dev_get_drvdata(dev);
  170. int ret = 0;
  171. ret = tmp421_update_device(tmp421);
  172. if (ret)
  173. return ret;
  174. switch (attr) {
  175. case hwmon_temp_input:
  176. if (!tmp421->channel[channel].enabled)
  177. return -ENODATA;
  178. *val = temp_from_raw(tmp421->channel[channel].temp,
  179. tmp421->config & TMP421_CONFIG_RANGE);
  180. return 0;
  181. case hwmon_temp_fault:
  182. if (!tmp421->channel[channel].enabled)
  183. return -ENODATA;
  184. /*
  185. * Any of OPEN or /PVLD bits indicate a hardware mulfunction
  186. * and the conversion result may be incorrect
  187. */
  188. *val = !!(tmp421->channel[channel].temp & 0x03);
  189. return 0;
  190. case hwmon_temp_enable:
  191. *val = tmp421->channel[channel].enabled;
  192. return 0;
  193. default:
  194. return -EOPNOTSUPP;
  195. }
  196. }
  197. static int tmp421_read_string(struct device *dev, enum hwmon_sensor_types type,
  198. u32 attr, int channel, const char **str)
  199. {
  200. struct tmp421_data *data = dev_get_drvdata(dev);
  201. *str = data->channel[channel].label;
  202. return 0;
  203. }
  204. static int tmp421_write(struct device *dev, enum hwmon_sensor_types type,
  205. u32 attr, int channel, long val)
  206. {
  207. struct tmp421_data *data = dev_get_drvdata(dev);
  208. int ret;
  209. switch (attr) {
  210. case hwmon_temp_enable:
  211. data->channel[channel].enabled = val;
  212. ret = tmp421_enable_channels(data);
  213. break;
  214. default:
  215. ret = -EOPNOTSUPP;
  216. }
  217. return ret;
  218. }
  219. static umode_t tmp421_is_visible(const void *data, enum hwmon_sensor_types type,
  220. u32 attr, int channel)
  221. {
  222. switch (attr) {
  223. case hwmon_temp_fault:
  224. case hwmon_temp_input:
  225. return 0444;
  226. case hwmon_temp_label:
  227. return 0444;
  228. case hwmon_temp_enable:
  229. return 0644;
  230. default:
  231. return 0;
  232. }
  233. }
  234. static int tmp421_init_client(struct tmp421_data *data)
  235. {
  236. int config, config_orig;
  237. struct i2c_client *client = data->client;
  238. /* Set the conversion rate to 2 Hz */
  239. i2c_smbus_write_byte_data(client, TMP421_CONVERSION_RATE_REG, 0x05);
  240. /* Start conversions (disable shutdown if necessary) */
  241. config = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
  242. if (config < 0) {
  243. dev_err(&client->dev,
  244. "Could not read configuration register (%d)\n", config);
  245. return config;
  246. }
  247. config_orig = config;
  248. config &= ~TMP421_CONFIG_SHUTDOWN;
  249. if (config != config_orig) {
  250. dev_info(&client->dev, "Enable monitoring chip\n");
  251. i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config);
  252. }
  253. return tmp421_enable_channels(data);
  254. }
  255. static int tmp421_detect(struct i2c_client *client,
  256. struct i2c_board_info *info)
  257. {
  258. enum chips kind;
  259. struct i2c_adapter *adapter = client->adapter;
  260. static const char * const names[] = {
  261. "TMP421", "TMP422", "TMP423",
  262. "TMP441", "TMP442"
  263. };
  264. int addr = client->addr;
  265. u8 reg;
  266. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  267. return -ENODEV;
  268. reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG);
  269. if (reg != TMP421_MANUFACTURER_ID)
  270. return -ENODEV;
  271. reg = i2c_smbus_read_byte_data(client, TMP421_CONVERSION_RATE_REG);
  272. if (reg & 0xf8)
  273. return -ENODEV;
  274. reg = i2c_smbus_read_byte_data(client, TMP421_STATUS_REG);
  275. if (reg & 0x7f)
  276. return -ENODEV;
  277. reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG);
  278. switch (reg) {
  279. case TMP421_DEVICE_ID:
  280. kind = tmp421;
  281. break;
  282. case TMP422_DEVICE_ID:
  283. if (addr == 0x2a)
  284. return -ENODEV;
  285. kind = tmp422;
  286. break;
  287. case TMP423_DEVICE_ID:
  288. if (addr != 0x4c && addr != 0x4d)
  289. return -ENODEV;
  290. kind = tmp423;
  291. break;
  292. case TMP441_DEVICE_ID:
  293. kind = tmp441;
  294. break;
  295. case TMP442_DEVICE_ID:
  296. if (addr != 0x4c && addr != 0x4d)
  297. return -ENODEV;
  298. kind = tmp442;
  299. break;
  300. default:
  301. return -ENODEV;
  302. }
  303. strscpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE);
  304. dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n",
  305. names[kind], client->addr);
  306. return 0;
  307. }
  308. static int tmp421_probe_child_from_dt(struct i2c_client *client,
  309. struct device_node *child,
  310. struct tmp421_data *data)
  311. {
  312. struct device *dev = &client->dev;
  313. u32 i;
  314. s32 val;
  315. int err;
  316. err = of_property_read_u32(child, "reg", &i);
  317. if (err) {
  318. dev_err(dev, "missing reg property of %pOFn\n", child);
  319. return err;
  320. }
  321. if (i >= data->channels) {
  322. dev_err(dev, "invalid reg %d of %pOFn\n", i, child);
  323. return -EINVAL;
  324. }
  325. of_property_read_string(child, "label", &data->channel[i].label);
  326. if (data->channel[i].label)
  327. data->temp_config[i] |= HWMON_T_LABEL;
  328. data->channel[i].enabled = of_device_is_available(child);
  329. err = of_property_read_s32(child, "ti,n-factor", &val);
  330. if (!err) {
  331. if (i == 0) {
  332. dev_err(dev, "n-factor can't be set for internal channel\n");
  333. return -EINVAL;
  334. }
  335. if (val > 127 || val < -128) {
  336. dev_err(dev, "n-factor for channel %d invalid (%d)\n",
  337. i, val);
  338. return -EINVAL;
  339. }
  340. i2c_smbus_write_byte_data(client, TMP421_N_FACTOR_REG_1 + i - 1,
  341. val);
  342. }
  343. return 0;
  344. }
  345. static int tmp421_probe_from_dt(struct i2c_client *client, struct tmp421_data *data)
  346. {
  347. struct device *dev = &client->dev;
  348. const struct device_node *np = dev->of_node;
  349. struct device_node *child;
  350. int err;
  351. for_each_child_of_node(np, child) {
  352. if (strcmp(child->name, "channel"))
  353. continue;
  354. err = tmp421_probe_child_from_dt(client, child, data);
  355. if (err) {
  356. of_node_put(child);
  357. return err;
  358. }
  359. }
  360. return 0;
  361. }
  362. static const struct hwmon_ops tmp421_ops = {
  363. .is_visible = tmp421_is_visible,
  364. .read = tmp421_read,
  365. .read_string = tmp421_read_string,
  366. .write = tmp421_write,
  367. };
  368. static int tmp421_probe(struct i2c_client *client)
  369. {
  370. struct device *dev = &client->dev;
  371. struct device *hwmon_dev;
  372. struct tmp421_data *data;
  373. int i, err;
  374. data = devm_kzalloc(dev, sizeof(struct tmp421_data), GFP_KERNEL);
  375. if (!data)
  376. return -ENOMEM;
  377. mutex_init(&data->update_lock);
  378. if (client->dev.of_node)
  379. data->channels = (unsigned long)
  380. of_device_get_match_data(&client->dev);
  381. else
  382. data->channels = i2c_match_id(tmp421_id, client)->driver_data;
  383. data->client = client;
  384. for (i = 0; i < data->channels; i++) {
  385. data->temp_config[i] = HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_ENABLE;
  386. data->channel[i].enabled = true;
  387. }
  388. err = tmp421_probe_from_dt(client, data);
  389. if (err)
  390. return err;
  391. err = tmp421_init_client(data);
  392. if (err)
  393. return err;
  394. data->chip.ops = &tmp421_ops;
  395. data->chip.info = data->info;
  396. data->info[0] = &data->temp_info;
  397. data->temp_info.type = hwmon_temp;
  398. data->temp_info.config = data->temp_config;
  399. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  400. data,
  401. &data->chip,
  402. NULL);
  403. return PTR_ERR_OR_ZERO(hwmon_dev);
  404. }
  405. static struct i2c_driver tmp421_driver = {
  406. .class = I2C_CLASS_HWMON,
  407. .driver = {
  408. .name = "tmp421",
  409. .of_match_table = of_match_ptr(tmp421_of_match),
  410. },
  411. .probe_new = tmp421_probe,
  412. .id_table = tmp421_id,
  413. .detect = tmp421_detect,
  414. .address_list = normal_i2c,
  415. };
  416. module_i2c_driver(tmp421_driver);
  417. MODULE_AUTHOR("Andre Prendel <[email protected]>");
  418. MODULE_DESCRIPTION("Texas Instruments TMP421/422/423/441/442 temperature sensor driver");
  419. MODULE_LICENSE("GPL");