asc7621.c 35 KB

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