asc7621.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * asc7621.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
  4. * Copyright (c) 2007, 2010 George Joseph <[email protected]>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/i2c.h>
  11. #include <linux/hwmon.h>
  12. #include <linux/hwmon-sysfs.h>
  13. #include <linux/err.h>
  14. #include <linux/mutex.h>
  15. /* Addresses to scan */
  16. static const unsigned short normal_i2c[] = {
  17. 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
  18. };
  19. enum asc7621_type {
  20. asc7621,
  21. asc7621a
  22. };
  23. #define INTERVAL_HIGH (HZ + HZ / 2)
  24. #define INTERVAL_LOW (1 * 60 * HZ)
  25. #define PRI_NONE 0
  26. #define PRI_LOW 1
  27. #define PRI_HIGH 2
  28. #define FIRST_CHIP asc7621
  29. #define LAST_CHIP asc7621a
  30. struct asc7621_chip {
  31. char *name;
  32. enum asc7621_type chip_type;
  33. u8 company_reg;
  34. u8 company_id;
  35. u8 verstep_reg;
  36. u8 verstep_id;
  37. const unsigned short *addresses;
  38. };
  39. static struct asc7621_chip asc7621_chips[] = {
  40. {
  41. .name = "asc7621",
  42. .chip_type = asc7621,
  43. .company_reg = 0x3e,
  44. .company_id = 0x61,
  45. .verstep_reg = 0x3f,
  46. .verstep_id = 0x6c,
  47. .addresses = normal_i2c,
  48. },
  49. {
  50. .name = "asc7621a",
  51. .chip_type = asc7621a,
  52. .company_reg = 0x3e,
  53. .company_id = 0x61,
  54. .verstep_reg = 0x3f,
  55. .verstep_id = 0x6d,
  56. .addresses = normal_i2c,
  57. },
  58. };
  59. /*
  60. * Defines the highest register to be used, not the count.
  61. * The actual count will probably be smaller because of gaps
  62. * in the implementation (unused register locations).
  63. * This define will safely set the array size of both the parameter
  64. * and data arrays.
  65. * This comes from the data sheet register description table.
  66. */
  67. #define LAST_REGISTER 0xff
  68. struct asc7621_data {
  69. struct i2c_client client;
  70. struct device *class_dev;
  71. struct mutex update_lock;
  72. bool valid; /* true if following fields are valid */
  73. unsigned long last_high_reading; /* In jiffies */
  74. unsigned long last_low_reading; /* In jiffies */
  75. /*
  76. * Registers we care about occupy the corresponding index
  77. * in the array. Registers we don't care about are left
  78. * at 0.
  79. */
  80. u8 reg[LAST_REGISTER + 1];
  81. };
  82. /*
  83. * Macro to get the parent asc7621_param structure
  84. * from a sensor_device_attribute passed into the
  85. * show/store functions.
  86. */
  87. #define to_asc7621_param(_sda) \
  88. container_of(_sda, struct asc7621_param, sda)
  89. /*
  90. * Each parameter to be retrieved needs an asc7621_param structure
  91. * allocated. It contains the sensor_device_attribute structure
  92. * and the control info needed to retrieve the value from the register map.
  93. */
  94. struct asc7621_param {
  95. struct sensor_device_attribute sda;
  96. u8 priority;
  97. u8 msb[3];
  98. u8 lsb[3];
  99. u8 mask[3];
  100. u8 shift[3];
  101. };
  102. /*
  103. * This is the map that ultimately indicates whether we'll be
  104. * retrieving a register value or not, and at what frequency.
  105. */
  106. static u8 asc7621_register_priorities[255];
  107. static struct asc7621_data *asc7621_update_device(struct device *dev);
  108. static inline u8 read_byte(struct i2c_client *client, u8 reg)
  109. {
  110. int res = i2c_smbus_read_byte_data(client, reg);
  111. if (res < 0) {
  112. dev_err(&client->dev,
  113. "Unable to read from register 0x%02x.\n", reg);
  114. return 0;
  115. }
  116. return res & 0xff;
  117. }
  118. static inline int write_byte(struct i2c_client *client, u8 reg, u8 data)
  119. {
  120. int res = i2c_smbus_write_byte_data(client, reg, data);
  121. if (res < 0) {
  122. dev_err(&client->dev,
  123. "Unable to write value 0x%02x to register 0x%02x.\n",
  124. data, reg);
  125. }
  126. return res;
  127. }
  128. /*
  129. * Data Handlers
  130. * Each function handles the formatting, storage
  131. * and retrieval of like parameters.
  132. */
  133. #define SETUP_SHOW_DATA_PARAM(d, a) \
  134. struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
  135. struct asc7621_data *data = asc7621_update_device(d); \
  136. struct asc7621_param *param = to_asc7621_param(sda)
  137. #define SETUP_STORE_DATA_PARAM(d, a) \
  138. struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
  139. struct i2c_client *client = to_i2c_client(d); \
  140. struct asc7621_data *data = i2c_get_clientdata(client); \
  141. struct asc7621_param *param = to_asc7621_param(sda)
  142. /*
  143. * u8 is just what it sounds like...an unsigned byte with no
  144. * special formatting.
  145. */
  146. static ssize_t show_u8(struct device *dev, struct device_attribute *attr,
  147. char *buf)
  148. {
  149. SETUP_SHOW_DATA_PARAM(dev, attr);
  150. return sprintf(buf, "%u\n", data->reg[param->msb[0]]);
  151. }
  152. static ssize_t store_u8(struct device *dev, struct device_attribute *attr,
  153. const char *buf, size_t count)
  154. {
  155. SETUP_STORE_DATA_PARAM(dev, attr);
  156. long reqval;
  157. if (kstrtol(buf, 10, &reqval))
  158. return -EINVAL;
  159. reqval = clamp_val(reqval, 0, 255);
  160. mutex_lock(&data->update_lock);
  161. data->reg[param->msb[0]] = reqval;
  162. write_byte(client, param->msb[0], reqval);
  163. mutex_unlock(&data->update_lock);
  164. return count;
  165. }
  166. /*
  167. * Many of the config values occupy only a few bits of a register.
  168. */
  169. static ssize_t show_bitmask(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. SETUP_SHOW_DATA_PARAM(dev, attr);
  173. return sprintf(buf, "%u\n",
  174. (data->reg[param->msb[0]] >> param->
  175. shift[0]) & param->mask[0]);
  176. }
  177. static ssize_t store_bitmask(struct device *dev,
  178. struct device_attribute *attr,
  179. const char *buf, size_t count)
  180. {
  181. SETUP_STORE_DATA_PARAM(dev, attr);
  182. long reqval;
  183. u8 currval;
  184. if (kstrtol(buf, 10, &reqval))
  185. return -EINVAL;
  186. reqval = clamp_val(reqval, 0, param->mask[0]);
  187. reqval = (reqval & param->mask[0]) << param->shift[0];
  188. mutex_lock(&data->update_lock);
  189. currval = read_byte(client, param->msb[0]);
  190. reqval |= (currval & ~(param->mask[0] << param->shift[0]));
  191. data->reg[param->msb[0]] = reqval;
  192. write_byte(client, param->msb[0], reqval);
  193. mutex_unlock(&data->update_lock);
  194. return count;
  195. }
  196. /*
  197. * 16 bit fan rpm values
  198. * reported by the device as the number of 11.111us periods (90khz)
  199. * between full fan rotations. Therefore...
  200. * RPM = (90000 * 60) / register value
  201. */
  202. static ssize_t show_fan16(struct device *dev,
  203. struct device_attribute *attr, char *buf)
  204. {
  205. SETUP_SHOW_DATA_PARAM(dev, attr);
  206. u16 regval;
  207. mutex_lock(&data->update_lock);
  208. regval = (data->reg[param->msb[0]] << 8) | data->reg[param->lsb[0]];
  209. mutex_unlock(&data->update_lock);
  210. return sprintf(buf, "%u\n",
  211. (regval == 0 ? -1 : (regval) ==
  212. 0xffff ? 0 : 5400000 / regval));
  213. }
  214. static ssize_t store_fan16(struct device *dev,
  215. struct device_attribute *attr, const char *buf,
  216. size_t count)
  217. {
  218. SETUP_STORE_DATA_PARAM(dev, attr);
  219. long reqval;
  220. if (kstrtol(buf, 10, &reqval))
  221. return -EINVAL;
  222. /*
  223. * If a minimum RPM of zero is requested, then we set the register to
  224. * 0xffff. This value allows the fan to be stopped completely without
  225. * generating an alarm.
  226. */
  227. reqval =
  228. (reqval <= 0 ? 0xffff : clamp_val(5400000 / reqval, 0, 0xfffe));
  229. mutex_lock(&data->update_lock);
  230. data->reg[param->msb[0]] = (reqval >> 8) & 0xff;
  231. data->reg[param->lsb[0]] = reqval & 0xff;
  232. write_byte(client, param->msb[0], data->reg[param->msb[0]]);
  233. write_byte(client, param->lsb[0], data->reg[param->lsb[0]]);
  234. mutex_unlock(&data->update_lock);
  235. return count;
  236. }
  237. /*
  238. * Voltages are scaled in the device so that the nominal voltage
  239. * is 3/4ths of the 0-255 range (i.e. 192).
  240. * If all voltages are 'normal' then all voltage registers will
  241. * read 0xC0.
  242. *
  243. * The data sheet provides us with the 3/4 scale value for each voltage
  244. * which is stored in in_scaling. The sda->index parameter value provides
  245. * the index into in_scaling.
  246. *
  247. * NOTE: The chip expects the first 2 inputs be 2.5 and 2.25 volts
  248. * respectively. That doesn't mean that's what the motherboard provides. :)
  249. */
  250. static const int asc7621_in_scaling[] = {
  251. 2500, 2250, 3300, 5000, 12000
  252. };
  253. static ssize_t show_in10(struct device *dev, struct device_attribute *attr,
  254. char *buf)
  255. {
  256. SETUP_SHOW_DATA_PARAM(dev, attr);
  257. u16 regval;
  258. u8 nr = sda->index;
  259. mutex_lock(&data->update_lock);
  260. regval = (data->reg[param->msb[0]] << 8) | (data->reg[param->lsb[0]]);
  261. mutex_unlock(&data->update_lock);
  262. /* The LSB value is a 2-bit scaling of the MSB's LSbit value. */
  263. regval = (regval >> 6) * asc7621_in_scaling[nr] / (0xc0 << 2);
  264. return sprintf(buf, "%u\n", regval);
  265. }
  266. /* 8 bit voltage values (the mins and maxs) */
  267. static ssize_t show_in8(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. SETUP_SHOW_DATA_PARAM(dev, attr);
  271. u8 nr = sda->index;
  272. return sprintf(buf, "%u\n",
  273. ((data->reg[param->msb[0]] *
  274. asc7621_in_scaling[nr]) / 0xc0));
  275. }
  276. static ssize_t store_in8(struct device *dev, struct device_attribute *attr,
  277. const char *buf, size_t count)
  278. {
  279. SETUP_STORE_DATA_PARAM(dev, attr);
  280. long reqval;
  281. u8 nr = sda->index;
  282. if (kstrtol(buf, 10, &reqval))
  283. return -EINVAL;
  284. reqval = clamp_val(reqval, 0, 0xffff);
  285. reqval = reqval * 0xc0 / asc7621_in_scaling[nr];
  286. reqval = clamp_val(reqval, 0, 0xff);
  287. mutex_lock(&data->update_lock);
  288. data->reg[param->msb[0]] = reqval;
  289. write_byte(client, param->msb[0], reqval);
  290. mutex_unlock(&data->update_lock);
  291. return count;
  292. }
  293. static ssize_t show_temp8(struct device *dev,
  294. struct device_attribute *attr, char *buf)
  295. {
  296. SETUP_SHOW_DATA_PARAM(dev, attr);
  297. return sprintf(buf, "%d\n", ((s8) data->reg[param->msb[0]]) * 1000);
  298. }
  299. static ssize_t store_temp8(struct device *dev,
  300. struct device_attribute *attr, const char *buf,
  301. size_t count)
  302. {
  303. SETUP_STORE_DATA_PARAM(dev, attr);
  304. long reqval;
  305. s8 temp;
  306. if (kstrtol(buf, 10, &reqval))
  307. return -EINVAL;
  308. reqval = clamp_val(reqval, -127000, 127000);
  309. temp = reqval / 1000;
  310. mutex_lock(&data->update_lock);
  311. data->reg[param->msb[0]] = temp;
  312. write_byte(client, param->msb[0], temp);
  313. mutex_unlock(&data->update_lock);
  314. return count;
  315. }
  316. /*
  317. * Temperatures that occupy 2 bytes always have the whole
  318. * number of degrees in the MSB with some part of the LSB
  319. * indicating fractional degrees.
  320. */
  321. /* mmmmmmmm.llxxxxxx */
  322. static ssize_t show_temp10(struct device *dev,
  323. struct device_attribute *attr, char *buf)
  324. {
  325. SETUP_SHOW_DATA_PARAM(dev, attr);
  326. u8 msb, lsb;
  327. int temp;
  328. mutex_lock(&data->update_lock);
  329. msb = data->reg[param->msb[0]];
  330. lsb = (data->reg[param->lsb[0]] >> 6) & 0x03;
  331. temp = (((s8) msb) * 1000) + (lsb * 250);
  332. mutex_unlock(&data->update_lock);
  333. return sprintf(buf, "%d\n", temp);
  334. }
  335. /* mmmmmm.ll */
  336. static ssize_t show_temp62(struct device *dev,
  337. struct device_attribute *attr, char *buf)
  338. {
  339. SETUP_SHOW_DATA_PARAM(dev, attr);
  340. u8 regval = data->reg[param->msb[0]];
  341. int temp = ((s8) (regval & 0xfc) * 1000) + ((regval & 0x03) * 250);
  342. return sprintf(buf, "%d\n", temp);
  343. }
  344. static ssize_t store_temp62(struct device *dev,
  345. struct device_attribute *attr, const char *buf,
  346. size_t count)
  347. {
  348. SETUP_STORE_DATA_PARAM(dev, attr);
  349. long reqval, i, f;
  350. s8 temp;
  351. if (kstrtol(buf, 10, &reqval))
  352. return -EINVAL;
  353. reqval = clamp_val(reqval, -32000, 31750);
  354. i = reqval / 1000;
  355. f = reqval - (i * 1000);
  356. temp = i << 2;
  357. temp |= f / 250;
  358. mutex_lock(&data->update_lock);
  359. data->reg[param->msb[0]] = temp;
  360. write_byte(client, param->msb[0], temp);
  361. mutex_unlock(&data->update_lock);
  362. return count;
  363. }
  364. /*
  365. * The aSC7621 doesn't provide an "auto_point2". Instead, you
  366. * specify the auto_point1 and a range. To keep with the sysfs
  367. * hwmon specs, we synthesize the auto_point_2 from them.
  368. */
  369. static const u32 asc7621_range_map[] = {
  370. 2000, 2500, 3330, 4000, 5000, 6670, 8000, 10000,
  371. 13330, 16000, 20000, 26670, 32000, 40000, 53330, 80000,
  372. };
  373. static ssize_t show_ap2_temp(struct device *dev,
  374. struct device_attribute *attr, char *buf)
  375. {
  376. SETUP_SHOW_DATA_PARAM(dev, attr);
  377. long auto_point1;
  378. u8 regval;
  379. int temp;
  380. mutex_lock(&data->update_lock);
  381. auto_point1 = ((s8) data->reg[param->msb[1]]) * 1000;
  382. regval =
  383. ((data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0]);
  384. temp = auto_point1 + asc7621_range_map[clamp_val(regval, 0, 15)];
  385. mutex_unlock(&data->update_lock);
  386. return sprintf(buf, "%d\n", temp);
  387. }
  388. static ssize_t store_ap2_temp(struct device *dev,
  389. struct device_attribute *attr,
  390. const char *buf, size_t count)
  391. {
  392. SETUP_STORE_DATA_PARAM(dev, attr);
  393. long reqval, auto_point1;
  394. int i;
  395. u8 currval, newval = 0;
  396. if (kstrtol(buf, 10, &reqval))
  397. return -EINVAL;
  398. mutex_lock(&data->update_lock);
  399. auto_point1 = data->reg[param->msb[1]] * 1000;
  400. reqval = clamp_val(reqval, auto_point1 + 2000, auto_point1 + 80000);
  401. for (i = ARRAY_SIZE(asc7621_range_map) - 1; i >= 0; i--) {
  402. if (reqval >= auto_point1 + asc7621_range_map[i]) {
  403. newval = i;
  404. break;
  405. }
  406. }
  407. newval = (newval & param->mask[0]) << param->shift[0];
  408. currval = read_byte(client, param->msb[0]);
  409. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  410. data->reg[param->msb[0]] = newval;
  411. write_byte(client, param->msb[0], newval);
  412. mutex_unlock(&data->update_lock);
  413. return count;
  414. }
  415. static ssize_t show_pwm_ac(struct device *dev,
  416. struct device_attribute *attr, char *buf)
  417. {
  418. SETUP_SHOW_DATA_PARAM(dev, attr);
  419. u8 config, altbit, regval;
  420. static const u8 map[] = {
  421. 0x01, 0x02, 0x04, 0x1f, 0x00, 0x06, 0x07, 0x10,
  422. 0x08, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f
  423. };
  424. mutex_lock(&data->update_lock);
  425. config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  426. altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
  427. regval = config | (altbit << 3);
  428. mutex_unlock(&data->update_lock);
  429. return sprintf(buf, "%u\n", map[clamp_val(regval, 0, 15)]);
  430. }
  431. static ssize_t store_pwm_ac(struct device *dev,
  432. struct device_attribute *attr,
  433. const char *buf, size_t count)
  434. {
  435. SETUP_STORE_DATA_PARAM(dev, attr);
  436. unsigned long reqval;
  437. u8 currval, config, altbit, newval;
  438. static const u16 map[] = {
  439. 0x04, 0x00, 0x01, 0xff, 0x02, 0xff, 0x05, 0x06,
  440. 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
  441. 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  442. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
  443. };
  444. if (kstrtoul(buf, 10, &reqval))
  445. return -EINVAL;
  446. if (reqval > 31)
  447. return -EINVAL;
  448. reqval = map[reqval];
  449. if (reqval == 0xff)
  450. return -EINVAL;
  451. config = reqval & 0x07;
  452. altbit = (reqval >> 3) & 0x01;
  453. config = (config & param->mask[0]) << param->shift[0];
  454. altbit = (altbit & param->mask[1]) << param->shift[1];
  455. mutex_lock(&data->update_lock);
  456. currval = read_byte(client, param->msb[0]);
  457. newval = config | (currval & ~(param->mask[0] << param->shift[0]));
  458. newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
  459. data->reg[param->msb[0]] = newval;
  460. write_byte(client, param->msb[0], newval);
  461. mutex_unlock(&data->update_lock);
  462. return count;
  463. }
  464. static ssize_t show_pwm_enable(struct device *dev,
  465. struct device_attribute *attr, char *buf)
  466. {
  467. SETUP_SHOW_DATA_PARAM(dev, attr);
  468. u8 config, altbit, minoff, val, newval;
  469. mutex_lock(&data->update_lock);
  470. config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  471. altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
  472. minoff = (data->reg[param->msb[2]] >> param->shift[2]) & param->mask[2];
  473. mutex_unlock(&data->update_lock);
  474. val = config | (altbit << 3);
  475. if (val == 3 || val >= 10)
  476. newval = 255;
  477. else if (val == 4)
  478. newval = 0;
  479. else if (val == 7)
  480. newval = 1;
  481. else if (minoff == 1)
  482. newval = 2;
  483. else
  484. newval = 3;
  485. return sprintf(buf, "%u\n", newval);
  486. }
  487. static ssize_t store_pwm_enable(struct device *dev,
  488. struct device_attribute *attr,
  489. const char *buf, size_t count)
  490. {
  491. SETUP_STORE_DATA_PARAM(dev, attr);
  492. long reqval;
  493. u8 currval, config, altbit, newval, minoff = 255;
  494. if (kstrtol(buf, 10, &reqval))
  495. return -EINVAL;
  496. switch (reqval) {
  497. case 0:
  498. newval = 0x04;
  499. break;
  500. case 1:
  501. newval = 0x07;
  502. break;
  503. case 2:
  504. newval = 0x00;
  505. minoff = 1;
  506. break;
  507. case 3:
  508. newval = 0x00;
  509. minoff = 0;
  510. break;
  511. case 255:
  512. newval = 0x03;
  513. break;
  514. default:
  515. return -EINVAL;
  516. }
  517. config = newval & 0x07;
  518. altbit = (newval >> 3) & 0x01;
  519. mutex_lock(&data->update_lock);
  520. config = (config & param->mask[0]) << param->shift[0];
  521. altbit = (altbit & param->mask[1]) << param->shift[1];
  522. currval = read_byte(client, param->msb[0]);
  523. newval = config | (currval & ~(param->mask[0] << param->shift[0]));
  524. newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
  525. data->reg[param->msb[0]] = newval;
  526. write_byte(client, param->msb[0], newval);
  527. if (minoff < 255) {
  528. minoff = (minoff & param->mask[2]) << param->shift[2];
  529. currval = read_byte(client, param->msb[2]);
  530. newval =
  531. minoff | (currval & ~(param->mask[2] << param->shift[2]));
  532. data->reg[param->msb[2]] = newval;
  533. write_byte(client, param->msb[2], newval);
  534. }
  535. mutex_unlock(&data->update_lock);
  536. return count;
  537. }
  538. static const u32 asc7621_pwm_freq_map[] = {
  539. 10, 15, 23, 30, 38, 47, 62, 94,
  540. 23000, 24000, 25000, 26000, 27000, 28000, 29000, 30000
  541. };
  542. static ssize_t show_pwm_freq(struct device *dev,
  543. struct device_attribute *attr, char *buf)
  544. {
  545. SETUP_SHOW_DATA_PARAM(dev, attr);
  546. u8 regval =
  547. (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  548. regval = clamp_val(regval, 0, 15);
  549. return sprintf(buf, "%u\n", asc7621_pwm_freq_map[regval]);
  550. }
  551. static ssize_t store_pwm_freq(struct device *dev,
  552. struct device_attribute *attr,
  553. const char *buf, size_t count)
  554. {
  555. SETUP_STORE_DATA_PARAM(dev, attr);
  556. unsigned long reqval;
  557. u8 currval, newval = 255;
  558. int i;
  559. if (kstrtoul(buf, 10, &reqval))
  560. return -EINVAL;
  561. for (i = 0; i < ARRAY_SIZE(asc7621_pwm_freq_map); i++) {
  562. if (reqval == asc7621_pwm_freq_map[i]) {
  563. newval = i;
  564. break;
  565. }
  566. }
  567. if (newval == 255)
  568. return -EINVAL;
  569. newval = (newval & param->mask[0]) << param->shift[0];
  570. mutex_lock(&data->update_lock);
  571. currval = read_byte(client, param->msb[0]);
  572. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  573. data->reg[param->msb[0]] = newval;
  574. write_byte(client, param->msb[0], newval);
  575. mutex_unlock(&data->update_lock);
  576. return count;
  577. }
  578. static const u32 asc7621_pwm_auto_spinup_map[] = {
  579. 0, 100, 250, 400, 700, 1000, 2000, 4000
  580. };
  581. static ssize_t show_pwm_ast(struct device *dev,
  582. struct device_attribute *attr, char *buf)
  583. {
  584. SETUP_SHOW_DATA_PARAM(dev, attr);
  585. u8 regval =
  586. (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  587. regval = clamp_val(regval, 0, 7);
  588. return sprintf(buf, "%u\n", asc7621_pwm_auto_spinup_map[regval]);
  589. }
  590. static ssize_t store_pwm_ast(struct device *dev,
  591. struct device_attribute *attr,
  592. const char *buf, size_t count)
  593. {
  594. SETUP_STORE_DATA_PARAM(dev, attr);
  595. long reqval;
  596. u8 currval, newval = 255;
  597. u32 i;
  598. if (kstrtol(buf, 10, &reqval))
  599. return -EINVAL;
  600. for (i = 0; i < ARRAY_SIZE(asc7621_pwm_auto_spinup_map); i++) {
  601. if (reqval == asc7621_pwm_auto_spinup_map[i]) {
  602. newval = i;
  603. break;
  604. }
  605. }
  606. if (newval == 255)
  607. return -EINVAL;
  608. newval = (newval & param->mask[0]) << param->shift[0];
  609. mutex_lock(&data->update_lock);
  610. currval = read_byte(client, param->msb[0]);
  611. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  612. data->reg[param->msb[0]] = newval;
  613. write_byte(client, param->msb[0], newval);
  614. mutex_unlock(&data->update_lock);
  615. return count;
  616. }
  617. static const u32 asc7621_temp_smoothing_time_map[] = {
  618. 35000, 17600, 11800, 7000, 4400, 3000, 1600, 800
  619. };
  620. static ssize_t show_temp_st(struct device *dev,
  621. struct device_attribute *attr, char *buf)
  622. {
  623. SETUP_SHOW_DATA_PARAM(dev, attr);
  624. u8 regval =
  625. (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  626. regval = clamp_val(regval, 0, 7);
  627. return sprintf(buf, "%u\n", asc7621_temp_smoothing_time_map[regval]);
  628. }
  629. static ssize_t store_temp_st(struct device *dev,
  630. struct device_attribute *attr,
  631. const char *buf, size_t count)
  632. {
  633. SETUP_STORE_DATA_PARAM(dev, attr);
  634. long reqval;
  635. u8 currval, newval = 255;
  636. u32 i;
  637. if (kstrtol(buf, 10, &reqval))
  638. return -EINVAL;
  639. for (i = 0; i < ARRAY_SIZE(asc7621_temp_smoothing_time_map); i++) {
  640. if (reqval == asc7621_temp_smoothing_time_map[i]) {
  641. newval = i;
  642. break;
  643. }
  644. }
  645. if (newval == 255)
  646. return -EINVAL;
  647. newval = (newval & param->mask[0]) << param->shift[0];
  648. mutex_lock(&data->update_lock);
  649. currval = read_byte(client, param->msb[0]);
  650. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  651. data->reg[param->msb[0]] = newval;
  652. write_byte(client, param->msb[0], newval);
  653. mutex_unlock(&data->update_lock);
  654. return count;
  655. }
  656. /*
  657. * End of data handlers
  658. *
  659. * These defines do nothing more than make the table easier
  660. * to read when wrapped at column 80.
  661. */
  662. /*
  663. * Creates a variable length array inititalizer.
  664. * VAA(1,3,5,7) would produce {1,3,5,7}
  665. */
  666. #define VAA(args...) {args}
  667. #define PREAD(name, n, pri, rm, rl, m, s, r) \
  668. {.sda = SENSOR_ATTR(name, S_IRUGO, show_##r, NULL, n), \
  669. .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
  670. .shift[0] = s,}
  671. #define PWRITE(name, n, pri, rm, rl, m, s, r) \
  672. {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
  673. .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
  674. .shift[0] = s,}
  675. /*
  676. * PWRITEM assumes that the initializers for the .msb, .lsb, .mask and .shift
  677. * were created using the VAA macro.
  678. */
  679. #define PWRITEM(name, n, pri, rm, rl, m, s, r) \
  680. {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
  681. .priority = pri, .msb = rm, .lsb = rl, .mask = m, .shift = s,}
  682. static struct asc7621_param asc7621_params[] = {
  683. PREAD(in0_input, 0, PRI_HIGH, 0x20, 0x13, 0, 0, in10),
  684. PREAD(in1_input, 1, PRI_HIGH, 0x21, 0x18, 0, 0, in10),
  685. PREAD(in2_input, 2, PRI_HIGH, 0x22, 0x11, 0, 0, in10),
  686. PREAD(in3_input, 3, PRI_HIGH, 0x23, 0x12, 0, 0, in10),
  687. PREAD(in4_input, 4, PRI_HIGH, 0x24, 0x14, 0, 0, in10),
  688. PWRITE(in0_min, 0, PRI_LOW, 0x44, 0, 0, 0, in8),
  689. PWRITE(in1_min, 1, PRI_LOW, 0x46, 0, 0, 0, in8),
  690. PWRITE(in2_min, 2, PRI_LOW, 0x48, 0, 0, 0, in8),
  691. PWRITE(in3_min, 3, PRI_LOW, 0x4a, 0, 0, 0, in8),
  692. PWRITE(in4_min, 4, PRI_LOW, 0x4c, 0, 0, 0, in8),
  693. PWRITE(in0_max, 0, PRI_LOW, 0x45, 0, 0, 0, in8),
  694. PWRITE(in1_max, 1, PRI_LOW, 0x47, 0, 0, 0, in8),
  695. PWRITE(in2_max, 2, PRI_LOW, 0x49, 0, 0, 0, in8),
  696. PWRITE(in3_max, 3, PRI_LOW, 0x4b, 0, 0, 0, in8),
  697. PWRITE(in4_max, 4, PRI_LOW, 0x4d, 0, 0, 0, in8),
  698. PREAD(in0_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 0, bitmask),
  699. PREAD(in1_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 1, bitmask),
  700. PREAD(in2_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 2, bitmask),
  701. PREAD(in3_alarm, 3, PRI_HIGH, 0x41, 0, 0x01, 3, bitmask),
  702. PREAD(in4_alarm, 4, PRI_HIGH, 0x42, 0, 0x01, 0, bitmask),
  703. PREAD(fan1_input, 0, PRI_HIGH, 0x29, 0x28, 0, 0, fan16),
  704. PREAD(fan2_input, 1, PRI_HIGH, 0x2b, 0x2a, 0, 0, fan16),
  705. PREAD(fan3_input, 2, PRI_HIGH, 0x2d, 0x2c, 0, 0, fan16),
  706. PREAD(fan4_input, 3, PRI_HIGH, 0x2f, 0x2e, 0, 0, fan16),
  707. PWRITE(fan1_min, 0, PRI_LOW, 0x55, 0x54, 0, 0, fan16),
  708. PWRITE(fan2_min, 1, PRI_LOW, 0x57, 0x56, 0, 0, fan16),
  709. PWRITE(fan3_min, 2, PRI_LOW, 0x59, 0x58, 0, 0, fan16),
  710. PWRITE(fan4_min, 3, PRI_LOW, 0x5b, 0x5a, 0, 0, fan16),
  711. PREAD(fan1_alarm, 0, PRI_HIGH, 0x42, 0, 0x01, 2, bitmask),
  712. PREAD(fan2_alarm, 1, PRI_HIGH, 0x42, 0, 0x01, 3, bitmask),
  713. PREAD(fan3_alarm, 2, PRI_HIGH, 0x42, 0, 0x01, 4, bitmask),
  714. PREAD(fan4_alarm, 3, PRI_HIGH, 0x42, 0, 0x01, 5, bitmask),
  715. PREAD(temp1_input, 0, PRI_HIGH, 0x25, 0x10, 0, 0, temp10),
  716. PREAD(temp2_input, 1, PRI_HIGH, 0x26, 0x15, 0, 0, temp10),
  717. PREAD(temp3_input, 2, PRI_HIGH, 0x27, 0x16, 0, 0, temp10),
  718. PREAD(temp4_input, 3, PRI_HIGH, 0x33, 0x17, 0, 0, temp10),
  719. PREAD(temp5_input, 4, PRI_HIGH, 0xf7, 0xf6, 0, 0, temp10),
  720. PREAD(temp6_input, 5, PRI_HIGH, 0xf9, 0xf8, 0, 0, temp10),
  721. PREAD(temp7_input, 6, PRI_HIGH, 0xfb, 0xfa, 0, 0, temp10),
  722. PREAD(temp8_input, 7, PRI_HIGH, 0xfd, 0xfc, 0, 0, temp10),
  723. PWRITE(temp1_min, 0, PRI_LOW, 0x4e, 0, 0, 0, temp8),
  724. PWRITE(temp2_min, 1, PRI_LOW, 0x50, 0, 0, 0, temp8),
  725. PWRITE(temp3_min, 2, PRI_LOW, 0x52, 0, 0, 0, temp8),
  726. PWRITE(temp4_min, 3, PRI_LOW, 0x34, 0, 0, 0, temp8),
  727. PWRITE(temp1_max, 0, PRI_LOW, 0x4f, 0, 0, 0, temp8),
  728. PWRITE(temp2_max, 1, PRI_LOW, 0x51, 0, 0, 0, temp8),
  729. PWRITE(temp3_max, 2, PRI_LOW, 0x53, 0, 0, 0, temp8),
  730. PWRITE(temp4_max, 3, PRI_LOW, 0x35, 0, 0, 0, temp8),
  731. PREAD(temp1_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 4, bitmask),
  732. PREAD(temp2_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 5, bitmask),
  733. PREAD(temp3_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 6, bitmask),
  734. PREAD(temp4_alarm, 3, PRI_HIGH, 0x43, 0, 0x01, 0, bitmask),
  735. PWRITE(temp1_source, 0, PRI_LOW, 0x02, 0, 0x07, 4, bitmask),
  736. PWRITE(temp2_source, 1, PRI_LOW, 0x02, 0, 0x07, 0, bitmask),
  737. PWRITE(temp3_source, 2, PRI_LOW, 0x03, 0, 0x07, 4, bitmask),
  738. PWRITE(temp4_source, 3, PRI_LOW, 0x03, 0, 0x07, 0, bitmask),
  739. PWRITE(temp1_smoothing_enable, 0, PRI_LOW, 0x62, 0, 0x01, 3, bitmask),
  740. PWRITE(temp2_smoothing_enable, 1, PRI_LOW, 0x63, 0, 0x01, 7, bitmask),
  741. PWRITE(temp3_smoothing_enable, 2, PRI_LOW, 0x63, 0, 0x01, 3, bitmask),
  742. PWRITE(temp4_smoothing_enable, 3, PRI_LOW, 0x3c, 0, 0x01, 3, bitmask),
  743. PWRITE(temp1_smoothing_time, 0, PRI_LOW, 0x62, 0, 0x07, 0, temp_st),
  744. PWRITE(temp2_smoothing_time, 1, PRI_LOW, 0x63, 0, 0x07, 4, temp_st),
  745. PWRITE(temp3_smoothing_time, 2, PRI_LOW, 0x63, 0, 0x07, 0, temp_st),
  746. PWRITE(temp4_smoothing_time, 3, PRI_LOW, 0x3c, 0, 0x07, 0, temp_st),
  747. PWRITE(temp1_auto_point1_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
  748. bitmask),
  749. PWRITE(temp2_auto_point1_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
  750. bitmask),
  751. PWRITE(temp3_auto_point1_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
  752. bitmask),
  753. PWRITE(temp4_auto_point1_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
  754. bitmask),
  755. PREAD(temp1_auto_point2_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
  756. bitmask),
  757. PREAD(temp2_auto_point2_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
  758. bitmask),
  759. PREAD(temp3_auto_point2_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
  760. bitmask),
  761. PREAD(temp4_auto_point2_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
  762. bitmask),
  763. PWRITE(temp1_auto_point1_temp, 0, PRI_LOW, 0x67, 0, 0, 0, temp8),
  764. PWRITE(temp2_auto_point1_temp, 1, PRI_LOW, 0x68, 0, 0, 0, temp8),
  765. PWRITE(temp3_auto_point1_temp, 2, PRI_LOW, 0x69, 0, 0, 0, temp8),
  766. PWRITE(temp4_auto_point1_temp, 3, PRI_LOW, 0x3b, 0, 0, 0, temp8),
  767. PWRITEM(temp1_auto_point2_temp, 0, PRI_LOW, VAA(0x5f, 0x67), VAA(0),
  768. VAA(0x0f), VAA(4), ap2_temp),
  769. PWRITEM(temp2_auto_point2_temp, 1, PRI_LOW, VAA(0x60, 0x68), VAA(0),
  770. VAA(0x0f), VAA(4), ap2_temp),
  771. PWRITEM(temp3_auto_point2_temp, 2, PRI_LOW, VAA(0x61, 0x69), VAA(0),
  772. VAA(0x0f), VAA(4), ap2_temp),
  773. PWRITEM(temp4_auto_point2_temp, 3, PRI_LOW, VAA(0x3c, 0x3b), VAA(0),
  774. VAA(0x0f), VAA(4), ap2_temp),
  775. PWRITE(temp1_crit, 0, PRI_LOW, 0x6a, 0, 0, 0, temp8),
  776. PWRITE(temp2_crit, 1, PRI_LOW, 0x6b, 0, 0, 0, temp8),
  777. PWRITE(temp3_crit, 2, PRI_LOW, 0x6c, 0, 0, 0, temp8),
  778. PWRITE(temp4_crit, 3, PRI_LOW, 0x3d, 0, 0, 0, temp8),
  779. PWRITE(temp5_enable, 4, PRI_LOW, 0x0e, 0, 0x01, 0, bitmask),
  780. PWRITE(temp6_enable, 5, PRI_LOW, 0x0e, 0, 0x01, 1, bitmask),
  781. PWRITE(temp7_enable, 6, PRI_LOW, 0x0e, 0, 0x01, 2, bitmask),
  782. PWRITE(temp8_enable, 7, PRI_LOW, 0x0e, 0, 0x01, 3, bitmask),
  783. PWRITE(remote1_offset, 0, PRI_LOW, 0x1c, 0, 0, 0, temp62),
  784. PWRITE(remote2_offset, 1, PRI_LOW, 0x1d, 0, 0, 0, temp62),
  785. PWRITE(pwm1, 0, PRI_HIGH, 0x30, 0, 0, 0, u8),
  786. PWRITE(pwm2, 1, PRI_HIGH, 0x31, 0, 0, 0, u8),
  787. PWRITE(pwm3, 2, PRI_HIGH, 0x32, 0, 0, 0, u8),
  788. PWRITE(pwm1_invert, 0, PRI_LOW, 0x5c, 0, 0x01, 4, bitmask),
  789. PWRITE(pwm2_invert, 1, PRI_LOW, 0x5d, 0, 0x01, 4, bitmask),
  790. PWRITE(pwm3_invert, 2, PRI_LOW, 0x5e, 0, 0x01, 4, bitmask),
  791. PWRITEM(pwm1_enable, 0, PRI_LOW, VAA(0x5c, 0x5c, 0x62), VAA(0, 0, 0),
  792. VAA(0x07, 0x01, 0x01), VAA(5, 3, 5), pwm_enable),
  793. PWRITEM(pwm2_enable, 1, PRI_LOW, VAA(0x5d, 0x5d, 0x62), VAA(0, 0, 0),
  794. VAA(0x07, 0x01, 0x01), VAA(5, 3, 6), pwm_enable),
  795. PWRITEM(pwm3_enable, 2, PRI_LOW, VAA(0x5e, 0x5e, 0x62), VAA(0, 0, 0),
  796. VAA(0x07, 0x01, 0x01), VAA(5, 3, 7), pwm_enable),
  797. PWRITEM(pwm1_auto_channels, 0, PRI_LOW, VAA(0x5c, 0x5c), VAA(0, 0),
  798. VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
  799. PWRITEM(pwm2_auto_channels, 1, PRI_LOW, VAA(0x5d, 0x5d), VAA(0, 0),
  800. VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
  801. PWRITEM(pwm3_auto_channels, 2, PRI_LOW, VAA(0x5e, 0x5e), VAA(0, 0),
  802. VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
  803. PWRITE(pwm1_auto_point1_pwm, 0, PRI_LOW, 0x64, 0, 0, 0, u8),
  804. PWRITE(pwm2_auto_point1_pwm, 1, PRI_LOW, 0x65, 0, 0, 0, u8),
  805. PWRITE(pwm3_auto_point1_pwm, 2, PRI_LOW, 0x66, 0, 0, 0, u8),
  806. PWRITE(pwm1_auto_point2_pwm, 0, PRI_LOW, 0x38, 0, 0, 0, u8),
  807. PWRITE(pwm2_auto_point2_pwm, 1, PRI_LOW, 0x39, 0, 0, 0, u8),
  808. PWRITE(pwm3_auto_point2_pwm, 2, PRI_LOW, 0x3a, 0, 0, 0, u8),
  809. PWRITE(pwm1_freq, 0, PRI_LOW, 0x5f, 0, 0x0f, 0, pwm_freq),
  810. PWRITE(pwm2_freq, 1, PRI_LOW, 0x60, 0, 0x0f, 0, pwm_freq),
  811. PWRITE(pwm3_freq, 2, PRI_LOW, 0x61, 0, 0x0f, 0, pwm_freq),
  812. PREAD(pwm1_auto_zone_assigned, 0, PRI_LOW, 0, 0, 0x03, 2, bitmask),
  813. PREAD(pwm2_auto_zone_assigned, 1, PRI_LOW, 0, 0, 0x03, 4, bitmask),
  814. PREAD(pwm3_auto_zone_assigned, 2, PRI_LOW, 0, 0, 0x03, 6, bitmask),
  815. PWRITE(pwm1_auto_spinup_time, 0, PRI_LOW, 0x5c, 0, 0x07, 0, pwm_ast),
  816. PWRITE(pwm2_auto_spinup_time, 1, PRI_LOW, 0x5d, 0, 0x07, 0, pwm_ast),
  817. PWRITE(pwm3_auto_spinup_time, 2, PRI_LOW, 0x5e, 0, 0x07, 0, pwm_ast),
  818. PWRITE(peci_enable, 0, PRI_LOW, 0x40, 0, 0x01, 4, bitmask),
  819. PWRITE(peci_avg, 0, PRI_LOW, 0x36, 0, 0x07, 0, bitmask),
  820. PWRITE(peci_domain, 0, PRI_LOW, 0x36, 0, 0x01, 3, bitmask),
  821. PWRITE(peci_legacy, 0, PRI_LOW, 0x36, 0, 0x01, 4, bitmask),
  822. PWRITE(peci_diode, 0, PRI_LOW, 0x0e, 0, 0x07, 4, bitmask),
  823. PWRITE(peci_4domain, 0, PRI_LOW, 0x0e, 0, 0x01, 4, bitmask),
  824. };
  825. static struct asc7621_data *asc7621_update_device(struct device *dev)
  826. {
  827. struct i2c_client *client = to_i2c_client(dev);
  828. struct asc7621_data *data = i2c_get_clientdata(client);
  829. int i;
  830. /*
  831. * The asc7621 chips guarantee consistent reads of multi-byte values
  832. * regardless of the order of the reads. No special logic is needed
  833. * so we can just read the registers in whatever order they appear
  834. * in the asc7621_params array.
  835. */
  836. mutex_lock(&data->update_lock);
  837. /* Read all the high priority registers */
  838. if (!data->valid ||
  839. time_after(jiffies, data->last_high_reading + INTERVAL_HIGH)) {
  840. for (i = 0; i < ARRAY_SIZE(asc7621_register_priorities); i++) {
  841. if (asc7621_register_priorities[i] == PRI_HIGH) {
  842. data->reg[i] =
  843. i2c_smbus_read_byte_data(client, i) & 0xff;
  844. }
  845. }
  846. data->last_high_reading = jiffies;
  847. } /* last_reading */
  848. /* Read all the low priority registers. */
  849. if (!data->valid ||
  850. time_after(jiffies, data->last_low_reading + INTERVAL_LOW)) {
  851. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  852. if (asc7621_register_priorities[i] == PRI_LOW) {
  853. data->reg[i] =
  854. i2c_smbus_read_byte_data(client, i) & 0xff;
  855. }
  856. }
  857. data->last_low_reading = jiffies;
  858. } /* last_reading */
  859. data->valid = true;
  860. mutex_unlock(&data->update_lock);
  861. return data;
  862. }
  863. /*
  864. * Standard detection and initialization below
  865. *
  866. * Helper function that checks if an address is valid
  867. * for a particular chip.
  868. */
  869. static inline int valid_address_for_chip(int chip_type, int address)
  870. {
  871. int i;
  872. for (i = 0; asc7621_chips[chip_type].addresses[i] != I2C_CLIENT_END;
  873. i++) {
  874. if (asc7621_chips[chip_type].addresses[i] == address)
  875. return 1;
  876. }
  877. return 0;
  878. }
  879. static void asc7621_init_client(struct i2c_client *client)
  880. {
  881. int value;
  882. /* Warn if part was not "READY" */
  883. value = read_byte(client, 0x40);
  884. if (value & 0x02) {
  885. dev_err(&client->dev,
  886. "Client (%d,0x%02x) config is locked.\n",
  887. i2c_adapter_id(client->adapter), client->addr);
  888. }
  889. if (!(value & 0x04)) {
  890. dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
  891. i2c_adapter_id(client->adapter), client->addr);
  892. }
  893. /*
  894. * Start monitoring
  895. *
  896. * Try to clear LOCK, Set START, save everything else
  897. */
  898. value = (value & ~0x02) | 0x01;
  899. write_byte(client, 0x40, value & 0xff);
  900. }
  901. static int
  902. asc7621_probe(struct i2c_client *client)
  903. {
  904. struct asc7621_data *data;
  905. int i, err;
  906. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  907. return -EIO;
  908. data = devm_kzalloc(&client->dev, sizeof(struct asc7621_data),
  909. GFP_KERNEL);
  910. if (data == NULL)
  911. return -ENOMEM;
  912. i2c_set_clientdata(client, data);
  913. mutex_init(&data->update_lock);
  914. /* Initialize the asc7621 chip */
  915. asc7621_init_client(client);
  916. /* Create the sysfs entries */
  917. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  918. err =
  919. device_create_file(&client->dev,
  920. &(asc7621_params[i].sda.dev_attr));
  921. if (err)
  922. goto exit_remove;
  923. }
  924. data->class_dev = hwmon_device_register(&client->dev);
  925. if (IS_ERR(data->class_dev)) {
  926. err = PTR_ERR(data->class_dev);
  927. goto exit_remove;
  928. }
  929. return 0;
  930. exit_remove:
  931. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  932. device_remove_file(&client->dev,
  933. &(asc7621_params[i].sda.dev_attr));
  934. }
  935. return err;
  936. }
  937. static int asc7621_detect(struct i2c_client *client,
  938. struct i2c_board_info *info)
  939. {
  940. struct i2c_adapter *adapter = client->adapter;
  941. int company, verstep, chip_index;
  942. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  943. return -ENODEV;
  944. for (chip_index = FIRST_CHIP; chip_index <= LAST_CHIP; chip_index++) {
  945. if (!valid_address_for_chip(chip_index, client->addr))
  946. continue;
  947. company = read_byte(client,
  948. asc7621_chips[chip_index].company_reg);
  949. verstep = read_byte(client,
  950. asc7621_chips[chip_index].verstep_reg);
  951. if (company == asc7621_chips[chip_index].company_id &&
  952. verstep == asc7621_chips[chip_index].verstep_id) {
  953. strscpy(info->type, asc7621_chips[chip_index].name,
  954. I2C_NAME_SIZE);
  955. dev_info(&adapter->dev, "Matched %s at 0x%02x\n",
  956. asc7621_chips[chip_index].name, client->addr);
  957. return 0;
  958. }
  959. }
  960. return -ENODEV;
  961. }
  962. static void asc7621_remove(struct i2c_client *client)
  963. {
  964. struct asc7621_data *data = i2c_get_clientdata(client);
  965. int i;
  966. hwmon_device_unregister(data->class_dev);
  967. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  968. device_remove_file(&client->dev,
  969. &(asc7621_params[i].sda.dev_attr));
  970. }
  971. }
  972. static const struct i2c_device_id asc7621_id[] = {
  973. {"asc7621", asc7621},
  974. {"asc7621a", asc7621a},
  975. {},
  976. };
  977. MODULE_DEVICE_TABLE(i2c, asc7621_id);
  978. static struct i2c_driver asc7621_driver = {
  979. .class = I2C_CLASS_HWMON,
  980. .driver = {
  981. .name = "asc7621",
  982. },
  983. .probe_new = asc7621_probe,
  984. .remove = asc7621_remove,
  985. .id_table = asc7621_id,
  986. .detect = asc7621_detect,
  987. .address_list = normal_i2c,
  988. };
  989. static int __init sm_asc7621_init(void)
  990. {
  991. int i, j;
  992. /*
  993. * Collect all the registers needed into a single array.
  994. * This way, if a register isn't actually used for anything,
  995. * we don't retrieve it.
  996. */
  997. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  998. for (j = 0; j < ARRAY_SIZE(asc7621_params[i].msb); j++)
  999. asc7621_register_priorities[asc7621_params[i].msb[j]] =
  1000. asc7621_params[i].priority;
  1001. for (j = 0; j < ARRAY_SIZE(asc7621_params[i].lsb); j++)
  1002. asc7621_register_priorities[asc7621_params[i].lsb[j]] =
  1003. asc7621_params[i].priority;
  1004. }
  1005. return i2c_add_driver(&asc7621_driver);
  1006. }
  1007. static void __exit sm_asc7621_exit(void)
  1008. {
  1009. i2c_del_driver(&asc7621_driver);
  1010. }
  1011. MODULE_LICENSE("GPL");
  1012. MODULE_AUTHOR("George Joseph");
  1013. MODULE_DESCRIPTION("Andigilog aSC7621 and aSC7621a driver");
  1014. module_init(sm_asc7621_init);
  1015. module_exit(sm_asc7621_exit);